简体   繁体   中英

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. 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.

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.

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. so i have to collect entire json data and then dump into cassandra columns

There are 2 possible answers to your question

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. Then you can simply insert those JSON content into Cassandra using INSERT statements like.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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