簡體   English   中英

使用Astyanax插入到Composite Columns時,InvalidRequestException

[英]InvalidRequestException while inserting into Composite Columns using Astyanax

我打算使用Astyanax客戶端插入Cassandra列系列的Composite Column中。 每列將是一個復合列,這意味着其值將由我的ComplexType類中所示的三個值組成。 以下是我在Cassandra中的專欄文章。

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;

我希望插入后看起來像這樣。 (讓我知道上面的列系列是否不適用於下面的用例。)

user-id   column1
123      (Column1-Value  Column1-SchemaName  LastModifiedDate)

以下是我的Java主要代碼-

public static void main(String[] args) {

    ComplexType ct = new ComplexType();
    ct.setVal1("Hello");
    ct.setVal2("World");
    ct.setTimestamp(System.currentTimeMillis());

    // e1 is the column-name and ct is its composite-value.
    attributesMap.put("e1", ct);

    clientDao.upsertCompositeAttributes("123", attributesMap, "USER_DATA");
}

下面是我的ComplexType類-

public static class ComplexType {
    @Component(ordinal = 0)
    String val1;

    @Component(ordinal = 1)
    String val2;

    @Component(ordinal = 2)
    long timestamp;


    public String getVal1() {
        return val1;
    }
    public void setVal1(String val1) {
        this.val1 = val1;
    }
    public String getVal2() {
        return val2;
    }
    public void setVal2(String val2) {
        this.val2 = val2;
    }
    public long getTimestamp() {
        return timestamp;
    }
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

下面是我的DAOImpl class upsertCompositeAttributes方法。

public void upsertCompositeAttributes(final String rowKey, final Map<String, ComplexType> ct, final String columnFamilyName) {

    try {
        AnnotatedCompositeSerializer<ComplexType> complexTypeSerializer = new AnnotatedCompositeSerializer<ComplexType>(ComplexType.class);

        ColumnFamily columnFamily = new ColumnFamily(columnFamilyName, StringSerializer.get(), StringSerializer.get());

        MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();

        ColumnListMutation<String> mutation = m.withRow(columnFamily, rowKey);

        for (Map.Entry<String, ComplexType> entry : ct.entrySet()) {
          // entry.getKey() is the column name and entry.getValue() is its composite value.
            mutation = mutation.putColumn(entry.getKey(), entry.getValue(), complexTypeSerializer, null);
        }

        m.setConsistencyLevel(ConsistencyLevel.CL_ONE).execute();

    } catch (ConnectionException e) {

    } catch (Exception e) {

    }
}

以下是例外,我每次都從upsertCompositeAttributes方法獲取信息。

com.netflix.astyanax.connectionpool.exceptions.BadRequestException: BadRequestException: [host=10.109.107.27(10.109.107.27):9160, latency=99(131), attempts=1]InvalidRequestException(why:Not enough bytes to read value of component 0)
    at com.netflix.astyanax.thrift.ThriftConverter.ToConnectionPoolException(ThriftConverter.java:159)
    at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:65)
    at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:28)
    at com.netflix.astyanax.thrift.ThriftSyncConnectionFactoryImpl$ThriftConnection.execute(ThriftSyncConnectionFactoryImpl.java:151)
    at com.netflix.astyanax.connectionpool.impl.AbstractExecuteWithFailoverImpl.tryOperation(AbstractExecuteWithFailoverImpl.java:69)
    at com.netflix.astyanax.connectionpool.impl.AbstractHostPartitionConnectionPool.executeWithFailover(AbstractHostPartitionConnectionPool.java:256)
    at com.netflix.astyanax.thrift.ThriftKeyspaceImpl.executeOperation(ThriftKeyspaceImpl.java:485)
    at com.netflix.astyanax.thrift.ThriftKeyspaceImpl.access$000(ThriftKeyspaceImpl.java:79)
    at com.netflix.astyanax.thrift.ThriftKeyspaceImpl$1.execute(ThriftKeyspaceImpl.java:123)
    at com.cassandra.astyanax.CassandraAstyanaxClient.upsertCompositeAttributes(CassandraAstyanaxClient.java:167)
    at com.example.AstyanaxCompositeColumns.main(AstyanaxCompositeColumns.java:24)
Caused by: InvalidRequestException(why:Not enough bytes to read value of component 0)
    at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:20833)
    at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
    at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:964)
    at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:950)
    at com.netflix.astyanax.thrift.ThriftKeyspaceImpl$1$1.internalExecute(ThriftKeyspaceImpl.java:129)
    at com.netflix.astyanax.thrift.ThriftKeyspaceImpl$1$1.internalExecute(ThriftKeyspaceImpl.java:126)
    at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:60)
    ... 9 more

下面是我的CassandraAstyanaxConnection類-

private CassandraAstyanaxConnection() {

    context = new AstyanaxContext.Builder()
    .forCluster(Constants.CLUSTER)
    .forKeyspace(Constants.KEYSPACE)
    .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
        .setPort(9160)
        .setMaxConnsPerHost(1000)
        .setSeeds("host:9160")
    )
    .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());
}

我正在運行Cassandra 1.2.9和Astyanax 1.56.37

模式定義中的比較器元素描述列名,而不是列值。

對於您的模式,Cassandra希望您為它提供一個包含3個組件的復雜鍵,但會得到一個簡單的字符串,並抱怨說:

沒有足夠的字節來讀取組件0的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM