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 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 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()); }
@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); }
@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); }