Firstly, I am new to Cassandra, I came from relational databases. I have used Hibernate API's which made the database related tasks pretty simple. I am using java based drivers from datastax to connect and execute queries. They provided the mapping library which implements JPA. So far so good. I created a model class as follows:
@Table(keyspace = "webscc", name = "t_data", readConsistency = "QUORUM",
writeConsistency = "QUORUM",caseSensitiveKeyspace = false,
caseSensitiveTable = false)
public class ChartData {
@PartitionKey
private UUID id;
@ClusteringColumn(0)
private Date timeStamp;
@ClusteringColumn(1)
private String controller;
@Column(name = "value")
private double value;
/** Constructors, getters and setters*/
}
Now I want to create a new ChartData object and save it into database. For that, I wanted to implement Mapper.
Session session = Cluster.builder().addContactPoint(props.getProperty(HOSTNAME)).build()
.connect(props.getProperty(KEYSPACE));
MappingManager manager = new MappingManager(session);
Mapper<ChartData> mapper = manager.mapper(ChartData.class);
ChartData chartData = new ChartData("controller",24.44);
mapper.save(chartData);
I configured everything, still I could not save ChartData object into table. The problem is it can not create table automatically, although I configured everything in ChartData via annotations. Did I do something wrong, or should I create all required tables manually. Why this does not create database schema automatically like Hibernate does?
Datastax Driver mapper does not create table automatically
There is a open issue On this : https://datastax-oss.atlassian.net/browse/JAVA-569
You have to first create the table manually.
CREATE TABLE webscc.t_data(
id uuid,
timestamp timestamp,
controller text,
value double,
PRIMARY KEY (id, timestamp, controller)
);
Or Use the below code after session initialized
session.execute("CREATE TABLE IF NOT EXISTS webscc.t_data(\n" +
" id uuid,\n" +
" timestamp timestamp,\n" +
" controller text,\n" +
" value double,\n" +
" PRIMARY KEY (id, timestamp, controller)\n" +
" )");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.