问题
I am new to Cassandra and this has been giving me issues. I downloaded apache-Cassandra 3.11 and I am using spring boot 1.5.4.RELEASE. I did some research and found a source where it says it may be because Spring data is using a different Cassandra driver core version? But the latest uses cql 3 correct? I also made a java class configuration file. The issue may be here.
import org.springframework.cassandra.config.CassandraCqlClusterFactoryBean;
import org.springframework.cassandra.config.DataCenterReplication;
import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableCassandraRepositories
public class CassandraConfig extends AbstractCassandraConfiguration{
private static final String KEYSPACE = "my_keyspace";
@Bean
@Override
public CassandraCqlClusterFactoryBean cluster() {
CassandraCqlClusterFactoryBean bean = new CassandraCqlClusterFactoryBean();
bean.setKeyspaceCreations(getKeyspaceCreations());
return bean;
}
/**
* if it dont exist , create it
* @return
*/
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
@Override
protected String getKeyspaceName() {
return KEYSPACE;
}
@Override
public String[] getEntityBasePackages() {
return new String[]{"com.cassandra"};
}
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
List<CreateKeyspaceSpecification> createKeyspaceSpecifications = new ArrayList<>();
createKeyspaceSpecifications.add(getKeySpaceSpecification());
return createKeyspaceSpecifications;
}
// Below method creates "my_keyspace" if it doesnt exist.
private CreateKeyspaceSpecification getKeySpaceSpecification() {
CreateKeyspaceSpecification pandaCoopKeyspace = new CreateKeyspaceSpecification();
DataCenterReplication dcr = new DataCenterReplication("dc1", 3L);
pandaCoopKeyspace.name(KEYSPACE);
pandaCoopKeyspace.ifNotExists(true).createKeyspace().withNetworkReplication(dcr);
return pandaCoopKeyspace;
}
@Override
public String getContactPoints() {
return "localhost";
}
}
This is the bean am trying to use
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;
import java.util.UUID;
@Table("user")
public class User {
@PrimaryKey
private UUID id;
private String firstName;
private String lastName;
public User() {
super();
}
public User(UUID id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
If you need more information, let me know, I cannot for the life of me figure out what is wrong. Thank you.
回答1:
Create user
table into keyspace my_keyspace
CREATE TABLE user(
id uuid PRIMARY KEY,
firstName text,
lastName text
);
来源:https://stackoverflow.com/questions/45249787/com-datastax-driver-core-exceptions-invalidqueryexception-unconfigured-table-us