Github user tushargosavi commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/163#discussion_r51699399
--- Diff:
contrib/src/main/java/com/datatorrent/contrib/parser/JsonParser.java ---
@@ -0,0 +1,241 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.datatorrent.contrib.parser;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.github.fge.jsonschema.exceptions.ProcessingException;
+import com.github.fge.jsonschema.main.JsonSchema;
+import com.github.fge.jsonschema.main.JsonSchemaFactory;
+import com.github.fge.jsonschema.report.ProcessingMessage;
+import com.github.fge.jsonschema.report.ProcessingReport;
+import com.github.fge.jsonschema.util.JsonLoader;
+import com.google.common.annotations.VisibleForTesting;
+
+import com.datatorrent.api.AutoMetric;
+import com.datatorrent.api.Context.OperatorContext;
+import com.datatorrent.api.DefaultOutputPort;
+import com.datatorrent.lib.parser.Parser;
+import com.datatorrent.lib.util.KeyValPair;
+import com.datatorrent.netlet.util.DTThrowable;
+
+/**
+ * Operator that parses a json string tuple against a specified json
schema and
+ * emits JSONObject on one port POJO on other port and tuples that could
not be
+ * parsed on error port.<br>
+ * Schema is specified in a json format as per http://json-schema.org/ <br>
+ * Example for the schema can be seen here
http://json-schema.org/example1.html <br>
+ * User can choose to skip validations by not specifying the schema at
all. <br>
+ * <br>
+ * <b>Properties</b> <br>
+ * <b>jsonSchema</b>:schema as a string<br>
+ * <b>clazz</b>:Pojo class <br>
+ * <b>Ports</b> <br>
+ * <b>in</b>:input tuple as a String. Each tuple represents a json
string<br>
+ * <b>parsedOutput</b>:tuples that are validated against the schema are
emitted
+ * as JSONObject on this port<br>
+ * <b>out</b>:tuples that are validated against the schema are emitted as
pojo
+ * on this port<br>
+ * <b>err</b>:tuples that do not confine to schema are emitted on this
port as
+ * KeyValPair<String,String><br>
+ * Key being the tuple and Val being the reason.
+ *
+ *
+ * @displayName JsonParser
+ * @category Parsers
+ * @tags json pojo parser
+ * @since 3.2.0
+ */
[email protected]
+public class JsonParser extends Parser<byte[], KeyValPair<String, String>>
+{
+
+ /**
+ * Contents of the schema.Schema is specified as per
http://json-schema.org/
+ */
+ private String jsonSchema;
+ private transient JsonSchema schema;
+ private transient ObjectMapper objMapper;
+ /**
+ * output port to emit validate records as JSONObject
+ */
+ public transient DefaultOutputPort<JSONObject> parsedOutput = new
DefaultOutputPort<JSONObject>();
+ /**
+ * metric to keep count of number of tuples emitted on {@link
#parsedOutput}
+ * port
+ */
+ @AutoMetric
+ long parsedOutputCount;
+
+ @Override
+ public void beginWindow(long windowId)
+ {
+ super.beginWindow(windowId);
+ parsedOutputCount = 0;
+ }
+
+ @Override
+ public void setup(OperatorContext context)
+ {
+ try {
+ if (jsonSchema != null) {
+ JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
+ JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
+ schema = factory.getJsonSchema(schemaNode);
+ }
+ objMapper = new ObjectMapper();
+ } catch (ProcessingException | IOException e) {
+ DTThrowable.wrapIfChecked(e);
+ }
+ }
+
+ @Override
+ public void processTuple(byte[] tuple)
+ {
+ if (tuple == null) {
+ if (err.isConnected()) {
+ err.emit(new KeyValPair<String, String>(null, "null tuple"));
+ }
+ errorTupleCount++;
+ return;
+ }
+ String incomingString = new String(tuple);
+ try {
+ if (schema != null) {
--- End diff --
validation is only required if error port is connected.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---