5

我有一个 Maven 项目 A

项目 A 的 pom.xml :

<dependencies>
       <dependency> 
            <groupId>com.app.cops</groupId>
            <artifactId>cassandra-logging</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
</dependencies>

cassandra-logging 项目的 pom.xml:

<dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.9.0.Final</version>
        </dependency>
        <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-core</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
            <version>3.0.0</version>
        </dependency>   
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-mapping</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
  </dependencies>

cops-logging 项目具有以下代码:

CassandraCopsComponentLogger.instance = new CassandraCopsComponentLogger();

                String hosts = CassandraClientUtil.getHost();
                String localDC = CassandraClientUtil.getLocalDC();
                Cluster cluster;
                if (StringUtils.isNotEmpty(localDC))
                {
                    cluster = Cluster.builder().addContactPoints(hosts.split(","))
                            .withCredentials(CassandraCopsComponentLogger.USER_NAME, CassandraCopsComponentLogger.AUTH_CODE)
                            .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_ONE))
                            .withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(localDC).build())).build();
                }
                else
                {
                    cluster = Cluster.builder().addContactPoints(hosts.split(","))
                            .withCredentials(CassandraCopsComponentLogger.USER_NAME, CassandraCopsComponentLogger.AUTH_CODE)
                            .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_ONE)).build();
                }

                Session session = cluster.connect();
                CassandraCopsComponentLogger.mappingManager = new MappingManager(session);

我继续在以下行中遇到异常:

cluster = Cluster.builder().addContactPoints(hosts.split(","))

我在 cassandra-logging 项目中有一个单元测试,可以很好地使用这个代码。但是当我从项目 AI 中调用相同的代码时,得到

java.lang.NoClassDefFoundError: Could not initialize class com.datastax.driver.core.Cluster
4

3 回答 3

1

我能够通过将 cassandra 依赖版本降级为<version>2.1.9</version>. 不知道这有什么帮助,但我能够继续前进。

于 2016-04-25T21:32:21.730 回答
1

将 cassandra-driver-core 和 cassandra-driver-mapping 的版本更新到 3.3.0 - 这对我有用:

        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
            <version>3.3.0</version>
        </dependency>   
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-mapping</artifactId>
            <version>3.3.0</version>
        </dependency>
于 2017-07-11T13:24:37.027 回答
0

我遇到了类似的问题。我得到的错误是:

Factory method 'sqlSession' threw exception; 
nested exception is java.lang.NoClassDefFoundError: com/datastax/oss/protocol/internal/SegmentCodec

相关 pom.xml 段:

<properties>
..
<oss-java-driver.version>4.9.0</oss-java-driver.version>
..
</properties>
..
<dependency>
    <groupId>com.datastax.oss</groupId>
    <artifactId>java-driver-core</artifactId>
    <version>${oss-java-driver.version}</version>
</dependency>
<dependency>
    <groupId>com.datastax.oss</groupId>
    <artifactId>java-driver-query-builder</artifactId>
    <version>${oss-java-driver.version}</version>
</dependency>

对我有用的解决方案:我必须明确声明对 native-protocol 的依赖以覆盖 native-protocol 的版本。

<properties>
..
<cassandra-native-protocol.version>1.4.11</cassandra-native-protocol.version>
..
</properties>
..
<dependency>
    <groupId>com.datastax.oss</groupId>
    <artifactId>native-protocol</artifactId>
    <version>${cassandra-native-protocol.version}</version>
</dependency>

参考:https ://community.datastax.com/answers/8185/view.html

于 2020-10-13T04:57:52.940 回答