Java 使用Datastax API(使用新的二进制协议)向Cassandra数据库中插入/读取数据
我已经开始使用卡桑德拉数据库了。我计划使用Java 使用Datastax API(使用新的二进制协议)向Cassandra数据库中插入/读取数据,java,cassandra,cql,cql3,datastax-java-driver,Java,Cassandra,Cql,Cql3,Datastax Java Driver,我已经开始使用卡桑德拉数据库了。我计划使用向卡桑德拉数据库中插入/读取。我对这个datastaxapi(它使用新的二进制协议)是完全陌生的,我也找不到很多有一些适当示例的文档 create column family profile with key_validation_class = 'UTF8Type' and comparator = 'UTF8Type' and default_validation_class = 'UTF8Type' and colu
向卡桑德拉数据库中插入/读取。我对这个datastaxapi
(它使用新的二进制协议)是完全陌生的,我也找不到很多有一些适当示例的文档
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数据库-
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;
}
}
第一个问题-使用使用新二进制协议的Datastax API连接Cassandra数据库时,请告知我是否缺少上述单例类中的任何内容
第二个问题-现在我正在尝试向卡桑德拉数据库中插入和读取数据-
这些是我在DAO中使用的方法,将使用上面的单例类-
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.
}
publicmap getColumnNames(最终字符串userId,最终集合columnNames){
//我不知道我应该在这里做什么?
//给定一个用户名,我需要从Cassandra数据库中检索这些列名
//然后将其放入带有列名及其值的映射中,最后返回映射
映射属性=新的ConcurrentHashMap();
for(字符串列:列名称){
attributes.put(col,colValue);
}
返回属性;
}
/**
*为指定id执行指定属性的upsert。
*/
public void upserttributes(最终字符串userId、最终映射列名称和值){
//我不确定我应该在这里做些什么来升级Cassandra数据库中的数据。
//给定一个用户ID,我需要将列值插入Cassandra数据库。
//columnNameAndValue是以列名为键,以相应列值为值的映射。
}
有人能帮我吗?我对这个Datastax API是完全陌生的,它使用新的二进制协议,所以在这方面有很多问题
谢谢您的帮助。在您的cassandra.yaml文件中,查找标签开始\u本机\u传输,默认情况下将其禁用,启用它
使用Datastax Java驱动程序与jdbc驱动程序非常相似
插入代码
String query = "insert into test(key,col1,col2) values('1','value1','value2')";
session.execute(query);
阅读卡桑德拉的作品
String query="select * from test;";
ResultSet result = session.execute(query);
for (Row rows: result){
System.out.println(rows.getString("key"));
}
您是否能够连接或显示任何错误?是的,我也无法使用Datastax Java驱动程序连接到Cassandra数据库。我也得到了与您得到的NoHostAvailableException
相同的异常。你能解决这个问题吗?哦……我也有同样的问题,然后在卡桑德拉论坛上发布了一个问题,是的,最终解决了这个问题。您使用的是哪个版本的cassandra??1.2.?我正在运行1.2.3。我还向您发送了一封电子邮件,试图通过该电子邮件建立连接并连接到群集。顺便问一下,您在cassandra.yaml文件中做了哪些更改?我还将进行更改并运行它,看看这是否会产生任何影响。我已经将其从false启用为true。不管怎样,我会努力使那件事成功的。您知道如何使用Datastax Java驱动程序插入Cassandra,然后检索它吗。?意思是我原来的问题。更改yaml文件后,是否重新启动服务器?尝试清理commitlog文件并启动服务器。回答最初的问题,当然会尝试提供您也能告诉我如何清理提交日志文件吗?这意味着我们通常应该以什么方式执行此操作?默认情况下,它存储在“\var\lib\cassandra\commitlog\”中。清除commitlog文件夹中的所有文件(使用rm-fr)Ok,这意味着将其删除。我在windows中工作。我也删除了它,然后重新启动了服务器,结果还是一样。我相信我试图联系的方式是不对的。您能否提供一个示例,让您尝试连接到Cassandra数据库?意思是连接代码。