如何解析cassandra中文本列的JSON值
How to parse JSON value of a text column in cassandra
我有一列文本类型包含 JSON 值。
{
"customer": [
{
"details": {
"customer1": {
"name": "john",
"addresses": {
"address1": {
"line1": "xyz",
"line2": "pqr"
},
"address2": {
"line1": "abc",
"line2": "efg"
}
}
}
"customer2": {
"name": "robin",
"addresses": {
"address1": null
}
}
}
}
]
}
如何使用查询提取列的 'address1' JSON 字段?
首先我尝试获取 JSON 值,然后我将继续解析。
SELECT JSON customer from text_column;
我的查询出现以下错误。
com.datastax.driver.core.exceptions.SyntaxError: line 1:12 no viable
alternative at input 'customer' (SELECT [JSON] customer...)
com.datastax.driver.core.exceptions.SyntaxError: line 1:12 no viable
alternative at input 'customer' (SELECT [JSON] customer...)
Cassandra 版本 2.1.13
您不能在 Cassandra v2.1.x CQL v3.2.x[=11 中使用 SELECT JSON =]
对于 Cassandra v2.1.x CQL v3.2.x:
SELECT 之后唯一支持的操作是:
- 不同
- 计数 (*)
- 计数 (1)
- column_name AS new_name
- 写入时间 (column_name)
- TTL (column_name)
- dateOf()、now()、minTimeuuid()、maxTimeuuid()、unixTimestampOf()、typeAsBlob() 和 blobAsType()
在Cassandra v2.2.x CQL v3.3.x中引入:SELECTJSON
With SELECT statements, the new JSON keyword can be used to return each row as a single JSON encoded map. The remainder of the SELECT statment behavior is the same.
The result map keys are the same as the column names in a normal result set. For example, a statement like “SELECT JSON a, ttl(b) FROM ...” would result in a map with keys "a" and "ttl(b)". However, this is one notable exception: for symmetry with INSERT JSON behavior, case-sensitive column names with upper-case letters will be surrounded with double quotes. For example, “SELECT JSON myColumn FROM ...” would result in a map key "\"myColumn\"" (note the escaped quotes).
The map values will JSON-encoded representations (as described below) of the result set values.
如果您的 Cassandra 版本是 2.1x 及以下,您可以使用基于 Python 的方法。
使用 Cassandra-Python API
编写 python 脚本
这里你必须先获取你的行,然后使用python json的loads方法,这会将你的json文本列值转换为JSON对象这将是 Python 中的字典。然后您可以使用 Python 字典并提取所需的嵌套键。请参阅下面的代码片段。
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
import json
if __name__ == '__main__':
auth_provider = PlainTextAuthProvider(username='xxxx', password='xxxx')
cluster = Cluster(['0.0.0.0'],
port=9042, auth_provider=auth_provider)
session = cluster.connect("keyspace_name")
print("session created successfully")
rows = session.execute('select * from user limit 10')
for user_row in rows:
customer_dict = json.loads(user_row.customer)
print(customer_dict().keys()
我有一列文本类型包含 JSON 值。
{
"customer": [
{
"details": {
"customer1": {
"name": "john",
"addresses": {
"address1": {
"line1": "xyz",
"line2": "pqr"
},
"address2": {
"line1": "abc",
"line2": "efg"
}
}
}
"customer2": {
"name": "robin",
"addresses": {
"address1": null
}
}
}
}
]
}
如何使用查询提取列的 'address1' JSON 字段?
首先我尝试获取 JSON 值,然后我将继续解析。
SELECT JSON customer from text_column;
我的查询出现以下错误。
com.datastax.driver.core.exceptions.SyntaxError: line 1:12 no viable alternative at input 'customer' (SELECT [JSON] customer...)
com.datastax.driver.core.exceptions.SyntaxError: line 1:12 no viable alternative at input 'customer' (SELECT [JSON] customer...)
Cassandra 版本 2.1.13
您不能在 Cassandra v2.1.x CQL v3.2.x[=11 中使用 SELECT JSON =]
对于 Cassandra v2.1.x CQL v3.2.x:
SELECT 之后唯一支持的操作是:
- 不同
- 计数 (*)
- 计数 (1)
- column_name AS new_name
- 写入时间 (column_name)
- TTL (column_name)
- dateOf()、now()、minTimeuuid()、maxTimeuuid()、unixTimestampOf()、typeAsBlob() 和 blobAsType()
在Cassandra v2.2.x CQL v3.3.x中引入:SELECTJSON
With SELECT statements, the new JSON keyword can be used to return each row as a single JSON encoded map. The remainder of the SELECT statment behavior is the same.
The result map keys are the same as the column names in a normal result set. For example, a statement like “SELECT JSON a, ttl(b) FROM ...” would result in a map with keys "a" and "ttl(b)". However, this is one notable exception: for symmetry with INSERT JSON behavior, case-sensitive column names with upper-case letters will be surrounded with double quotes. For example, “SELECT JSON myColumn FROM ...” would result in a map key "\"myColumn\"" (note the escaped quotes).
The map values will JSON-encoded representations (as described below) of the result set values.
如果您的 Cassandra 版本是 2.1x 及以下,您可以使用基于 Python 的方法。 使用 Cassandra-Python API
编写 python 脚本这里你必须先获取你的行,然后使用python json的loads方法,这会将你的json文本列值转换为JSON对象这将是 Python 中的字典。然后您可以使用 Python 字典并提取所需的嵌套键。请参阅下面的代码片段。
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
import json
if __name__ == '__main__':
auth_provider = PlainTextAuthProvider(username='xxxx', password='xxxx')
cluster = Cluster(['0.0.0.0'],
port=9042, auth_provider=auth_provider)
session = cluster.connect("keyspace_name")
print("session created successfully")
rows = session.execute('select * from user limit 10')
for user_row in rows:
customer_dict = json.loads(user_row.customer)
print(customer_dict().keys()