Spring 4 + Cassandra 3.4 + 不工作

Posted

技术标签:

【中文标题】Spring 4 + Cassandra 3.4 + 不工作【英文标题】:Spring 4 + Cassandra 3.4 + Not working 【发布时间】:2017-09-10 08:54:47 【问题描述】:

我正在尝试使用 Spring 4 连接 Cassandra 3.4,但我无法做到。 我尝试在没有弹簧的情况下连接它,它工作得很好,但是在使用 Spring 时它有一些我无法找出的问题。

我的 CassandraConnectionManager.java

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.HostDistance;
import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SocketOptions;

public class CassandraConnectionManager 
    int concurrency = 50;
    int maxRequestsPerConnection = 128;
    int maxConnections = 10;
    int coreConnections = 5;
    int port = 7001;
    String hosts[];
    private Cluster cluster;
    private Session session;

    public CassandraConnectionManager(String hosts, String port, int maxConn, int coreConn) 
        this.maxConnections = maxConn;
        this.coreConnections = coreConn;
        this.port = Integer.parseInt(port);
        this.hosts = hosts.split(",");
        this.CassandraConnect();
    

    public CassandraConnectionManager(String hosts) 
        this.maxConnections = concurrency / maxRequestsPerConnection + 1;
        this.hosts = hosts.split(",");
        this.CassandraConnect();
    

    private void CassandraConnect() 
        PoolingOptions pools = new PoolingOptions();
        pools.setCoreConnectionsPerHost(HostDistance.LOCAL, coreConnections);
        pools.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnections);
        pools.setCoreConnectionsPerHost(HostDistance.REMOTE, coreConnections);
        pools.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnections);
        pools.setMaxRequestsPerConnection(HostDistance.LOCAL, concurrency);

        Cluster.Builder builder = new Cluster.Builder();
        builder.withPort(port);
        for (String host : hosts) 
            builder.addContactPoints(host);
        
        builder.withPoolingOptions(pools);
        builder.withSocketOptions(new SocketOptions().setTcpNoDelay(true));
        this.cluster = builder.build();
        this.session = cluster.connect();
        System.out.println("Connected");
    

    public Session getSession() 
        return this.session;
    

    /** Close cluster. */

    public void close() 
        cluster.close();
    

我的 CassandraDAO

import org.apache.log4j.Logger;

import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.netcore.CassandraConnectionManager;

public class CassandraDAO 

    private CassandraConnectionManager casConnManager;
    private Session casSession;
    private PreparedStatement insertIntoP1events;

    final static Logger logger = Logger.getLogger(CassandraDAO.class);

    public CassandraDAO(CassandraConnectionManager casConnManager)
        this.casConnManager = casConnManager;
        this.casSession = casConnManager.getSession();
    

    public void insert(Person book) 
        StringBuilder sb = new StringBuilder();
          sb.append("INSERT INTO ").append("login_event")
          .append("(id, age, name) ")
          .append("VALUES (").append(book.getId()).append(", '")
          .append(book.getAge()).append("', '")
          .append(book.getName()).append("');");

        String query = sb.toString();
        casSession.execute(query);
    


然后我有一个带有 id,name 年龄的 pojo 和它的 setter getter。

下面是我的 Test.java

public class TEST 
    private static ClassPathXmlApplicationContext ctx ;

    public static void main(String[] args) 
        Person p = new Person("S1", "Test", 24);
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        CassandraDAO casDAO = (CassandraDAO) ctx.getBean("casDao");
        casDAO.insert(p);

    


我的 ApplicaitonContext.xml

    <?xml version='1.0'?>
    <?xml version='1.0'?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd " >



      <bean id="casandraManager" class ="com.test.CassandraConnectionManager" >
            <constructor-arg name="hosts" value="127.0.0.1" />
            <constructor-arg name="port" value="9042" />
            <constructor-arg name="maxConn" value="100" />
            <constructor-arg name="coreConn" value="5" />
            <constructor-arg name="ttlDays" value="7" />
        </bean>

        <bean id="casDao" class="com.test.CassandraDAO" >
            <constructor-arg index="0" ref="casandraManager" />
        </bean>

    </beans>

最后是我的日志

 Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.expression.spel.SpelParserConfiguration.<init>(Lorg/springframework/expression/spel/SpelCompilerMode;Ljava/lang/ClassLoader;)V
    at org.springframework.context.expression.StandardBeanExpressionResolver.<init>(StandardBeanExpressionResolver.java:98)
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:625)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:513)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.netcore.TEST.main(TEST.java:20)

没有 Spring 也可以正常工作。

Test2.java

public class Test2 
    public static void main(String[] args) 
        CassandraConnectionManager cm = new CassandraConnectionManager("localhost", "9042", 2, 2);
        CassandraDAO cd = new CassandraDAO(cm);


    

【问题讨论】:

您在编译应用程序时是否连接到互联网。似乎应用程序无法从 http://www.springframework.org/schema/data/cassandra/ 找到 xsd 架构定义 您是否尝试过搜索错误消息“无法找到 XML 模式命名空间的 Spring NamespaceHandler”?似乎 *** 上的其他很多人之前都遇到过/问过这个问题,例如***.com/questions/1937767/… 是的 Rajith,我已连接到互联网。 Yes tylerparsons 我试过还是同样的问题,配置问题:找不到 XML 模式命名空间的 Spring NamespaceHandler [springframework.org/schema/data/cassandra] 【参考方案1】:

相同的代码在以下驱动程序中运行完美,驱动程序存在一些问题。

Required JARs

【讨论】:

以上是关于Spring 4 + Cassandra 3.4 + 不工作的主要内容,如果未能解决你的问题,请参考以下文章

Spring认证中国教育管理中心-Apache Cassandra 的 Spring 数据

Spring Boot 1.5.x with Spring Data Cassandra 1.5.x 与 Cassandra 2.1 不兼容

如何忽略 spring-boot-cassandra 默认配置来加载 cassandra 连接实例

Spring Data Cassandra 中的分页和排序查询

具有响应式Cassandra 的Spring Data

禁用 Spring Boot 启动器 Cassandra 进行单元测试