Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra + SpringBoot, java.lang.ClassNotFoundException: com.datastax.oss.protocol.internal.SegmentCodec

I'm trying to create a Spring application using Cassandra DB. But getting following error when I try to run the application.

Caused by: java.lang.NoClassDefFoundError: com/datastax/oss/protocol/internal/SegmentCodec
    ... 103 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.datastax.oss.protocol.internal.SegmentCodec
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_152-release]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_152-release]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_152-release]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_152-release]
    ... 103 common frames omitted

Spring Boot version: 2.3.4.RELEASE

Cassandra(spring-boot-starter-data-cassandra) version: 3.0.4

application.yml file:

spring:

  data:
    cassandra:
      keyspaceName: my_keyspace
      contactPoints: localhost
      port: 9042
      schema-action: CREATE_IF_NOT_EXISTS
      local-datacenter: datacenter1

CassandraConfig.java

@Configuration
@EnableCassandraRepositories(basePackages = "com.myproject.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {

    @Value("${spring.data.cassandra.keyspaceName}")
    private String keyspace;

    @Value("${spring.data.cassandra.contactPoints}")
    private String contactPoints;

    @Value("${spring.data.cassandra.port}")
    private int port;

    @Value("${spring.data.cassandra.local-datacenter}")
    private String dataCenter;

    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.CREATE_IF_NOT_EXISTS;
    }

    @Override
    protected SessionBuilderConfigurer getSessionBuilderConfigurer() {
        return new SessionBuilderConfigurer() {
            @Override
            public CqlSessionBuilder configure(CqlSessionBuilder cqlSessionBuilder) {

                return cqlSessionBuilder
                        .addContactPoint(new InetSocketAddress(
                                contactPoints,
                                getPort()))
                        ;

            }
        };
    }

    @Override
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
        return Arrays.asList(CreateKeyspaceSpecification
                .createKeyspace(getKeyspaceName())
                .ifNotExists(true)
                .withSimpleReplication()
                .with(KeyspaceOption.DURABLE_WRITES));

    }

    @Override
    protected KeyspacePopulator keyspacePopulator() {
        ResourceKeyspacePopulator keyspacePopulate = new ResourceKeyspacePopulator();
        keyspacePopulate.setSeparator(";");
        keyspacePopulate.setScripts(new ClassPathResource("table-schema.cql"));
        return keyspacePopulate;
    }

    @Override
    protected String getKeyspaceName() {
        return keyspace;
    }

    @Override
    protected int getPort(){
        return port;
    }

    @Override
    public String[] getEntityBasePackages() {
        return new String[]{"com.myproject.entity"};
    }

    @Override
    protected String getLocalDataCenter() {
        return dataCenter;
    }

}

table-schema.cql

CREATE TABLE IF NOT EXISTS orders (order_id text, user_name text, created_at timestamp, item_id text, PRIMARY KEY ((order_id), item_id));

Following dependencies are present in pom.xml(driver.version is 4.9.0)

<dependency>
            <groupId>com.datastax.oss</groupId>
            <artifactId>java-driver-core</artifactId>
            <version>${driver.version}</version>
        </dependency>
        <dependency>
            <groupId>com.datastax.oss</groupId>
            <artifactId>java-driver-query-builder</artifactId>
            <version>${driver.version}</version>
        </dependency>
        <dependency>
            <groupId>com.datastax.oss</groupId>
            <artifactId>java-driver-mapper-runtime</artifactId>
            <version>${driver.version}</version>
        </dependency>
like image 891
gulgulu Avatar asked Oct 13 '20 02:10

gulgulu


1 Answers

Try adding this dependency to get the latest native protocol version:

    <!-- https://mvnrepository.com/artifact/com.datastax.oss/native-protocol -->
    <dependency>
        <groupId>com.datastax.oss</groupId>
        <artifactId>native-protocol</artifactId>
        <version>1.4.11</version>
    </dependency>

Spring boot seems to link to an older protocol version which causes this exception.

like image 110
bswynn Avatar answered Oct 17 '22 16:10

bswynn