更新(2017-JAN-31)所有示例代码均可在此处获得: C Y Kan's Mastering Cassandra Tutorial Video with free sample code。我尝试使用skk的示例代码增强第54章,但它没有帮助。下面的pom实际上来自第52章(我猜CY在复制pom.xml时忘了更改名称标签)。
无论我做什么,我都会得到以下错误之一,具体取决于我为Cassandra-Driver-Core指定的版本(或者我指定的版本)。
正如skk建议的那样,我尝试过spring boot 1.4.4和其他版本。
我已经确认我确实有一个键空间" cwt"和卡萨德拉定义的人。您需要更多细节吗?
好的,这是我尝试使用skk的建议来增强CY Kan的源代码。这导致以下错误无法创建bean会话。如果我明确指定3.1.2 Cassandra驱动程序核心它成功连接但给我noSuchMethod错误。与第52章错误(下面)相反,第54章中的Cassandra Mapping bean中发生了noSuchMethod错误。
package com.cassandrawebtrader.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
@Configuration
@EnableCassandraRepositories(basePackages="com.cassandrawebtrader")
public class CassandraConfig extends AbstractCassandraConfiguration {
@Override
protected String getKeyspaceName() {
// TODO Auto-generated method stub
return "cwt";
}
/*
@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints("127.0.0.1");
cluster.setPort(9042);
return cluster;
}
*/
@Bean
public CassandraMappingContext cassandraMapping() {
return new BasicCassandraMappingContext();
}
// begin paste from http://stackoverflow.com/questions/41881168/wanted-help-making-springboot-cassandratemplate-example-work/41910388#41910388
String cassandraContactPoints = "127.0.0.1";
String cassandraPort = "9042";
@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(cassandraContactPoints);
cluster.setPort(Integer.parseInt(cassandraPort));
return cluster;
}
@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}
@Bean
public org.springframework.data.cassandra.convert.CassandraConverter converter() {
return new org.springframework.data.cassandra.convert.MappingCassandraConverter(mappingContext());
}
@Bean
public org.springframework.data.cassandra.config.CassandraSessionFactoryBean session() throws Exception {
org.springframework.data.cassandra.config.CassandraSessionFactoryBean session = new org.springframework.data.cassandra.config.CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(getKeyspaceName());
session.setConverter(converter());
session.setSchemaAction(org.springframework.data.cassandra.config.SchemaAction.NONE);
return session;
}
/*
@Bean
public org.springframework.data.cassandra.core.CassandraOperations cassandraTemplate() throws Exception {
return new org.springframework.data.cassandra.core.CassandraTemplate(session().getObject());
}
*/
// end paste from http://stackoverflow.com/questions/41881168/wanted-help-making-springboot-cassandratemplate-example-work/41910388#41910388
}
您还想要其他哪些细节?
更新(2017-JAN-30):skk,请发布您的java驱动程序代码。
正如skk所建议的那样,我尝试了建议的版本号并尝试了建议的bean。
我仍然收到类似的错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraMapping' defined in class path resource [com/cassandrawebtrader/config/CassandraConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.mapping.CassandraMappingContext]: Factory method 'cassandraMapping' threw exception; nested exception is java.lang.NoSuchMethodError: com.datastax.driver.core.DataType.asJavaClass()Ljava/lang/Class;
由于
早期更新:正如建议的那样,我尝试删除Cassandra组件的版本号,并为spring-boot-starter-parent指定1.4.3.RELEASE并收到此错误:
使用名称' session'创建bean时出错在类路径资源中定义[org / springframework / boot / autoconfigure / data / cassandra / CassandraDataAutoConfiguration.class]:调用init方法失败;嵌套异常是com.datastax.driver.core.exceptions.NoHostAvailableException:所有尝试查询的主机都失败了(尝试过:localhost / 127.0.0.1:9042(com.datastax.driver.core.exceptions.InvalidQueryException:unconfigured table schema_keyspaces ))
所以现在它甚至没有连接(在它连接之前)。
原件: 经过几个小时的谷歌搜索,没有找到任何有效的java / spring-boot / cassandra样本,我终于在第52章的视频中找到了CQLTemplate示例:[PacktPub C Y Kan' s Mastering Cassandra 2nd Ed]有效。 1。这有效! (但是,我需要使用CassandraTemplate而不是CQLTemplate)。
这意味着我在Windows 10和java 1.8上正确运行Cassandra 3.9 - 您同意吗?
下一个示例(本教程第53章)使用CassandraTemplate,我收到此错误:
java.lang.NoSuchMethodError:com.datastax.driver.core.DataType.asJavaClass()Ljava / lang / Class;
我从第52章发现,我必须将com.datastax.cassandra / cassandra-driver-core的版本从2.1.6更改为3.0.0。我一直在尝试更改com.datastax.cassandra / assandra-driver-mapping的版本,到目前为止还没有运气。这是我从PacktPub(下面)下载的稍微修改过的pom.xml。有人可以帮我做这项工作或建议另一个CassandraTemplate示例(将POJO映射到Cassandra数据库)吗?
非常感谢! 齐格弗里德
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.cassandrawebtrader</groupId>
<artifactId>chapter51</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>chapter51</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>1.2.5.RELEASE</version> -->
<version>1.2.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.0.0</version> <!-- 2.1.6 -->
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.0.0</version> <!-- 2.1.6 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
答案 0 :(得分:0)
您可以使用以下代码作为示例:Spring启动版本 - 1.4.3和Spring-data cassandra版本 - 1.4.6。如果您需要Java驱动程序的示例代码,请告诉我。
@Configuration
public class CassandraConfig {
@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(cassandraContactPoints);
cluster.setPort(Integer.parseInt(cassandraPort));
return cluster;
}
@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}
@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}
@Bean
public CassandraSessionFactoryBean session() throws Exception {
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(cassandraKeyspace);
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);
return session;
}
@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}
}