我正在尝试使用Composite Columns
客户端插入Cassandra中的Astyanax
。下面是我创建的列系列,其中所有列都是Composite Columns
。我不确定如何插入下面的列族,每列都是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}];
以下是我创建的一个简单示例,该示例将插入Cassandra
但不适用于Composite Columns
。 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);
以下是我的DAOImpl code
upsertAttributes method
,其中/**
* 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
}
}
仅用于插入Cassandra的单列值。有什么办法,我可以修改相同的东西,开始使用Astyanax?
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;
}
}
以下是我使用Astyanax客户端制作Cassandra Connection的课程 -
{{1}}
使用Astyanax插入复合列的任何简单示例都可以帮助我更好地理解。我无法找到与此相关的任何示例。有人可以帮我吗?
答案 0 :(得分:2)
我们只使用复合键完成此操作,但我相信校长保持不变。 ColumnListMutation类有一个putColumn method,它可以采用任意类型和它的序列化程序。这需要一个固定的类型(不是任意的Map),尽管你可以创建一个带有Map的序列化器(有一个MapSerializer但我还没有使用它)。我们使用AnnotatedCompositeSerializer而不是从头开始编写Serializer。所以代码就是这样的(我没有测试过这个,但它应该足以让你开始):
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
}
}