在 Pig 中解析复杂的 JSON 字符串 [英] Parse Complex JSON String in Pig

查看:25
本文介绍了在 Pig 中解析复杂的 JSON 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Pig 中解析一串复杂的 JSON.具体来说,我希望 Pig 将我的 JSON 数组理解为包而不是单个字符数组.使用 JsonLoader 时,我可以通过指定架构轻松完成此操作,如这个问题.有什么方法可以让 Pig 为我找出我的模式,或者在 Pig 解析字符串时指定它?我一直在使用 JsonStringToMap,但找不到指定架构的方法,或者无法正确理解我的 JSON 数组是数组而不是单个字符数组.

I want to parse a string of complex JSON in Pig. Specifically, I want Pig to understand my JSON array as a bag instead of as a single chararray. When using JsonLoader, I can do this easily by specifying the schema, as in this question. Is there any way to either have Pig figure out my schema for me, or to specify it when Pig is parsing a string? I've been using JsonStringToMap, but can't find a way to specify Schema, or to have it properly understand my JSON array is an array and not a single chararray.

推荐答案

我最终使用了 JsonTupleMap()Mozilla 的用于猪的 Akela 库.它通过解析我的所有 JSON 来完成我想要的,即使它很复杂,即使我不提供模式也这样做.如果你遇到和我一样的问题,就用那个.

I wound up using JsonTupleMap() in Mozilla's Akela library for pig. It accomplishes exactly what I want by parsing all of my JSON even when it's complex, and doing this even when I don't provide a schema. If you run into the same problem as me, use that.

示例用法:

REGISTER '/path/to/akela-0.5-SNAPSHOT.jar';
DEFINE JsonTupleMap com.mozilla.pig.eval.json.JsonTupleMap();
loaded = LOAD '$INPUT' AS (json_string:chararray, ...);
jsonified = FOREACH loaded GENERATE JsonTupleMap(json_string) AS json:map[], ...;
some_generate = FOREACH jsonified GENERATE json#'key'#'sub_key';

这篇关于在 Pig 中解析复杂的 JSON 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