繁体   English   中英

如何从本地系统加载json文件并分配给一些变量

[英]how to load json file from local system and assign to some variable

我想将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,但就我而言,我必须通过其余API正文部分传递jsondata。 所以我必须收集整个json数据,然后转储到cassandra列中

您的问题有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),您将面临读取性能问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM