Not able to insert data using MongoTemplate of Spring data MongoDB - mongodb

``I am getting the following exception while instantiatingthg mongoTemplate
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/data/mapping/model/FieldNamingStrategy
at org.springframework.data.mongodb.core.MongoTemplate.getDefaultMongoConverter(MongoTemplate.java:1962)
at org.springframework.data.mongodb.core.MongoTemplate.(MongoTemplate.java:210)
at org.springframework.data.mongodb.core.MongoTemplate.(MongoTemplate.java:174)
at com.adobe.ffc.controller.MongoDBTest.createData(MongoDBTest.java:29)
at com.adobe.ffc.controller.MongoDBTest.main(MongoDBTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: org.springframework.data.mapping.model.FieldNamingStrategy
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 10 more
Process finished with exit code 1
the error is coming from line 1 in the following code :
MongoOperations mongoOps = new MongoTemplate(new Mongo(),"database");
User user = new User();
user.setId("200");
user.setFullName("Mongo Template");
user.setStatus("A");
user.setAge("29");
mongoOps.insert(user);
The pom configration are as follows:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>

The problem is mixing Spring Data Release Trains with one another.
spring-data-mongodb 1.4 is part of Release Train Codd that requies to have spring-data-commons 1.7, while spring-data-commons 1.5 which you're currently using was part of the Arora release.
So either downgrade to spring-data-mongodb 1.2 or upgrade to spring-data-commons 1.7. Maybe also have a look at your dependencies and check for potential conflicts.
Sidenote: you can checout the spring data wiki pages to see which versions have been part of a particular release.

Related

Cloud Dataproc can't access Cloud Storage bucket

I have a cloud dataproc Spark job that also uses Cloud Strage API from Drvier side (to choose specific files from the same folder to work with).
Here are maven dependencies:
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>2.4.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.101.0</version>
</dependency>
</dependencies>
Here is the simplest version of the code that fails:
import com.google.cloud.storage._
object Test {
def main(args: Array[String]): Unit = {
val storage = StorageOptions.getDefaultInstance().getService()
--> storage.list("intent_raw")
}
}
here is stacktrace:
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;
at com.google.api.gax.retrying.BasicRetryingFuture.<init>(BasicRetryingFuture.java:84)
at com.google.api.gax.retrying.DirectRetryingExecutor.createFuture(DirectRetryingExecutor.java:88)
at com.google.api.gax.retrying.DirectRetryingExecutor.createFuture(DirectRetryingExecutor.java:74)
at com.google.cloud.RetryHelper.run(RetryHelper.java:75)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
at com.google.cloud.storage.StorageImpl.listBlobs(StorageImpl.java:372)
at com.google.cloud.storage.StorageImpl.list(StorageImpl.java:328)
--> at ai.mandal.cloud.dataproc.Test$.main(Test.scala:14)
at ai.mandal.cloud.dataproc.Test.main(Test.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.JavaMainApplication.start(SparkApplication.scala:52)
at org.apache.spark.deploy.SparkSubmit.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:845)
at org.apache.spark.deploy.SparkSubmit.doRunMain$1(SparkSubmit.scala:161)
at org.apache.spark.deploy.SparkSubmit.submit(SparkSubmit.scala:184)
at org.apache.spark.deploy.SparkSubmit.doSubmit(SparkSubmit.scala:86)
at org.apache.spark.deploy.SparkSubmit$$anon$2.doSubmit(SparkSubmit.scala:920)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:929)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
My question is generally what can cause it, and also if I am running it from a dataproc service (which has access to the bucket), do I need to configure separate credentials for that.
The solution was to add
spark.executor.userClassPathFirst = true
spark.driver.userClassPathFirst = true
to job properties.
The problem is caused by conflicting versions of guava found in google-cloud-storage and the host environment.
Google recommends to shade the conflicting guava in your dependency, I've tried that too but that didn't work for this case.

Drools program throwing java.lang.NoClassDefFoundError:org/drools/core/impl/KnowledgeBaseFactoryServiceImpl

