如何让我的 Cassandra DB 将我的键和值表示为 UTF8



我是Cassandra的新手,一直在玩Hector API。正如您在下面的屏幕截图中看到的,我定义了一个列系列,当我使用 CQL 返回行时,它将键和值作为十六进制值返回,如果可能的话,我想将其作为 UTF8 值返回。另外,似乎我的列名称没有采用,而是使用"Column1"。我将在下面发布我的col家庭声明。

//Define ColumnFamily Def in Hector
            ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(keyspaceName,"DP_ColumnFamily1",ComparatorType.UTF8TYPE);      

            //Add the column family to actual Cassandra Instance
            cluster.addColumnFamily(cfDef,false);
            stringSerializer = StringSerializer.get();
             //The following example inserts a Column with the column name "Datapower_Device_Name" and the column value of "DPIPE0101" under the key "key1". 
            Mutator mutator = HFactory.createMutator(ksp, stringSerializer);
            mutator.insert("key1", "DP_ColumnFamily1", HFactory.createStringColumn("Datapower_Device_Name", "DPIPE0101"));

cqlsh:test3> select * from "DP_ColumnFamily1";
 key        | column1               | value
------------+-----------------------+----------------------
 0x6b657931 | Datapower_Device_Name | 0x445049504530313031
(1 rows)

这一行:

ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(keyspaceName,"DP_ColumnFamily1",ComparatorType.UTF8TYPE);

创建比较设置为 UTF8 的列族。 比较器定义列的数据类型,不说明值。 CQL 足够智能,可以使用此比较器在运行查询时将名称正确显示为字符串,但它根本没有关于键或值类型的信息。

更好的方法是使用新的本机驱动程序,它完全避免了 Thrift,并允许您通过 CQL 执行所有操作。 然后,你将能够在 CQL 中创建架构,如下所示:

CREATE TABLE Device (
  DeviceID varchar, 
  DeviceName varchar,
  PRIMARY KEY (DeviceID)
);

。然后像这样插入:

INSERT INTO Device (DeviceID, DeviceName) 
VALUES ('Some_ID', 'DPIPE0101');

然后,如果您在cqlsh中查询,它看起来像这样:

 deviceid | devicename
----------+------------
  Some_ID |  DPIPE0101

请记住,CQL 行与存储行不直接相关,因此不应真正混合使用节俭和基于 CQL 的操作。 例外情况是,使用 cassandra-cli 查看存储引擎对 CQL 执行的操作很有帮助。 在本例中,它看起来像这样:

RowKey: Some_ID
=> (name=, value=, timestamp=1382118992099000)
=> (name=devicename, value=445049504530313031, timestamp=1382118992099000)

最新更新