Upsert /使用Datastax API读取/读取Cassandra数据库(使用新的二进制协议) [英] Upsert/Read into/from Cassandra database using Datastax API (using new Binary protocol)
问题描述
我开始使用 Cassandra数据库
。我计划使用 Datastax API 来 upsert / read
into / from Cassandra数据库
。我完全是新的 Datastax API
(它使用新的二进制协议),我不能找到很多文档以及一些适当的例子。
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 column family profile
with key_validation_class = 'UTF8Type'
and comparator = 'UTF8Type'
and default_validation_class = 'UTF8Type'
and column_metadata = [
{column_name : crd, validation_class : 'DateType'}
{column_name : lmd, validation_class : 'DateType'}
{column_name : account, validation_class : 'UTF8Type'}
{column_name : advertising, validation_class : 'UTF8Type'}
{column_name : behavior, validation_class : 'UTF8Type'}
{column_name : info, validation_class : 'UTF8Type'}
];
下面是 Singleton类
已使用 Datastax API
创建连接到Cassandra数据库,该数据库使用新的二进制协议 -
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;
}
}
第一个问题 - let我知道如果在使用新的二进制协议的Datastax API连接到Cassandra数据库时,我在上面的 singleton类
中缺少任何东西。
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.
第二个问题现在我试图从/从Cassandra数据库
Second question- Now I am trying to upsert and read data
into/from Cassandra database-
这些是我在我的DAO中使用上面的Singleton类的方法 -
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.
}
任何人都可以帮助我吗?我完全是新的Datastax API,它使用新的二进制协议,因此有很多问题。
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.
感谢您的帮助。
推荐答案
在您的cassandra.yaml文件中查找标签 start_native_transport ,默认情况下禁用它,启用它。
In your cassandra.yaml file look for the tag start_native_transport, by default its disabled, enable it.
使用Datastax Java Driver与jdbc driver非常相似。
Playing with Datastax Java Driver is quite similar like jdbc driver.
插入代码
String query = "insert into test(key,col1,col2) values('1','value1','value2')";
session.execute(query);
从Cassandra读取
String query="select * from test;";
ResultSet result = session.execute(query);
for (Row rows: result){
System.out.println(rows.getString("key"));
}
这篇关于Upsert /使用Datastax API读取/读取Cassandra数据库(使用新的二进制协议)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!