1. 程式人生 > >Trafodion 處理JSON半結構化資料

Trafodion 處理JSON半結構化資料

Trafodion不僅可以處理結構化資料,還可以處理半結構化資料及非結構化資料,對於半結構化資料和非結構化資料,主要用到TMUDF功能。
本文通過一個實際的案例詳解如何通過自定義一個TMUDF來解析JSON格式文字從而實現對半結構化資料的處理。

/* ===================================================================================
     *
     * This is a Table Mapping UDF for Trafodion
     *
     * Input table contains a single VARCHAR/STRING column that is a complete JSON record
     * UDF outputs values corresponding to JSON tags passed in
the calling statement * A variable number of tags can be passed in * * Tag names must be fully qualified, eg, 'Master.Employee.Middle Initial' for JSON * {"MASTER": {"EMPLOYEE": { "Middle Initial": "Q" } } } * * * To invoke this UDF: * * select * from udf(json_column(table( select * from <your json table> ), * '<tag>'
, '<tag>', ...) ); * * =================================================================================== */

2 編譯並生成JAR包json_columnizer.jar(注意:此處需要把javax.json-1.0.4.jar一起打包進去)

[root@cent-2 udr]# ll json_columnizer.jar
-rw-rw-r--. 1 centos centos 380574
Feb 7 11:47 json_columnizer.jar [root@cent-2 udr]# pwd /home/trafodion/esgynDB-2.2.0/udr

3 建立Library及Table_Mapping Function

create library trafodion.seabase.json_columnizer file '/home/trafodion/esgynDB-2.2.0/udr/json_columnizer.jar';

create table_mapping function trafodion.seabase.unjson_obj
(
)
EXTERNAL NAME 'json_columnizer'
LIBRARY trafodion.seabase.json_columnizer
LANGUAGE JAVA
NO SQL;

4 準備樣例JSON文字

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    },
    {
      "type": "mobile",
      "number": "123 456-7890"
    }
  ],
  "children": [],
  "spouse": null
}

5 建立測試表並插入JSON字串

SQL>create table json_test ( jsondata varchar(2000));
SQL>insert into json_test values('{"firstName":"John","lastName":"Smith","isAlive":true,"age":25,"address":{"streetAddress":"212ndStreet","city":"NewYork","state":"NY","postalCode":"10021-3100"},"phoneNumbers":[{"type":"home","number":"212555-1234"},{"type":"office","number":"646555-4567"},{"type":"mobile","number":"123456-7890"}],"children":[],"spouse":null}');

6 查詢解析後的JSON字串

SQL>select * from udf(trafodion.seabase.unjson_obj(table(select jsondata from trafodion.seabase.json_test),'firstName','lastName'));

FIRSTNAME                                                                                            LASTNAME                                                                                           
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
John                                                                                                 Smith                                                                                              

--- 1 row(s) selected.