I have started working with Cassandra database
. I am planning to use Datastax API
to upsert/read
into/from Cassandra database
. I am totally new to this Datastax API
(which uses new Binary protocol) and I am not able to find lot of documentations as well which have some proper examples.
CREATE TABLE profile (
id varchar,
account varchar,
advertising varchar,
behavior varchar,
info varchar,
PRIMARY KEY (id)
);
Now below is the Singleton class
that I have created for connecting to Cassandra database using Datastax API
which uses new Binary protocol-
public class CassandraDatastaxConnection {
private static CassandraDatastaxConnection _instance;
protected static Cluster cluster;
protected static Session session;
public static synchronized CassandraDatastaxConnection getInstance() {
if (_instance == null) {
_instance = new CassandraDatastaxConnection();
}
return _instance;
}
/**
* Creating Cassandra connection using Datastax API
*
*/
private CassandraDatastaxConnection() {
try{
cluster = Cluster.builder().addContactPoint("localhost").build();
session = cluster.connect("my_keyspace");
} catch (NoHostAvailableException e) {
throw new RuntimeException(e);
}
}
public static Cluster getCluster() {
return cluster;
}
public static Session getSession() {
return session;
}
}
**First question-** let me know if I am missing anything in the above singleton class
while making connection to Cassandra database using Datastax API which uses new Binary protocol.
**Second question-** Now I am trying to upsert and read data
into/from Cassandra database-
These are the methods I have in my DAO's which will use the above Singleton class-
public Map<String, String> getColumnNames(final String userId, final Collection<String> columnNames) {
//I am not sure what I am supposed to do here?
//Given a userId, I need to retrieve those columnNames from the Cassandra database
//And then put it in the map with column name and its value and then finally return the map
Map<String, String> attributes = new ConcurrentHashMap<String, String>();
for(String col : columnNames ) {
attributes.put(col, colValue);
}
return attributes;
}
/**
* Performs an upsert of the specified attributes for the specified id.
*/
public void upsertAttributes(final String userId, final Map<String, String> columnNameAndValue) {
//I am not sure what I am supposed to do here to upsert the data in Cassandra database.
//Given a userId, I need to upsert the columns values into Cassandra database.
//columnNameAndValue is the map which will have column name as the key and corresponding column value as the value.
}
Can anyone help me with this? I am totally new to this Datastax API which is using new Binary protocol so having lot of problem on this.
Thanks for the help.