com.datastax.driver.core.exceptions.InvalidQueryException:未配置的表用户“

时间:2017-07-22 01:35:09

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

我是Cassandra的新手,这给了我一些问题。我下载了apache-Cassandra 3.11,我使用的是spring boot 1.5.4.RELEASE。我做了一些研究并找到了一个source,它说它可能是因为Spring数据使用的是不同的Cassandra驱动核心版本?但最新使用cql 3是否正确?我还制作了一个java类配置文件。问题可能在这里。

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";
    }
}

这是我尝试使用的bean

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 + '\'' +
                '}';
    }
}

如果您需要更多信息,请告诉我,我不能为我的生活弄清楚出了什么问题。谢谢。

1 个答案:

答案 0 :(得分:2)

在密钥空间user

中创建my_keyspace
CREATE TABLE user(
    id uuid PRIMARY KEY,
    firstName text,
    lastName text
);