【问题标题】:USQL call to read json array without a keyUSQL 调用无键读取 json 数组
【发布时间】:2018-08-21 12:40:22
【问题描述】:

我有一个嵌套的 json,我试图在 usql 中将其展平。我无法共享数据,但结构与此类似。

{
    "userlist": [user1, user1],
    "objects": {
        "largeobjects": [object1, object2, object3]
        "smallobjects": [s_object1, s_object2]
    },
    "applications": [{
            "application": sdq3ds5dsa
        }, {
            "application": dksah122j4
        }, {
            "application": sadsw2dq2s
        }, {
            "application": pro3dfdsn3
        }
    ],
    "date" : 12344232,
    "timezone" : "Asia",
    "id" : "sad2ddssa2",
    "admin": {
        "lang": "eng",
        "country": "us",
    }
}

我正在使用自定义 jsonoutputter (https://github.com/Azure/usql/tree/master/Examples/DataFormats/Microsoft.Analytics.Samples.Formats) 从 json 文件中提取,并使用 jsontuple 函数来提取值。我的问题是该函数使用生成键值对的 sql map。这适用于我有键的情况,但是当我尝试使用该函数从无键数组中获取值时会引发错误。

任何有关如何解决此问题的建议将不胜感激。

编辑 这是我正在关注的输出:

sad2ddssa2, object1, 12344232, "Asia", "eng", "us",
sad2ddssa2, object2, 12344232, "Asia", "eng", "us"

【问题讨论】:

  • 请发布一些有效的 JSON,即使它是虚构的。您的预期结果会是什么样子?
  • @wBob,我已经编辑了这个问题,希望这能提供更多信息

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


【解决方案1】:

第一选择

尝试在你的 u-sql 中使用 PROSE。使用 PROSE 的 c# nuget 处理数据并进行复杂的提取。这是一个非常强大的 AI 包。在此处查看视频和示例:https://microsoft.github.io/prose

第二个选项

创建一个 c# 函数来处理您的 json。像这样的东西,使用 c# json api 将此示例适应您的自定义提取请求:

/* Formats the array of values into a named json array. */

DECLARE @JsonArray Func<SqlArray<string>, string, string> = (data, name) => 
    {
        StringBuilder buffer = new StringBuilder();

        buffer.Append("{\r\n\t\"" + name + "\": [\r\n");

        for (int i = 0; i < data.Count(); i++)
        {
            if (i > 0)
            {
                buffer.Append(",\r\n");
            }

            buffer.Append("\t\"" + data[i] + "\"");
        }

        buffer.Append("\r\n\t]\r\n}");

        return buffer.ToString();
    };

/* Format the array containing groups of comma separated values into a named json array */

@Query = 
    SELECT
        @JsonArray(SubscriptionArray, "subscriptionList") AS JsonArray
    FROM @subscriptionsQuery1;

第三个选项

在适应您的需求后尝试这种方法:

/* For each json line create a json map (SqlMap) */

@subscriptionsQuery1 = 
    SELECT 
           JsonFunctions.JsonTuple(JsonLine) AS JsonMap
    FROM @SubscriptionsExtractor AS t;

/* For each json map get the required property value */

@subscriptionsQuery1 = 
    SELECT DISTINCT
           JsonMap["alias"] AS Subscription
    FROM @subscriptionsQuery1 AS t;

/* Join the value of all rows into a single row containing an array of all values */

@subscriptionsQuery1 = 
    SELECT
        ARRAY_AGG<string>(Subscription) AS SubscriptionArray
    FROM @subscriptionsQuery1 AS t;

【讨论】:

  • 谢谢,PROSE 对于我以后的一些任务看起来很有趣。第三种方法是我尝试做的,但 json 元组需要一个 sql.map,而我的数组只有值没有键,并产生一个不兼容的 sql.array,因此我的问题。我最终编写了进行扁平化的自定义代码。我确信这不是最优雅的解决方案,也许我本可以弄清楚如何使用现有功能,但它确实有效。不过我会接受这个答案,因为 PROSE 将来会证明对我有用。
  • 你也可以试试第二种方法。使用 Json.NET api 解析你的 json 字符串,你不需要对文件进行平面化。
  • 有趣的方法@MiguelDomingues +10。
【解决方案2】:

我能够使用 NewtonSoft MultiLevelJsonExtractor 提取器和this fixed-up JSON file:

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

USING Microsoft.Analytics.Samples.Formats.Json;

DECLARE @inputFile string = @"\input\yourInputJSON.json";
DECLARE @outputFile string = @"\output\output.csv";


@input =
    EXTRACT id string,
            largeobjects string,
            date string,
            timezone string,
            lang string,
            country string
    FROM @inputFile
    USING new MultiLevelJsonExtractor("objects", false,
          "id",
          "largeobjects",
          "date",
          "timezone",
          "admin.lang",
          "admin.country"
          );

// Convert the JSON column to SQL MAP to multiple rows
@working =
    SELECT id,
           JsonFunctions.JsonTuple(largeobjects).Values AS largeobject,
           date,
           timezone,
           lang,
           country
    FROM @input;


// Explode the JSON SQL MAP
@output =
    SELECT id,
           x.y AS largeobject,
           date,
           timezone,
           lang,
           country
    FROM @working
         CROSS APPLY
             EXPLODE(largeobject) AS x(y);


OUTPUT @output
TO @outputFile
USING Outputters.Csv(quoting : false);

我的结果:

我会说这可能比使用自己动手的方法更安全,因为 NewtonSoft 库专门用于处理 JSON,并且经过了尝试和测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2020-05-22
    相关资源
    最近更新 更多