Flatten complex nested JSON with Pig and output in | delimited file

838 views Asked by At

I need to take some complex nested JSON and transform it into tab delimited output where I have a unique output line for each ts and y pair from the input JSON. I know how to output with tab delimited format but having trouble getting the JSON flattened in the right way. Any suggestions based on the input JSON below and desired output? I am using ElephantBird to load the JSON.

I have the following input JSON:

{
    “gateway": [
        {"beer" : [
                {"change_date": "change_date"},
                {"type": "squirrel-pale-ale"},
                {"vendor": "foo-vendor"},
                {"size": "size"}
            ] 
        },
        {"name": "SBS01"},
        {"hw_version": "1.1"}
    ],
    "sensors": [
        [
            {"info": {
                "name": "fake-sensor01",
                "serial_number": “fakies40911",
                "type": "temperature"
                }
            },
            {"values": [
                    {"ts": 1400869261, "y": 998}, // "ts" is UNIX Epoch in UTC
                    {"ts": 1400869276, "y": 1002}
                ]
            }
        ],
        [
            {"info": {
                "name": "fake-sensor02",
                "serial_number": “fakies40944",
                "type": "flow"
                }
            },
            {"values": [
                    {"ts": 1400869294, "y": 54},
                    {"ts": 1400869303, "y": 76}
                ]
            }
        ]
    ]
}

I am able to load it using this pig script:

register 's3://path-to-scripts/elephant-bird-core-4.5.jar';
register 's3://path-to-scripts/elephant-bird-hadoop-compat-4.5.jar';
register 's3://path-to-scripts/elephant-bird-pig-4.5.jar';
register 's3://path-to-scripts/json-simple-1.1.1.jar';

data = load 's3://path-to-data/example_record.json' using com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad') AS (json:map[]);

Now I want to get tuples that are flattened for each ts and y pair while retaining attributes other attributes. I have tried various sequence of statements generate with flatten and referencing kv pairs from the map but struggling. Looking for suggestions on how to get this result:

(SBS01, 1.1, change_date, squirrel-pale-ale, foo-vendor, size, fake-sensor01, fakies40911, temperature, 1400869261, 998)
(SBS01, 1.1, change_date, squirrel-pale-ale, foo-vendor, size, fake-sensor01, fakies40911, temperature, 1400869276, 1002)
(SBS01, 1.1, change_date, squirrel-pale-ale, foo-vendor, size, fake-sensor02, fakies40944, flow, 1400869294, 54)
(SBS01, 1.1, change_date, squirrel-pale-ale, foo-vendor, size, fake-sensor02, fakies40944, flow, 1400869303, 76)
0

There are 0 answers