【问题标题】:how to load json file from local system and assign to some variable如何从本地系统加载 json 文件并分配给某个变量
【发布时间】:2016-04-21 14:55:08
【问题描述】:

我想将 json 文件从本地系统存储到 cassandra 列,因为新版本的 cassandra 支持 json 数据。对于解决方案,我正在寻找这样的方法:-

globalvariable<-load json file from local system.
cassandracolumn<-dump(globalvariable)
third thing i want to know the type of cassandracolumn, so that is support,   
entire json data, whether it is text or blob.

我不知道这是否可能。非常感谢您的建议。我在 ubuntu 14.04 lts 系统上使用 cassandra2.2.4。

I tried so far:
1.var data = params.jsonData; //assign entire json data to variable
2. var insertQuery = "INSERT INTO " +keyspace+ "."+table_name+ " JSON        
'{"+data+"}';";
console.log(insertQuery);
client.execute(insertQuery, function(err, result2){
if(result2){
return res.json("data inserted");
}
else{
return res.json("data insertion failed");
}           
});

我在准备 insertQuery 时做错了。

On console log :  INSERT INTO jsondb.iris JSON '{[object Object]}';

Response : “data insertion failed”

 application:  rest API creation

我没有将 json 数据直接传递给 cassandra,但在我的情况下,我必须通过 rest API 正文部分传递 jsondata。所以我必须收集整个 json 数据,然后转储到 cassandra 列中

【问题讨论】:

  • 看起来jsonData 是一个对象而不是字符串,INSERT INTO table JSON ? 需要一个字符串。如果您有一个具有类型值的对象,您可以编写完整的查询:INSERT INTO table (col1, col2, col3, ...) VALUES (?, ?, ?, ...) 并使用jsonData 的属性作为参数。
  • JSON.parse 会,我认为是正确的包。但无法通过 npm registery 安装。通过 JSON.parse(整个 json 对象),我们可以分配给一些变量..然后将该变量转储到 cassandra 列中......任何人都可以建议我在我的节点项目中的 ubuntu 14.04 lts 中安装 JSON.parse 的命令。

标签: javascript node.js cassandra


【解决方案1】:

您的问题有 2 个可能的答案

1) 如果您的 JSON 文件具有清晰的结构,您可以设计一个与该结构匹配的 Cassandra 表,如果需要,可以使用用户定义的类型对嵌套结构进行编码。然后,您可以使用 INSERT 语句将这些 JSON 内容简单地插入 Cassandra。

CREATE TABLE example (
    id int PRIMARY KEY,
    tupleval tuple<int, text>,
    numbers set<int>,
    words list<text>
)

INSERT INTO example JSON '{"id": 0, "tupleval": [1, "abc"], "numbers": [1, 2, 3], "letters": ["a", "b", "c"]}';

2) 如果您的 JSON 文件没有任何 架构,在这种情况下,只需将它们存储为文本块

CREATE TABME my_json(
   id int,
   json text,
   PRIMARY KEY(id)
);

INSERT INTO my_json(id,json) VALUES(1, '{"1": "foo", "2": "bar"}');

但请注意,避免存储大量 JSON 数据(例如价值 10Mb 的有效负载),您将面临读取性能问题。

【讨论】:

  • @donaduyhai,我没有将 json 数据直接传递给 cassandra,但在我的情况下,我必须通过 rest API 正文部分传递 jsondata。所以我必须收集整个 json 数据,然后转储到 cassandra 列中。
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多