简体   繁体   English

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

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

I want to store json file from local system to cassandra column, as newer version of cassandra supports json data. 我想将json文件从本地系统存储到cassandra列,因为cassandra的较新版本支持json数据。 for solution, I am looking for approach something like this:- 对于解决方案,我正在寻找类似这样的方法:

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.

I don't know whether it is possible or not.your's suggestions are highly appreciated. 我不知道是否可能。您的建议受到高度赞赏。 i am using cassandra2.2.4 on ubuntu 14.04 lts system. 我在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");
}           
});

I did something wrong while preparing insertQuery. 在准备insertQuery时发生了错误。

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

Response : “data insertion failed”

 application:  rest API creation

I am not passing json data directly to cassandra, but in my case I have to pass jsondata through rest API body part. 我没有将json数据直接传递到cassandra,但就我而言,我必须通过其余API正文部分传递jsondata。 so i have to collect entire json data and then dump into cassandra columns 所以我必须收集整个json数据,然后转储到cassandra列中

There are 2 possible answers to your question 您的问题有2个可能的答案

1) If your JSON files have a clear structure , you can design a Cassandra table that matches this structure, using User Defined Types if necessary to encode nested structure. 1)如果您的JSON文件具有清晰的结构 ,则可以设计与该结构匹配的Cassandra表,并在必要时使用用户定义的类型来编码嵌套结构。 Then you can simply insert those JSON content into Cassandra using INSERT statements like. 然后,您可以使用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) If your JSON files do not have any schema , in this case just store them as a blob of text 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"}');

A warning though, avoid to store huge blob of JSON data (like a payload worth of 10Mb), you'll face read performance issues. 但是请注意,避免存储大量JSON数据(例如有效负载为10Mb),您将面临读取性能问题。

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

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