摘要: 本文爲您介紹如何利用DataWorks數據集成將JSON數據從OSS遷移到MaxCompute,並使用MaxCompute內置字符串函數GET_JSON_OBJECT提取JSON信息。html
本文爲您介紹如何利用DataWorks數據集成將JSON數據從OSS遷移到MaxCompute,並使用MaxCompute內置字符串函數GET_JSON_OBJECT提取JSON信息。app
將您的JSON文件重命名後綴爲TXT文件,並上傳到OSS。本文中使用的JSON文件示例以下。函數
{ "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": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 }
將applog.txt文件上傳到OSS,本文中OSS Bucket位於華東2區。 測試
進入DataWorks數據集成控制檯,新增OSS類型數據源。
spa
具體參數以下所示,測試數據源連通性經過便可點擊完成。Endpoint地址請參見OSS各區域的外網、內網地址,本例中爲http://oss-cn-shanghai.aliyuncs.com或 http://oss-cn-shanghai-internal.aliyuncs.com(因爲本文中OSS和DataWorks項目處於同一個region中,本文選用後者,經過內網鏈接)。
3d
"fileFormat":"binary"
。該步驟能夠保證OSS中的JSON文件同步到MaxCompute以後存在同一行數據中,即爲一個字段。其餘參數保持不變,腳本模式代碼示例以下。 { "type": "job", "steps": [ { "stepType": "oss", "parameter": { "fieldDelimiterOrigin": "^", "nullFormat": "", "compress": "", "datasource": "OSS_userlog", "column": [ { "name": 0, "type": "string", "index": 0 } ], "skipHeader": "false", "encoding": "UTF-8", "fieldDelimiter": "^", "fileFormat": "binary", "object": [ "applog.txt" ] }, "name": "Reader", "category": "reader" }, { "stepType": "odps", "parameter": { "partition": "", "isCompress": false, "truncate": true, "datasource": "odps_first", "column": [ "mqdata" ], "emptyAsNull": false, "table": "mqdata" }, "name": "Writer", "category": "writer" } ], "version": "2.0", "order": { "hops": [ { "from": "Reader", "to": "Writer" } ] }, "setting": { "errorLimit": { "record": "" }, "speed": { "concurrent": 2, "throttle": false, "dmu": 1 } } }完成上述配置後,點擊運行接便可。運行成功日誌示例以下所示。
在您的業務流程中新建一個ODPS SQL節點。 日誌
您能夠首先輸入 SELECT*from mqdata;
語句,查看當前mqdata表中數據。固然這一步及後續步驟,您也能夠直接在MaxCompute客戶端中輸入命令運行。 code
確認導入表中的數據結果無誤後,您能夠使用MaxCompute內建字符串函數GET_JSON_OBJECT獲取您想要的JSON數據。本例中使用 SELECT GET_JSON_OBJECT(mqdata.MQdata,'$.expensive') FROM mqdata;
獲取JSON文件中的 expensive值。以下圖所示,能夠看到已成功獲取數據。 orm
原文連接htm