2016-11-23 22 views
0

我有一列文本類型應包含JSON值。如何解析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...)

卡桑德拉版本2.1.13

+0

向我們展示您的表架構 –

+0

在Cassandra 2.2中添加了JSON查詢支持。在運行2.1.13時,必須升級才能訪問此功能:http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-2-json-support – Aaron

回答

1

不能在卡桑德拉使用SELECT JSON v2.1.x CQL爲V3.2.x

對於卡桑德拉v2.1.x CQL爲V3.2.x:SELECT後

唯一支持的操作是:

  • DISTINCT
  • COUNT(*)
  • COUNT(1)
  • 列名AS NEW_NAME
  • WRITETIME(列)
  • TTL(列)
  • dateOf(),現在(),minTimeuuid(),maxTimeuuid (),unixTimestampOf(),typeAsBlob()和blobAsType()

在卡桑德拉v2.2.x CQL v3.3.x介紹:SELECT JSON

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.

+0

值得注意的是,「**你不能這樣做**」在Cassandra 2.2之前。 – Aaron

+1

更新了答案謝謝@Aaron –