使用u-sql查询名称和城市

时间:2018-11-11 11:24:50

标签: azure azure-sql-database azure-data-lake u-sql

我需要从嵌套的json下面过滤名称和城市。

[
    {
        "Employee": [
            {
                "name": "John",
                "Gender": "Male"
            }
        ],
        "Info": {
            "location": {
                "city": "Kuvala",
                "continent": "Asia",
                "country": "Singapore"
            }
        }
    },
    {
        "Employee": [
            {
                "name": "Mike",
                "Gender": "Male"
            }
        ],
        "Info": {
            "location": {
                "city": "Dhabi",
                "continent": "Asia",
                "country": "Dubai"
            }
        }
    }
]
  

我的代码

CREATE ASSEMBLY IF NOT EXISTS [Newtonsoft.Json] FROM @"/Assemblies/Newtonsoft.Json.dll";
CREATE ASSEMBLY IF NOT EXISTS [Microsoft.Analytics.Samples.Formats] FROM @"/Assemblies/Microsoft.Analytics.Samples.Formats.dll";

REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];

USING Microsoft.Analytics.Samples.Formats.Json;

DECLARE @InputFile string = @"/Source/example.blob";
DECLARE @output string = @"/Output/output.csv";

@json  =
    EXTRACT Employee String,
            Info String
    FROM @InputFile
    USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor("*");

@CreateJSONTuple =
    SELECT   JsonFunctions.JsonTuple(Employee) AS EmployeeData,
             JsonFunctions.JsonTuple(Info) AS InfoData
    FROM @json;

@result =
    SELECT EmployeeData["name"] AS Name,
           JsonFunctions.JsonTuple(InfoData, "location") AS LocationData
    FROM @CreateJSONTuple;

OUTPUT @result
TO @output
USING Outputters.Csv(outputHeader : true, quoting : true);
  

我遇到一个错误,说不能从'Microsoft.Analytics.Types.Sql.SqlArray'转换为'string',请帮忙。

     

请帮助我从中获取名称,城市。    这应该输出:    约翰·瓦拉拉    迈克·达比

1 个答案:

答案 0 :(得分:0)

我认为您收到此错误是因为 Employee 元素的内容定义为数组:

assignmentAudit

尝试将 Employee 元素更改为以下内容:

"Employee": [
        {
            "name": "Mike",
            "Gender": "Male"
        }
    ]

更新

似乎您可能需要对数组项执行 JsonTuple ,所以这可能起作用:

"Employee": {
            "name": "Mike",
            "Gender": "Male"
        }

要注意的一件事是,如果 Employee 数组可能包含多个项目,则可能需要使用Cross Apply Explode

更新2

不幸的是,这是我能想到的最后一件事。尝试更换:

@result =
SELECT JsonFunctions.JsonTuple(EmployeeData[0])["name"] AS Name,
       JsonFunctions.JsonTuple(InfoData, "location") AS LocationData
FROM @CreateJSONTuple;

与此:

@CreateJSONTuple =
SELECT   JsonFunctions.JsonTuple(Employee) AS EmployeeData,
         JsonFunctions.JsonTuple(Info) AS InfoData
FROM @json;

@result =
SELECT EmployeeData["name"] AS Name,
       JsonFunctions.JsonTuple(InfoData, "location") AS LocationData
FROM @CreateJSONTuple;

希望有帮助!