如何从hector(cassandra)添加特定验证类的数据

时间:2012-11-01 17:06:45

标签: cassandra

我正在服用一些babysteps进入cassandra,我想要一些建议。

我想将数据插入到一个列族中,我已经创建了一个具有以下凭据的列族 -

  ColumnFamily: testkp
  Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type
  Default column value validator: org.apache.cassandra.db.marshal.UTF8Type
  Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
  GC grace seconds: 864000
  Compaction min/max thresholds: 4/32
  Read repair chance: 0.1
  DC Local Read repair chance: 0.0
  Replicate on write: true
  Caching: KEYS_ONLY
  Bloom Filter FP chance: default
  Built indexes: []
  Column Metadata:
    Column Name: Col_A
      Validation Class: org.apache.cassandra.db.marshal.FloatType
    Column Name: Col_B
      Validation Class: org.apache.cassandra.db.marshal.LongType
    Column Name: Col_C
      Validation Class: org.apache.cassandra.db.marshal.Int32Type
    Column Name: Col_D
      Validation Class: org.apache.cassandra.db.marshal.DateType
    Column Name: Col_E
      Validation Class: org.apache.cassandra.db.marshal.UTF8Type
  Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy
  Compression Options:
    chunk_length_kb: 64
    sstable_compression: org.apache.cassandra.io.compress.SnappyCompressor

现在我看到了一些基本的插入机制,我试过,如果我没有预定义我的架构,它基本上有效

public class SampleDataInsert {

private static StringSerializer stringSerializer = StringSerializer.get();

public static void main(String[] args) throws Exception {
    Cluster cluster = HFactory.getOrCreateCluster("TestCluster","localhost:9160");

    Keyspace keyspaceOperator = HFactory.createKeyspace("test",cluster);

    try {
        Mutator<String> mutator =   HFactory.createMutator(keyspaceOperator,stringSerializer);

        for (int i = 0; i < 2; i++) {
            mutator.addInsertion("key_" + i, "testkp", HFactory.createStringColumn("Col_A", ""+ i));
            mutator.addInsertion("key_" + i, "testkp", HFactory.createStringColumn("Col_B", ""+ i));
            mutator.addInsertion("key_" + i, "testkp", HFactory.createStringColumn("Col_C", ""+ i));
            mutator.addInsertion("key_" + i, "testkp", HFactory.createStringColumn("Col_D", ""+ i));
            mutator.addInsertion("key_" + i, "testkp", HFactory.createStringColumn("Col_E", ""+ i));
}
mutator.execute();
}

现在,因为我已经预定义了架构,所以如果我尝试使用除UTF8Type之外的密钥验证类添加任何列。它基本上会弹出一个错误(这很好)。

我的问题是如何在hector中定义其他验证类(FloatType,LongType,DateType)..以便我可以执行插入。

任何链接,建议以查找此信息..

P.S我尝试了几天,但我找不到太多信息。如果我的问题有点低级/或格式不正确,请道歉。

最好的问候

1 个答案:

答案 0 :(得分:1)

mutator.addInsertion(key, cf, HFactory.createColumn(longName, floatValue, LongSerializer.get(), FloatSerializer.get()))