user1643003
user1643003

Reputation: 285

Spring Data Cassandra Connection Issue

I am trying to connect Spring data cassandra I have cassandra server running on network. Here are the details

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

I have prefixed them through spring.data as I was trying to auto inject these properties. But, most examples and information i came across are using configuration class extended as below CassandraConfig extends 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;
}

Here is the exception i get -

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

I am not sure what I am missing. How do I specify the clustername GEO-LOCAL-Cluster using configuration class. I have traced all the properties are populated correctly.

I have used @Table on entity class and @EnableCassandraRepositories on the main method of application.

Upvotes: 2

Views: 4529

Answers (1)

Yati Sawhney
Yati Sawhney

Reputation: 1402

Following are the three mandatory settings to setup the connection for a Cassandra client:

  • Setup the host name(contactPoints) on which Cassandra server is running
  • Port is simply the listening port.
  • KeyspaceName is the namespace that defines the data replication on nodes, based on a Cassandra related concept.

Configuration for the Cassandra

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

Create the EntityCassandraRepository

@Repository
public interface EntityRepository extends CassandraRepository<Entity> {

}

Configuration for the same repo

@Configuration
@EnableCassandraRepositories(
  basePackages = "org.yates.data.cassandra.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {

}

Upvotes: 1

Related Questions