如何从本地系统加载 JSON 文件并分配给某个变量

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

本文关键字:分配 变量 文件 JSON 加载 系统      更新时间:2023-09-26

我想将本地系统中的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");
}           
});

我在准备插入查询时做错了什么。

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 列中

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