insert JSON String in Apache-Cassandra 0.8.2
Anybody know which is the easy way to insert a json string in cassandra. Suppose I have a json string like this: {'key1':'val1','key2开发者_开发百科':'val2'}
In MongoDB we can insert directly a json string like dbobj.insert(jsonstring); So is there any way to do like this in Cassandra?(I am coding in java)
There are at least 3 ways, but it depends what you are trying to achieve and what kinds of query you want to run.
You could store the JSON string as just a plain string/byte, as a Cassandra column name (assuming there is something you can use as the row key). You won't be able to do queries based on the JSON content, though; this would be opaque data that you process client-side.
You could split up the JSON before storage, so that key1, key2 are column names and val1, val2 are the corresponding column values. Again, you'd need something to use as a row key. This method would let you retrieve individual values, and use secondary indexes to retrieve rows with particular values.
You could even use key1, key2 as row keys, with val1, val2 as column names. Given that you have the key-val pairs grouped in JSON, they presumably belong to the same entity and are related, so this is unlikely to be useful, but I mention it for completeness.
Edited to add: If your question is actually how to insert data into Cassandra at all, then you should read the docs for a Java client such as Hector (there are other options too - see http://wiki.apache.org/cassandra/ClientOptions)
you can try this: INSERT INTO users JSON '{"id": "user123", "age": 42, "state": "TX"}';
Reference: http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-2-json-support
精彩评论