Need some help in running a simple POC from eclipse.
I have written a java class and a rule (.drl) in eclipse (using drools distribution 7.6.0).Code snippet where i'm creating a knowledgebase to run the rules:
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("rules/Sample Rule.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
The code does not have any error. But on run time, i see the below exception
Caused by: java.lang.ClassNotFoundException: org.drools.core.impl.KnowledgeBaseFactoryServiceImpl
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 14 more
I have imported Knowledge-api-6.5.0.Final.jar. But do not see
org.drools.core.impl.KnowledgeBaseFactoryServiceImpl
instead i see
org.drools.impl.KnowledgeBaseFactoryServiceImpl
inside the jar. Am i missing something?
If you are using Drools 7.6.0, why did you import Knowledge-api-6.5.0.Final.jar?
Even if you import the right version of that jar, it will only contain the API interfaces, but no implementation at all. You still need to provide the classes implementing those interfaces while executing your code.
If you are using Maven, these are the dependencies you will need:
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools.version}</version>
</dependency>
For version 7.7.0-SNAPSHOT, these dependencies translate to the following jar files:
kie-api-7.7.0-SNAPSHOT.jar
drools-compiler-7.7.0-SNAPSHOT.jar
drools-core-7.7.0-SNAPSHOT.jar
The problem of not using a dependency management system (like Maven) is that you may still need to add some extra jars to your classpath.
Hope it helps,

OSGi got FrameworkFactory could not be instantiated Error

I am trying to use OSGi and maven to develop an standard alone application.
I have the following osgi dependency in pom.xml
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
</dependency>
I tried the following code
import org.osgi.framework.launch.FrameworkFactory;
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
and got this error message
Exception in thread "main" java.util.ServiceConfigurationError: org.osgi.framework.launch.FrameworkFactory: Provider org.osgi.framework.launch.FrameworkFactory could not be instantiated
at java.util.ServiceLoader.fail(ServiceLoader.java:232)
at java.util.ServiceLoader.access$100(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at com.scottfu.ci.test.OSGiTest.main(OSGiTest.java:19)
Caused by: java.lang.InstantiationException: org.osgi.framework.launch.FrameworkFactory
at java.lang.Class.newInstance(Class.java:427)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
... 3 more
Caused by: java.lang.NoSuchMethodException: org.osgi.framework.launch.FrameworkFactory.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 4 more
what is wrong?
The dependency you added is the OSGi Core API.
You need an implementation like equinox, felix or knopflerfish.
E.g.:
Instead of adding the dependency you mentioned, add
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.main</artifactId>
<version>5.6.2</version>
</dependency>

Not able to connect to Redshift server using postgresql or redshift driver

I have a code which connects to redshift server and gets some table entry. When I run it locally it works fine. But when I build it as a jar and run it on hadoop then I am getting this exception :
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
I have added this in my pom.xml :
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
I have also added the jar file location in HADOOP_CLASSPATH variable in hadoop-env.sh.
Is there something I am missing?
Thanks.
I changed the location of my jar and it is working now. I still don't understand why it was not picking up the jar from previous path.

Java XMPP clint with smack 4.1 NoClassDefFoundError:

I am trying to write XMPP Clint using smack 4.1.1. I have added smack jar files into build path. I am using eclipse IDE to write the code.
However, when I am trying to run application I get NoClassDefFoundErrors. I fixed some of these errors adding xpp3, jxmpp and minidns jar files into the build path. Yet, I am getting the following error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/jxmpp/util/cache/ExpirationCache
at org.jivesoftware.smack.util.dns.minidns.MiniDnsResolver.<clinit>(MiniDnsResolver.java:46)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:213)
at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193)
at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163)
at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148)
at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:116)
at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
at org.jivesoftware.smack.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
at JabberSmackAPI.login(JabberSmackAPI.java:29)
at JabberSmackAPI.main(JabberSmackAPI.java:76)
Caused by: java.lang.ClassNotFoundException: org.jxmpp.util.cache.ExpirationCache
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 12 more
How to fix this issue?
In the internet, it is advised to use gradle or maven for building the same. If so, can you help me how to use the same? I have never used them before.
As per below stack trace
Caused by: java.lang.ClassNotFoundException: org.jxmpp.util.cache.ExpirationCache
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
It seem You are missing jxmpp-util-cache-0.5.0-alpha2.jar in your class path. Download and add this jar to your lib/ directory of your application .
In the internet, it is advised to use gradle or maven for building the
same. If so, can you help me how to use the same?
Create a meven project in eclipse .If you are new to meven project refer
How to create a Java project with Maven
And add Below dependencies to pom.xml of your meven build project
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-java7</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-tcp</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-im</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-extensions</artifactId>
<version>4.1.0</version>
</dependency>
For more details refer this link