2017-10-14 69 views
0

我在BigQuery documentation中讀到它支持表達式語言的一個子集JsonPath。但是我找不到實際上支持的JsonPath的哪個部分是?例如,當我在控制檯中試用時,我似乎無法在BigQuery的JsonPath表達式中使用通配符或過濾器。BigQuery支持哪些JsonPath表達式?

  1. 是否可以在BigQuery中的JsonPath表達式中使用通配符和過濾器?
  2. 是否有參考文檔或其他文檔描述完整在BigQuery中的JsonPath支持(因爲我似乎無法找到它)?

回答

3

是否有可能在BigQuery中的JsonPath表達式中使用通配符和過濾器?

爲了克服BigQiery「限制」爲JsonPath,一個可以引入custom function爲例如下所示:
注:它使用jsonpath-0.8.0.js可以從https://code.google.com/archive/p/jsonpath/downloads下載並上傳到谷歌雲存儲 - GS://your_bucket/jsonpath-0.8.0.js

#standardSQL 
CREATE TEMPORARY FUNCTION CUSTOM_JSON_EXTRACT(json STRING, json_path STRING) 
RETURNS STRING 
LANGUAGE js AS """ 
    try { var parsed = JSON.parse(json); 
     return JSON.stringify(jsonPath(parsed, json_path)); 
    } catch (e) { return null } 
""" 
OPTIONS (
    library="gs://your_bucket/jsonpath-0.8.0.js" 
); 
WITH t AS (
SELECT ''' 
{ "store": { 
     "book": [ 
      { "category": "reference", 
       "author": "Nigel Rees", 
       "title": "Sayings of the Century", 
       "price": 8.95 
      }, 
      { "category": "fiction", 
       "author": "Evelyn Waugh", 
       "title": "Sword of Honour", 
       "price": 12.99 
      }, 
      { "category": "fiction", 
       "author": "Herman Melville", 
       "title": "Moby Dick", 
       "isbn": "0-553-21311-3", 
       "price": 8.99 
      }, 
      { "category": "fiction", 
       "author": "J. R. R. Tolkien", 
       "title": "The Lord of the Rings", 
       "isbn": "0-395-19395-8", 
       "price": 22.99 
      } 
     ], 
     "bicycle": { 
      "color": "red", 
      "price": 19.95 
     } 
    } 
} 
''' AS x 
) 
SELECT 
    CUSTOM_JSON_EXTRACT(x, '$.store.book[*].author'), 
    CUSTOM_JSON_EXTRACT(x, '$..*[?(@.price==22.99)].author'), 
    CUSTOM_JSON_EXTRACT(x, '$..author'), 
    CUSTOM_JSON_EXTRACT(x, '$.store.*'), 
    CUSTOM_JSON_EXTRACT(x, '$.store..price'), 
    CUSTOM_JSON_EXTRACT(x, '$..book[(@.length-1)]'), 
    CUSTOM_JSON_EXTRACT(x, '$..book[-1:]'), 
    CUSTOM_JSON_EXTRACT(x, '$..book[0,1]'), 
    CUSTOM_JSON_EXTRACT(x, '$..book[:2]'), 
    CUSTOM_JSON_EXTRACT(x, '$..book[?(@.isbn)]') 
FROM t 

結果如下

對於CUSTOM_JSON_EXTRACT(x, '$.store.book[*].author')

[ 
    "Nigel Rees" 
    "Evelyn Waugh" 
    "Herman Melville" 
    "J. R. R. Tolkien" 
] 

對於CUSTOM_JSON_EXTRACT(x, '$..*[?(@.price==22.99)].author')

[ 
    "J. R. R. Tolkien" 
] 

對於CUSTOM_JSON_EXTRACT(x, '$.store..price')

[ 
    8.95 
    12.99 
    8.99 
    22.99 
    19.95 
] 

等等...

正如你所看到的 - 現在你可以使用通配符和過濾器和所有爵士樂:o)

+0

真的令人印象深刻,我不知道你可以用BQ做到這一點。謝謝! – Johan

3

支持的元素位於鏈接到的部分的表格中。具體而言,它包括$,.[],其中後者可以是子操作符或下標(數組)操作符。如果沒有列出,則不支持。