Spring Data Cassandra连接问题

时间:2018-06-05 11:24:56

标签: java spring spring-boot cassandra spring-data-cassandra

我正在尝试连接Spring数据cassandra我在网络上运行cassandra服务器。以下是详细信息

spring.data.cassandra.contact-points=192.168.33.10
spring.data.cassandra.cluster-name=GEO-LOCAL-Cluster
spring.data.cassandra.port=9042
spring.data.cassandra.keyspace-name=organizationlicenses
spring.data.cassandra.username=cassandra
spring.data.cassandra.password=cassandra

我尝试自动注入这些属性时,通过spring.data为它们添加了前缀。但是,我遇到的大多数示例和信息都使用如下扩展的配置类 CassandraConfig扩展了AbstractCassandraConfiguration ......

@Bean
public CassandraCqlClusterFactoryBean cluster() {
    CassandraCqlClusterFactoryBean cluster = 
      new CassandraClusterFactoryBean();
    cluster.setContactPoints(properties.getContactPoints());
    cluster.setPort(properties.getPort());
    cluster.setUsername(properties.getUsername());
    cluster.setPassword(properties.getPassword());

    return cluster;
}

这是我得到的例外 -

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'session' defined in class path resource [com/example/demo/DemoClientCassandraConfiguration.class]: Invocation of init method failed; nested exception is com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /192.168.33.10:9042 (com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table schema_keyspaces))

我不确定我错过了什么。如何使用配置类指定clustername GEO-LOCAL-Cluster。我已经跟踪了所有属性都正确填充。

我在实体类上使用了@Table,在主要的应用方法上使用了@EnableCassandraRepositories。

1 个答案:

答案 0 :(得分:1)

以下是为Cassandra客户端设置连接的三个必需设置:

  • 设置运行Cassandra服务器的主机名(contactPoints)
  • 端口只是监听端口。
  • KeyspaceName是定义数据复制的命名空间 节点,基于Cassandra相关概念。

Cassandra的配置

var obj = JSON.parse(json)[0].Data[0]; 

创建EntityCassandraRepository

    var json = '[{"Status":"Success","Data":[{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"31","WishListName":"General","WishListItems":[{"WishListItemID":"3","ItemCode":"4414082000005","ItemName":"Notebook 4414-082","Image":"http://aljeel.gct.om/ProductsImages/4414082000005_MT1.jpg","ItemPrice":0.500,"CreatedDate":"25/05/2018"}]},{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"24","WishListName":"Default","WishListItems":[]}],"Message":null}]';

JSON.parse(json)[0].Data.forEach(obj=>{
obj.data = obj.WishListItems;
    delete obj.WishListItems;
 json = JSON.stringify([obj]);
    console.log("FINAL JSON " + (json));
});

相同仓库的配置

@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {

    //Must override this method
    @Override
    protected String getKeyspaceName() {
        return "sampleKeySpace";
    }

    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = 
          new CassandraClusterFactoryBean();
        cluster.setContactPoints("127.0.0.1");
        cluster.setPort(9142);
        return cluster;
    }

    @Bean
    public CassandraMappingContext cassandraMapping() 
      throws ClassNotFoundException {
        return new BasicCassandraMappingContext();
    }
}