1

我有一个包含 json 块列表的文件,并且卡在 U-Sql 中处理/读取它们并写入文本文件。

{
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batters":
        {
            "batter":
                [
                    { "id": "1001", "type": "Regular" },
                    { "id": "1002", "type": "Chocolate" },
                    { "id": "1003", "type": "Blueberry" },
                    { "id": "1004", "type": "Devil's Food" }
                ]
        },
    "topping":
        [
            { "id": "5001", "type": "None" },
            { "id": "5002", "type": "Glazed" },
            { "id": "5005", "type": "Sugar" },
            { "id": "5007", "type": "Powdered Sugar" },
            { "id": "5006", "type": "Chocolate with Sprinkles" },
            { "id": "5003", "type": "Chocolate" },
            { "id": "5004", "type": "Maple" }
        ]
}
{
    "id": "0002",
    "type": "nut",
    "name": "ake",
    "ppu": 1.55,
    "batters":
        {
            "batter":
                [
                    { "id": "1001", "type": "Regular" },
                    { "id": "1002", "type": "Chocolate" },
                    { "id": "1003", "type": "Blueberry" },
                    { "id": "1004", "type": "Devil's Food" }
                ]
        },
    "topping":
        [
            { "id": "5001", "type": "None" },
            { "id": "5002", "type": "Glazed" },
            { "id": "5005", "type": "Sugar" },
            { "id": "5007", "type": "Powdered Sugar" },
            { "id": "5006", "type": "Chocolate with Sprinkles" },
            { "id": "5003", "type": "Chocolate" },
            { "id": "5004", "type": "Maple" }
        ]
}

{
    "id": "0003",
    "type": "test",
    "name": "ake",
    "ppu": 1.55,
    "batters":
        {
            "batter":
                [

                ]
        },
    "topping":
        [

            { "id": "5003", "type": "Chocolate" },
            { "id": "5004", "type": "Maple" }
        ]
}

有人可以帮我解决这个问题。

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

DECLARE @Full_Path string = @"C:\Users\test\Desktop\File\JsonTest.json";

USING [Microsoft.Analytics.Samples.Formats];

@RawExtract = 
    EXTRACT 
        [RawString] string

    FROM
        @Full_Path
    USING 
        Extractors.Text(delimiter:'\n', quoting : false);

@ParsedJSONLines =
    SELECT JsonFunctions.JsonTuple([RawString]) AS JSONLine

    FROM @RawExtract;


@StagedData =
    SELECT 
        JSONLine["id"] AS Id,
        JSONLine["name"] AS Name,
        JSONLine["type"] AS Type,
        JSONLine["ppu"] AS PPU,
JSONLine["batters"] AS Batter
    FROM 
        @ParsedJSONLines;

DECLARE @Output_Path string = @"C:\Users\Test\Desktop\File\Test2.csv";

OUTPUT @StagedData
TO @Output_Path 
USING Outputters.Csv();

评估表达式时收到错误。

Error while evaluating expression JsonFunctions.JsonTuple(RawString)
4

1 回答 1

0

除非您使用 Json Lines,否则您不能使用 Text Extraxtor 提取 Json。

使用提取器将拆分 json,您将收到错误消息。

使用 JsonExtractor 而不是文本提取器。

https://github.com/Azure/usql/blob/master/Examples/DataFormats/Microsoft.Analytics.Samples.Formats/Json/JsonExtractor.cs

于 2019-01-10T16:17:12.567 回答