Siegfried
Siegfried

Reputation: 705

Wanted: Help making SpringBoot CassandraTemplate Example Work

Update (2017-JAN-31) All the sample code is available code is available here: C Y Kan's Mastering Cassandra Tutorial Video with free sample code. I tried to enhance chapter 54 with the sample code from skk and it did not help. The pom below is actually from chapter 52 (I guess CY forgot to change the name tag when he copied pom.xml).

No matter what I do, I get one of the errors below depending on which versions I specify (or if I specify a version) for Cassandra-Driver-Core.

As skk suggested, I have tried spring boot 1.4.4 and other versions.

I have confirmed that I do indeed have a keyspace "cwt" and person defined in Cassandra. What more details do you need?

Ok, here is my attempt to enhance CY Kan's source code with skk's suggestion. This results in the error below about not being able to create bean session. If I explicitly specify 3.1.2 the Cassandra driver core it succeeds in connecting but gives me the noSuchMethod error. In contrast to the chapter 52 error (below), the noSuchMethod error happens in the Cassandra Mapping bean in Chapter 54.

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

}

What other details would you like?

Update (2017-JAN-30): skk, please post your java driver code.

As suggested by skk, I have tried the suggested version number and tried the suggested beans.

I'm still getting a similar error:

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;

Thanks

Earlier Update: As suggested, I tried removing the version numbers for the Cassandra components and just specify 1.4.3.RELEASE for spring-boot-starter-parent and I received this error:

Error creating bean with name 'session' defined in class path resource [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: Invocation of init method failed; nested exception is com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table schema_keyspaces))

So now it is not even connecting (before it was connecting).

Original: After many hours of google searching and not finding any java/spring-boot/cassandra samples that work, I finally found the CQLTemplate example in chapter 52 from the video here:[PacktPub C Y Kan's Mastering Cassandra 2nd Ed] works. 1. This works! (However, I need to use CassandraTemplate instead of CQLTemplate).

This means that I have Cassandra 3.9 running correctly on Windows 10 and java 1.8 - do you agree?

The next example (chapter 53 from this tutorial) uses the CassandraTemplate and I get this error:

java.lang.NoSuchMethodError: com.datastax.driver.core.DataType.asJavaClass()Ljava/lang/Class;

I discovered from Chapter 52 that I have to change the version of com.datastax.cassandra/cassandra-driver-core from 2.1.6 to 3.0.0. I've been experimenting with changing the versions of com.datastax.cassandra/assandra-driver-mapping and no luck so far. Here is my slightly modified pom.xml that I downloaded from PacktPub (below). Can someone help me make this work or suggested another CassandraTemplate example (that maps POJOs to the Cassandra database) that works?

Thank you very much! Siegfried

<?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>

Upvotes: 0

Views: 3851

Answers (1)

denzal
denzal

Reputation: 1253

You can use the below code as example: Spring boot version - 1.4.3 and Spring-data cassandra version - 1.4.6. Let me know if you need the sample code for Java driver.

@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());
    }
}

Upvotes: 0

Related Questions