apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shubham-pathak22 <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: MLHR-1962 json parser enhancem...
Date Mon, 11 Jan 2016 12:06:50 GMT
Github user shubham-pathak22 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/163#discussion_r49314239
  
    --- Diff: contrib/src/main/java/com/datatorrent/contrib/parser/JsonParser.java ---
    @@ -0,0 +1,187 @@
    +/**
    + * 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.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
    + */
    +@InterfaceStability.Evolving
    +public class JsonParser extends Parser<String, 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(String tuple)
    +  {
    +    try {
    +      if (schema != null) {
    --- End diff --
    
    User may chose to not provide schema if he doesn't wish to perform any validations. In
such cases, "jsonSchema" field can be null and hence the check. 


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message