datafusion functions json
v0.43.0 2024-11-13
注意:這不是 Apache 軟體基金會的官方版本,請參閱 datafusion-contrib/datafusion-functions-json#5。
該套件提供了一組用於在 DataFusion 中查詢 JSON 字串的函數。這些函數被實作為可在 SQL 查詢中使用的標量函數。
要使用這些功能,您只需呼叫:
datafusion_functions_json :: register_all ( & mut ctx ) ? ;
在SessionContext
中註冊以下 JSON 函數。
-- Create a table with a JSON column stored as a string
CREATE TABLE test_table (id INT , json_col VARCHAR ) AS VALUES
( 1 , ' {} ' ),
( 2 , ' { "a": 1 } ' ),
( 3 , ' { "a": 2 } ' ),
( 4 , ' { "a": 1, "b": 2 } ' ),
( 5 , ' { "a": 1, "b": 2, "c": 3 } ' );
-- Check if each document contains the key 'b'
SELECT id, json_contains(json_col, ' b ' ) as json_contains FROM test_table;
-- Results in
-- +----+---------------+
-- | id | json_contains |
-- +----+---------------+
-- | 1 | false |
-- | 2 | false |
-- | 3 | false |
-- | 4 | true |
-- | 5 | true |
-- +----+---------------+
-- Get the value of the key 'a' from each document
SELECT id, json_col - > ' a ' as json_col_a FROM test_table
-- +----+------------+
-- | id | json_col_a |
-- +----+------------+
-- | 1 | {null=} |
-- | 2 | {int=1} |
-- | 3 | {int=2} |
-- | 4 | {int=1} |
-- | 5 | {int=1} |
-- +----+------------+
json_contains(json: str, *keys: str | int) -> bool
- 如果 JSON 字串具有特定鍵(用於?
運算子),則為 true json_get(json: str, *keys: str | int) -> JsonUnion
- 透過「路徑」從 JSON 字串取得值json_get_str(json: str, *keys: str | int) -> str
- 透過「路徑」從 JSON 字串取得字串值json_get_int(json: str, *keys: str | int) -> int
- 透過「路徑」從 JSON 字串取得整數值json_get_float(json: str, *keys: str | int) -> float
- 透過「路徑」從 JSON 字串取得浮點值json_get_bool(json: str, *keys: str | int) -> bool
- 透過「路徑」從 JSON 字串取得布林值json_get_json(json: str, *keys: str | int) -> str
- 透過「路徑」從 JSON 字串取得嵌套的原始 JSON 字串json_as_text(json: str, *keys: str | int) -> str
- 透過「路徑」從 JSON 字串取得任何值,表示為字串(用於->>
運算子) json_length(json: str, *keys: str | int) -> int
- 取得 JSON 字串或陣列的長度->
運算子 - json_get
的別名->>
運算子 - json_as_text
的別名?
運算子 - json_contains
的別名使用json_get
的轉換表達式被重寫為適當的方法,例如
select * from foo where json_get(attributes, ' bar ' )::string = ' ham '
將被重寫為:
select * from foo where json_get_str(attributes, ' bar ' ) = ' ham '
json_keys(json: str, *keys: str | int) -> list[str]
- 取得 JSON 字串的鍵json_is_obj(json: str, *keys: str | int) -> bool
- 如果 JSON 是物件則為 true json_is_array(json: str, *keys: str | int) -> bool
- 如果 JSON 是數組則是 true json_valid(json: str) -> bool
- 如果 JSON 有效則為 true