Javaを介してCassandraインスタンスへの簡単な接続を試みました。cqlshに「デモ」キースペースを作成し、Javaプログラムにテーブルを作成しました。コードは以下のとおりです。
使用した瓶:
cassandra-all-2.1.2
public class CassandraConnection {
public static void main(String[] args){
String ipAddress="127.0.0.1";
String keySpace="demo";
Cluster cluster;
Session session;
cluster=Cluster.builder().addContactPoint(ipAddress).build();
session=cluster.connect(keySpace);
System.out.println("====================Before insert");
String cqlInsertStmt="insert into users (lastname,age,city,email,firstname) values"
+"('Gopalan',32,'Paramakkudi','[email protected]','Murugan') ";
session.execute(cqlInsertStmt);
String cqlSelectStmt="select * from users";
ResultSet resultSet=session.execute(cqlSelectStmt);
System.out.println("=================After insert");
for(Row row: resultSet){
System.out.format("%s %s %d %s %s \n", row.getString("firstname"),row.getString("lastname"),row.getInt("age"),row.getString("city"),row.getString("email"));
}
System.out.println("=================After update");
}
}
次のエラーが発生します。
Failed to instantiate SLF4J LoggerFactory
Reported exception:
Java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException
at org.slf4j.LoggerFactory.bind(LoggerFactory.Java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.Java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.Java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.Java:357)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.Java:383)
at com.datastax.driver.core.Cluster.<clinit>(Cluster.Java:60)
at CassandraConnection.main(CassandraConnection.Java:21)
Caused by: Java.lang.ClassNotFoundException: ch.qos.logback.core.joran.spi.JoranException
at Java.net.URLClassLoader$1.run(Unknown Source)
at Java.net.URLClassLoader$1.run(Unknown Source)
at Java.security.AccessController.doPrivileged(Native Method)
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)
... 7 more
Exception in thread "main" Java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException
at org.slf4j.LoggerFactory.bind(LoggerFactory.Java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.Java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.Java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.Java:357)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.Java:383)
at com.datastax.driver.core.Cluster.<clinit>(Cluster.Java:60)
at CassandraConnection.main(CassandraConnection.Java:21)
Caused by: Java.lang.ClassNotFoundException: ch.qos.logback.core.joran.spi.JoranException
at Java.net.URLClassLoader$1.run(Unknown Source)
at Java.net.URLClassLoader$1.run(Unknown Source)
at Java.security.AccessController.doPrivileged(Native Method)
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)
... 7 more
この問題の根本的な原因は、必要なjarがCLASSPATHで利用できないことです。次の依存関係を追加してください-問題が解決します。
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>