CQL3 每行都有自己的架构 [英] CQL3 Each row to have its own schema
问题描述
我想在 .Net 应用程序中使用 Cassandra.我的目标是将一些数据存储在一个列族中,但每一行数据都有不同的架构.
I want to use Cassandra in a .Net application. My objective is to store some data in a column family, but each row of data will have varying schema.
示例(一个非常简单的示例)我想要一个玩具"列族来存储以下对象,(注意它们除了 ID 属性之外还有非常不同的属性)
Example (A very simple one) I want to have a 'Toys' column family to store the following objects, (Notice how they have very different properties other than the ID property)
玩具对象 1{id":1","name":"汽车","number_of_doors":4,喜欢":3}
Toy object 1 { "id":"1", "name":"Car", "number_of_doors":4, "likes":3}
玩具对象 2{id":2","type":"飞机","flying_range":"100m"}
Toy object 2 { "id":"2", "type":"Plane", "flying_range":"100m"}
玩具对象 3{id":3","类别":"火车","number_of_carriages":10}
Toy object 3 { "id":"3", "category":"Train", "number_of_carriages":10}
从我对 Datastax CSharp 驱动程序的初步理解和使用来看,我必须始终更改不适合我的表(列族).我希望每一行都有自己的架构.Thrift API 或许可以解决这个问题,但 HectorSharp 似乎已经死了.
From my initial understanding and using of Datastax CSharp driver I have to always alter the table (Column family) which does not sit right with me. I would like each row to have its own schema. Thrift API might be able to solve this but it seems HectorSharp is all but dead.
与我的要求类似的问题,但没有我想要的答案
A question similar to my requirement but it doesn't have the answer I want
Cassandra对于无模式数据库,每天有数百万个订单表和数百万次查询
我是否期望每一行都有自己的架构,或者有没有办法使用 Cassandra+Csharp 来做到这一点?
Am I barking up the wrong tree by expecting each row to have its own schema or is there a way to do this using Cassandra+Csharp ?
预先感谢您的回答.
推荐答案
Cassandra 的旧版本是无模式的,这意味着您没有任何地方可以定义行可以包含的内容.您现在需要的可以部分在 Cassandra 2.1
Older versions of Cassandra were Schema-less, meaning that you didn't have anywhere a definition of what a row could contain. What you need now could be partially done with a Map
on Cassandra 2.1
CREATE TABLE toys (
id text PRIMARY KEY,
toy map<text, text>
)
放一些数据...
INSERT INTO toys (id, toy) VALUES ( '1', {'name':'Car', 'number_of_doors':'4', 'likes':'3'});
INSERT INTO toys (id, toy) VALUES ( '2', {'type':'Plane', 'flying_range':'100m'});
INSERT INTO toys (id, toy) VALUES ( '3', {'category':'Train', 'number_of_carriages':'10'});
表格内容...
id | toy
----+-------------------------------------------------------
3 | {'category': 'Train', 'number_of_carriages': '10'}
2 | {'flying_range': '100m', 'type': 'Plane'}
1 | {'likes': '3', 'name': 'Car', 'number_of_doors': '4'}
我们现在可以在键上创建索引...
We can now create an index on keys ...
CREATE INDEX toy_idx ON toys (KEYS(toy));
... 并对 Map 键执行查询...
... and perform queries on Map keys ...
SELECT * FROM toys WHERE toy CONTAINS KEY 'name';
id | toy
----+-------------------------------------------------------
1 | {'likes': '3', 'name': 'Car', 'number_of_doors': '4'}
现在您可以像处理普通列一样更新或删除地图条目,而无需在写入前阅读
Now you can update or delete map entries like you would do with normal columns, without reading before writing
DELETE toy['name'] FROM toys WHERE id='1';
UPDATE toys set toy = toy + {'name': 'anewcar'} WHERE id = '1';
SELECT * FROM toys;
id | toy
----+-----------------------------------------------------------
3 | {'category': 'Train', 'number_of_carriages': '10'}
2 | {'flying_range': '100m', 'type': 'Plane'}
1 | {'likes': '3', 'name': 'anewcar', 'number_of_doors': '4'}
一些限制
- 您无法检索集合的一部分:即使在内部将地图的每个条目存储为一列,您也只能检索整个集合
- 您必须选择是在键上还是在值上同时创建索引不支持.
- 因为地图是输入的,所以你不能输入混合值——在我的例子中,所有整数现在都是字符串
我个人认为这种方法的广泛使用是一种反模式.
I personally consider an extensive usage of this approach an anti-pattern.
HTH,卡罗
这篇关于CQL3 每行都有自己的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!