return null; extractObject = extract(extractObject, pathExpr[i], i == pathExprStart && isRootArray);
mKeyGroup1Cache.put(path, mKeyGroup1); json = extract_json_withkey(json, mKeyGroup1); json = extract_json_withindex(json, indexList);
protected void runTest(String json, String path, String exp, UDFJson udf) { Text res = udf.evaluate(json, path); if (exp == null) { Assert.assertNull(res); } else { Assert.assertNotNull(res); Assert.assertEquals("get_json_object test", exp, res.toString()); } } }
@Test public void testNestedGetJsonObject() throws SerDeException, JSONException { Writable w = new Text( "{ \"kind\": \"youtube#videoListResponse\", \"etag\": \"\\\"79S54kzisD_9SOTfQLu_0TVQSpY/mYlS4-ghMGhc1wTFCwoQl3IYDZc\\\"\", \"pageInfo\": { \"pagehit\":{ \"kind\": \"youtube#video\" } ,\"totalResults\": 1, \"resultsPerPage\": 1 }, \"items\": [ { \"kind\": \"youtube#video\", \"etag\": \"\\\"79S54kzisD_9SOTfQLu_0TVQSpY/A4foLs-VO317Po_ulY6b5mSimZA\\\"\", \"id\": \"wHkPb68dxEw\", \"statistics\": { \"viewCount\": \"9211\", \"likeCount\": \"79\", \"dislikeCount\": \"11\", \"favoriteCount\": \"0\", \"commentCount\": \"29\" }, \"topicDetails\": { \"topicIds\": [ \"/m/02mjmr\" ], \"relevantTopicIds\": [ \"/m/0cnfvd\", \"/m/01jdpf\" ] } } ] }"); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JSONObject result = (JSONObject) instance.deserialize(w); Object res = soi.getStructFieldData(result, soi.getStructFieldRef("pageinfo")); StringObjectInspector loi = (StringObjectInspector) soi.getStructFieldRef("pageinfo") .getFieldObjectInspector(); UDFJson udfJson = new UDFJson(); Text output = udfJson.evaluate(loi.getPrimitiveJavaObject(res), "$.pagehit"); assertEquals("{\"kind\":\"youtube#video\"}", output.toString()); }
+ ",\"owner\":\"amy\",\"zip code\":\"94025\",\"fb:testid\":\"1234\"}"; UDFJson udf = new UDFJson();
@Test public void testGetJsonObject() throws SerDeException, JSONException { Writable w = new Text( "{ \"kind\": \"youtube#videoListResponse\", \"etag\": \"\\\"79S54kzisD_9SOTfQLu_0TVQSpY/mYlS4-ghMGhc1wTFCwoQl3IYDZc\\\"\", \"pageInfo\": { \"totalResults\": 1, \"resultsPerPage\": 1 }, \"items\": [ { \"kind\": \"youtube#video\", \"etag\": \"\\\"79S54kzisD_9SOTfQLu_0TVQSpY/A4foLs-VO317Po_ulY6b5mSimZA\\\"\", \"id\": \"wHkPb68dxEw\", \"statistics\": { \"viewCount\": \"9211\", \"likeCount\": \"79\", \"dislikeCount\": \"11\", \"favoriteCount\": \"0\", \"commentCount\": \"29\" }, \"topicDetails\": { \"topicIds\": [ \"/m/02mjmr\" ], \"relevantTopicIds\": [ \"/m/0cnfvd\", \"/m/01jdpf\" ] } } ] }"); JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); Object res = soi.getStructFieldData(result, soi.getStructFieldRef("pageinfo")); StringObjectInspector loi = (StringObjectInspector) soi.getStructFieldRef("pageinfo") .getFieldObjectInspector(); UDFJson udfJson = new UDFJson(); Text output = udfJson.evaluate(loi.getPrimitiveJavaObject(res), "$.totalresults"); assertEquals("1", output.toString()); }
@Test public void testRootArray() throws HiveException { UDFJson udf = new UDFJson(); runTest("[1,2,3]", "$[0]", "1", udf); runTest("[1,2,3]", "$.[0]", "1", udf); runTest("[1,2,3]", "$.[1]", "2", udf); runTest("[1,2,3]", "$[1]", "2", udf); runTest("[1,2,3]", "$[3]", null, udf); runTest("[1,2,3]", "$.[*]", "[1,2,3]", udf); runTest("[1,2,3]", "$[*]", "[1,2,3]", udf); runTest("[1,2,3]", "$", "[1,2,3]", udf); runTest("[{\"k1\":\"v1\"},{\"k2\":\"v2\"},{\"k3\":\"v3\"}]", "$[2]", "{\"k3\":\"v3\"}", udf); runTest("[{\"k1\":\"v1\"},{\"k2\":\"v2\"},{\"k3\":\"v3\"}]", "$[2].k3", "v3", udf); runTest("[{\"k1\":[{\"k11\":[1,2,3]}]}]", "$[0].k1[0].k11[1]", "2", udf); runTest("[{\"k1\":[{\"k11\":[1,2,3]}]}]", "$[0].k1[0].k11", "[1,2,3]", udf); runTest("[{\"k1\":[{\"k11\":[1,2,3]}]}]", "$[0].k1[0]", "{\"k11\":[1,2,3]}", udf); runTest("[{\"k1\":[{\"k11\":[1,2,3]}]}]", "$[0].k1", "[{\"k11\":[1,2,3]}]", udf); runTest("[{\"k1\":[{\"k11\":[1,2,3]}]}]", "$[0]", "{\"k1\":[{\"k11\":[1,2,3]}]}", udf); runTest("[[1,2,3],[4,5,6],[7,8,9]]", "$[1]", "[4,5,6]", udf); runTest("[[1,2,3],[4,5,6],[7,8,9]]", "$[1][0]", "4", udf); runTest("[\"a\",\"b\"]", "$[1]", "b", udf); runTest("[[\"a\",\"b\"]]", "$[0][1]", "b", udf); runTest("[1,2,3]", "[0]", null, udf); runTest("[1,2,3]", "$0", null, udf); runTest("[1,2,3]", "0", null, udf); runTest("[1,2,3]", "$.", null, udf); runTest("[1,2,3]", "$", "[1,2,3]", udf); runTest("{\"a\":4}", "$", "{\"a\":4}", udf); }
mKeyGroup1Cache.put(path, mKeyGroup1); json = extract_json_withkey(json, mKeyGroup1); json = extract_json_withindex(json, indexList);
return null; extractObject = extract(extractObject, pathExpr[i], i == pathExprStart && isRootArray);
@Test public void testStringWhenFieldIsNotInJson() throws SerDeException, JSONException { Writable w = new Text( "{ \"kind\": \"youtube#videoListResponse\", \"etag\": \"\\\"79S54kzisD_9SOTfQLu_0TVQSpY/mYlS4-ghMGhc1wTFCwoQl3IYDZc\\\"\", \"pageInfo\": { \"totalResults\": 1, \"resultsPerPage\": 1 }, \"items\": [ { \"kind\": \"youtube#video\", \"etag\": \"\\\"79S54kzisD_9SOTfQLu_0TVQSpY/A4foLs-VO317Po_ulY6b5mSimZA\\\"\", \"id\": \"wHkPb68dxEw\", \"statistics\": { \"viewCount\": \"9211\", \"likeCount\": \"79\", \"dislikeCount\": \"11\", \"favoriteCount\": \"0\", \"commentCount\": \"29\" }, \"topicDetails\": { \"topicIds\": [ \"/m/02mjmr\" ], \"relevantTopicIds\": [ \"/m/0cnfvd\", \"/m/01jdpf\" ] } } ] }"); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JSONObject result = (JSONObject) instance.deserialize(w); Object res = soi.getStructFieldData(result, soi.getStructFieldRef("pageinfo")); StringObjectInspector loi = (StringObjectInspector) soi.getStructFieldRef("pageinfo") .getFieldObjectInspector(); UDFJson udfJson = new UDFJson(); Text output = udfJson.evaluate(loi.getPrimitiveJavaObject(res), "$.test_field"); assertNull(output); }
mKeyGroup1Cache.put(path, mKeyGroup1); json = extract_json_withkey(json, mKeyGroup1); json = extract_json_withindex(json, indexList);
extractObject = extract(extractObject, pathExpr[i]);
@Test public void testStringWhenNotJson() throws SerDeException, JSONException { Writable w = new Text( "{ \"kind\": \"youtube#videoListResponse\", \"etag\": \"\\\"79S54kzisD_9SOTfQLu_0TVQSpY/mYlS4-ghMGhc1wTFCwoQl3IYDZc\\\"\", \"pageInfo\": \"page\", \"items\": [ { \"kind\": \"youtube#video\", \"etag\": \"\\\"79S54kzisD_9SOTfQLu_0TVQSpY/A4foLs-VO317Po_ulY6b5mSimZA\\\"\", \"id\": \"wHkPb68dxEw\", \"statistics\": { \"viewCount\": \"9211\", \"likeCount\": \"79\", \"dislikeCount\": \"11\", \"favoriteCount\": \"0\", \"commentCount\": \"29\" }, \"topicDetails\": { \"topicIds\": [ \"/m/02mjmr\" ], \"relevantTopicIds\": [ \"/m/0cnfvd\", \"/m/01jdpf\" ] } } ] }"); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JSONObject result = (JSONObject) instance.deserialize(w); Object res = soi.getStructFieldData(result, soi.getStructFieldRef("pageinfo")); StringObjectInspector loi = (StringObjectInspector) soi.getStructFieldRef("pageinfo") .getFieldObjectInspector(); UDFJson udfJson = new UDFJson(); Text output = udfJson.evaluate(loi.getPrimitiveJavaObject(res), "$.test_field"); assertNull(output); }
mKeyGroup1Cache.put(path, mKeyGroup1); json = extract_json_withkey(json, mKeyGroup1); json = extract_json_withindex(json, indexList);
return null; extractObject = extract(extractObject, pathExpr[i], i == pathExprStart && isRootArray);