Java 如何使用Astyanax客户端在Cassandra中插入复合列?
我正在尝试使用Astyanax客户机在Cassandra中插入组合列。下面是我创建的列族,其中所有列都是Java 如何使用Astyanax客户端在Cassandra中插入复合列?,java,cassandra,astyanax,column-family,Java,Cassandra,Astyanax,Column Family,我正在尝试使用Astyanax客户机在Cassandra中插入组合列。下面是我创建的列族,其中所有列都是复合列。我不知道如何插入到下面的列族中,因为每个列都是一个组合列 create column family USER_DATA with key_validation_class = 'UTF8Type' and comparator = 'CompositeType(UTF8Type,UTF8Type,DateType)' and default_validation_class = 'UT
复合列
。我不知道如何插入到下面的列族中,因为每个列都是一个组合列
create column family USER_DATA
with key_validation_class = 'UTF8Type'
and comparator = 'CompositeType(UTF8Type,UTF8Type,DateType)'
and default_validation_class = 'UTF8Type'
and gc_grace = 86400
and column_metadata = [ {column_name : 'lmd', validation_class : DateType}];
下面是我创建的一个简单示例,它将插入到Cassandra
中,但不用于复合列attributesMap
是包含我试图插入的数据的映射,键
将是列名
,而实际值
将是该列的复合值
String s1 = "Hello";
String S2 = "World";
long s3 = System.currentTimeMillis();
// insert the above three values into a composite column
clientDao.upsertAttributes("123", attributesMap, columnFamily);
下面是我的DAOImpl代码
,它有upsertAttributes方法
,我用它只为单列值插入Cassandra。有没有办法,我可以修改它,开始使用Astyanax使用复合列行为
/**
* Performs an upsert of the specified attributes for the specified id.
*/
public void upsertAttributes(final String rowKey, final Map<String, String> attributes, final String columnFamily) {
try {
MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();
ColumnListMutation<String> mutation = m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(), rowKey);
for (Map.Entry<String, String> entry : attributes.entrySet()) {
mutation = mutation.putColumn(entry.getKey(), entry.getValue(), null);
}
mutation.putColumn("lmd", System.currentTimeMillis());
m.setConsistencyLevel(ConsistencyLevel.CL_ONE).execute();
} catch (ConnectionException e) {
// log here
} catch (Exception e) {
// log here
}
}
/**
*为指定id执行指定属性的upsert。
*/
公共void upsertAttributes(最终字符串行键、最终贴图属性、最终字符串列族){
试一试{
MutationBatch m=CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();
ColumnListMutation=m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(),rowKey);
for(Map.Entry:attributes.entrySet()){
突变=突变.putColumn(entry.getKey(),entry.getValue(),null);
}
mutation.putColumn(“lmd”,System.currentTimeMillis());
m、 setconsistentylevel(consistentylevel.CL_ONE).execute();
}捕获(连接异常e){
//在这里登录
}捕获(例外e){
//在这里登录
}
}
下面是我使用Astyanax客户端进行Cassandra连接的类-
public class CassandraAstyanaxConnection {
private AstyanaxContext<Keyspace> context;
private Keyspace keyspace;
private ColumnFamily<String, String> emp_cf;
private static class ConnectionHolder {
static final CassandraAstyanaxConnection connection = new CassandraAstyanaxConnection();
}
public static CassandraAstyanaxConnection getInstance() {
return ConnectionHolder.connection;
}
/**
* Creating Cassandra connection using Astyanax client
*
*/
private CassandraAstyanaxConnection() {
context = new AstyanaxContext.Builder()
.forCluster(Constants.CLUSTER)
.forKeyspace(Constants.KEYSPACE)
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
.setPort(9160)
.setMaxConnsPerHost(1000)
.setSeeds("host1:portnumber")
)
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setCqlVersion("3.0.0")
.setTargetCassandraVersion("1.2")
.setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN)
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE))
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
context.start();
keyspace = context.getEntity();
emp_cf = ColumnFamily.newColumnFamily(Constants.COLUMN_FAMILY, StringSerializer.get(), StringSerializer.get());
}
/**
* returns the keyspace
*
* @return
*/
public Keyspace getKeyspace() {
return keyspace;
}
public ColumnFamily<String, String> getEmp_cf() {
return emp_cf;
}
}
公共类CassandraAstyanaxConnection{
私有上下文上下文;
私有密钥空间密钥空间;
私人家庭emp_cf;
私有静态类连接持有者{
静态最终CassandraAstyanaxConnection连接=新CassandraAstyanaxConnection();
}
公共静态CassandraAstyanaxConnection getInstance(){
返回ConnectionHolder.connection;
}
/**
*使用Astyanax客户端创建Cassandra连接
*
*/
私人CassandraAstyanaxConnection(){
context=new AstyanaxContext.Builder()
.forCluster(常数.CLUSTER)
.forKeyspace(常数.KEYSPACE)
.withConnectionPoolConfiguration(新的ConnectionPoolConfigurationMPL(“MyConnectionPool”)
.设置端口(9160)
.setMaxConnsPerHost(1000)
.setSeeds(“主机1:端口号”)
)
.withAstyanaxConfiguration(新的AstyanaxConfiguration-Impl())
.setCqlVersion(“3.0.0”)
.setTargetCassandraVersion(“1.2”)
.setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN)
.setDiscoveryType(NodeDiscoveryType.RING_描述))
.withConnectionPoolMonitor(新计数ConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
context.start();
keyspace=context.getEntity();
emp_cf=ColumnFamily.newColumnFamily(Constants.COLUMN_FAMILY,StringSerializer.get(),StringSerializer.get());
}
/**
*返回键空间
*
*@返回
*/
公共密钥空间getKeyspace(){
返回键空间;
}
公共专栏系列getEmp_cf(){
返回emp_cf;
}
}
任何使用Astyanax插入复合柱的简单示例都将帮助我更好地理解。我找不到任何与此相关的例子。有人能帮我吗?我们只使用组合键,但我相信原理是一样的。ColumnListMutation类有一个可以采用任意类型的序列化程序。这需要一个固定类型(不是任意映射),尽管您可以创建一个接受映射的序列化程序(有一个,但我没有使用它)。我们使用而不是从头开始编写序列化程序。所以代码应该是这样的(我还没有测试过,但应该足以让您开始):
公共静态类ComplexType{
@组件(序号=0)
字符串val1;
@组件(序号=1)
字符串val2;
@组件(序号=2)
int时间戳;
//获取/设置遗漏的方法。。。
}
私有静态最终AnnotateCompositeSerializer complexTypeSerializer=新的AnnotatedCompositeSerializer(ComplexType.class);
公共void upsertAttributes(最终字符串行键、最终贴图属性、最终字符串列族){
试一试{
MutationBatch m=CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();
ComplexType ct=createComplexTypeFromAttributes(属性);
ColumnListMutation=m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(),rowKey);
突变
.putColumn(“lmd”,ct,complexTypeSerializer,null);
m、 setconsistentylevel(consistentylevel.CL_ONE).execute();
}捕获(连接异常e){
//在这里登录
}捕获(例外e){
//在这里登录
}
}
public static class ComplexType {
@Component(ordinal = 0)
String val1;
@Component(ordinal = 1)
String val2;
@Component(ordinal = 2)
int timestamp;
// get/set methods left out...
}
private static final AnnotateCompositeSerializer<ComplexType> complexTypeSerializer = new AnnotatedCompositeSerializer<>(ComplexType.class);
public void upsertAttributes(final String rowKey, final Map<String, String> attributes, final String columnFamily) {
try {
MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();
ComplexType ct = createComplexTypeFromAttributes(attributes);
ColumnListMutation<String> mutation = m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(), rowKey);
mutation
.putColumn("lmd", ct, complexTypeSerializer, null);
m.setConsistencyLevel(ConsistencyLevel.CL_ONE).execute();
} catch (ConnectionException e) {
// log here
} catch (Exception e) {
// log here
}
}