[英]How to insert in Cassandra into Composite Columns using Astyanax client?
I am trying to insert into Composite Columns
in Cassandra using Astyanax
client. 我正在尝试使用Astyanax
客户端插入Cassandra中的Composite Columns
。 Below is the column family I have created in which all the columns are Composite Columns
. 下面是我创建的列系列,其中所有列都是Composite Columns
。 I am not sure how should I insert into below Column Family with each columns is a Composite Column
. 我不确定如何插入Column Family下面,每列都是一个Composite Column
。
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}];
Below is a simple example which I have created that will insert into Cassandra
but its not for Composite Columns
. 下面是我创建的一个简单示例,它将插入到Cassandra
但不适用于Composite Columns
。 attributesMap
is the map which will have the data that I am trying to insert, key
will be column name
and the actual value
will be composite value
of that column. attributesMap
是具有我尝试插入的数据的映射, key
将是column name
, actual value
将是该列的composite value
。
String s1 = "Hello";
String S2 = "World";
long s3 = System.currentTimeMillis();
// insert the above three values into a composite column
clientDao.upsertAttributes("123", attributesMap, columnFamily);
And Below is my DAOImpl code
which has upsertAttributes method
which I was using to insert into Cassandra only for single column value. 下面是我的DAOImpl code
,它有upsertAttributes method
,我用它来插入Cassandra只用于单列值。 Is there any way, I can modify the same to start using Composite Column behavior using Astyanax? 有什么办法,我可以修改同样的方法来开始使用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
}
}
Below is the class where I am making Cassandra Connection using Astyanax client- 以下是我使用Astyanax客户端制作Cassandra Connection的课程 -
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;
}
}
Any simple example to insert into Composite Columns using Astyanax will help me understand better. 使用Astyanax插入复合列的任何简单示例都将帮助我更好地理解。 I am not able to find any example related to that. 我无法找到与此相关的任何示例。 Can anybody help me with this? 任何人都可以帮我吗?
We have only done this with Composite keys, but I believe the principal remains the same. 我们只用复合键完成了这个,但我相信校长仍然是一样的。 The ColumnListMutation class has a putColumn method which can take an arbitrary type and it's serializer. ColumnListMutation类有一个putColumn方法 ,它可以采用任意类型和它的序列化器。 This requires a fixed type (not an arbitrary Map) although you could create a serializer that took a Map (there is a MapSerializer but I have not used it). 这需要一个固定的类型(不是任意的Map),尽管你可以创建一个带有Map的序列化器(有一个MapSerializer但我还没有使用它)。 We use the AnnotatedCompositeSerializer instead of writing a Serializer from scratch. 我们使用AnnotatedCompositeSerializer而不是从头开始编写Serializer。 So the code would be something like this (I have not tested this, but it should be enough to get you started): 所以代码将是这样的(我没有测试过这个,但它应该足以让你开始):
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.