3

我正在尝试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插入 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 连接的课程-

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

4

1 回答 1

2

我们只用复合键做到了这一点,但我相信主体保持不变。ColumnListMutation 类有一个putColumn 方法,它可以采用任意类型,它是序列化程序。这需要一个固定类型(不是任意 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
}
}
于 2013-09-19T14:25:52.740 回答