2017-04-03 27 views
-1

我有一个复杂的JSON我想推入Cassandra,我将有问题和选项mapping.how我应该在Cassandra中设计我的表吗?我的表架构应该是什么?我有一个复杂的json我想推入cassandra

insert into questions_options JSON'{ 
    "qid": "12736467", 
    "chapter_id": "12", 
    "subject_id": 19482065, 
    "topic_id": 216.28, 
    "type": "picked", 
    "content": "on time", 
    "difficulty": 1, 
    "marks": 12, 
    "hint": "do something", 
    "explanations": "do something dude", 
    "created_by": 1232, 
    "create_on": "2013-06-10", 
    "options": [ 
    { 
     "option_id": 1, 
     "option_text": "ACBD", 
     "sort_order": 1, 
     "correct_option": true 
    }, 
    { 
     "option_id": 2, 
     "option_text": "bcdd", 
     "sort_order": 2, 
     "correct_option": true 
    } 
    ] 
}' 

我写了下面的创建表query.Is是正确的吗?

create table questions_options (
    qid bigint, 
    chapter_id bigint, 
    subject_id int, 
    topic_id int, 
    class varchar, 
    type varchar, 
    content text, 
    difficulty tinyint, 
    marks int, 
    hint text, 
    explanations text, 
    created_by bigint, 
    create_on timestamp, 
    option_id bigint, 
    option_text text, 
    sort_order int, 
    correct_option boolean, 
    PRIMARY KEY((qid), option_id) 
); 
+0

在卡桑德拉你要设计你的表要查询的方式。 –

+0

可以请你给我创建表查询?正如在上面的JSON中,它是一对多的映射,我想把它放在一个表中。 – sam

回答

0

在卡桑德拉你要设计你的表要查询的,而不是你要插入的一个方式。

顺便问一下,你问什么是支持你的json的表。

下面是如何,你可以转换:

  • 对于每个JSON键创建卡桑德拉列具有相同名称
  • 定义由JSON字段值字段的数据类型。
  • 如果该值也是json对象,则在cassandra中创建自定义数据类型
  • 定义主键通过分析您的读取查询。

为了您的JSON,这将是该模式:

CREATE TYPE option (
    option_id bigint, 
    option_text text, 
    sort_order int, 
    correct_option boolean 
); 

CREATE TABLE questions_options (
    qid bigint PRIMARY KEY, 
    chapter_id bigint, 
    class text, 
    content text, 
    create_on date, 
    created_by bigint, 
    difficulty int, 
    explanations text, 
    hint text, 
    marks int, 
    options list<frozen<option>>, 
    subject_id int, 
    topic_id double, 
    type text 
);