gpt4 book ai didi

java - 如何使用 Astyanax 客户端将 Cassandra 插入到复合列中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:00 24 4
gpt4 key购买 nike

我正在尝试使用 Astyanax 客户端插入 Cassandra 中的 Composite Columns。下面是我创建的列族,其中所有列都是 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 ColumnsattributesMap 是将包含我要插入的数据的 map ,key 将是 列名实际值 将是该列的复合值

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
}
}

下面是我使用 Astyanax 客户端建立 Cassandra Connection 的类(class)-

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 插入复合列的任何简单示例都将帮助我更好地理解。我找不到与此相关的任何示例。有人可以帮我解决这个问题吗?

最佳答案

我们只使用复合键完成了此操作,但我相信主体保持不变。 ColumnListMutation 类有一个 putColumn method它可以采用任意类型,它是序列化程序。这需要固定类型(不是任意 Map),尽管您可以创建一个采用 Map 的序列化程序(有一个 MapSerializer,但我没有使用它)。我们使用 AnnotatedCompositeSerializer而不是从头开始编写序列化程序。所以代码应该是这样的(我没有测试过这个,但它应该足以让你开始):

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
}
}

关于java - 如何使用 Astyanax 客户端将 Cassandra 插入到复合列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18884582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com