apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tushargosavi <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: MLHR-1838 added parser and for...
Date Fri, 09 Oct 2015 06:01:37 GMT
Github user tushargosavi commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/22#discussion_r41599903
  
    --- Diff: contrib/src/main/java/com/datatorrent/contrib/schema/formatter/CsvFormatter.java
---
    @@ -0,0 +1,276 @@
    +/**
    + * 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.schema.formatter;
    +
    +import java.io.IOException;
    +import java.io.StringWriter;
    +import java.util.ArrayList;
    +
    +import javax.validation.constraints.NotNull;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.supercsv.cellprocessor.FmtDate;
    +import org.supercsv.cellprocessor.Optional;
    +import org.supercsv.cellprocessor.ift.CellProcessor;
    +import org.supercsv.exception.SuperCsvException;
    +import org.supercsv.io.CsvBeanWriter;
    +import org.supercsv.io.ICsvBeanWriter;
    +import org.supercsv.prefs.CsvPreference;
    +
    +import org.apache.hadoop.classification.InterfaceStability;
    +
    +import com.datatorrent.api.Context;
    +import com.datatorrent.netlet.util.DTThrowable;
    +
    +/**
    + * Operator that converts POJO to CSV string
    + * <br>Assumption is that each field in the delimited data should map to a simple
java type.<br>
    + * <br><b>Properties</b>
    + * <br><b>fieldInfo</b>:User need to specify fields and their types
as a comma separated string having format
    + *  &lt;NAME&gt;:&lt;TYPE&gt;|&lt;FORMAT&gt; in the same order
as incoming data.
    + *  FORMAT refers to dates with dd/mm/yyyy as default
    + *  e.g name:string,dept:string,eid:integer,dateOfJoining:date|dd/mm/yyyy
    + * @displayName CsvFormatter
    + * @category Formatter 
    + * @tags pojo csv formatter
    + */
    +@InterfaceStability.Evolving
    +public class CsvFormatter extends Formatter<String>
    +{
    +
    +  private ArrayList<Field> fields;
    +  @NotNull
    +  protected String classname;
    +  @NotNull
    +  protected int fieldDelimiter;
    +  protected String lineDelimiter;
    +  
    +  @NotNull
    +  protected String fieldInfo;
    +  
    +  public enum FIELD_TYPE
    +  {
    +    BOOLEAN, DOUBLE, INTEGER, FLOAT, LONG, SHORT, CHARACTER, STRING, DATE
    +  };
    +  
    +  protected transient String[] nameMapping;
    +  protected transient CellProcessor[] processors;
    +  protected transient CsvPreference preference;
    +  
    +  public CsvFormatter()
    +  {
    +    fields = new ArrayList<Field>();
    +    fieldDelimiter = ',';
    +    lineDelimiter = "\r\n";
    +    
    +  }
    +  
    +  @Override public void setup(Context.OperatorContext context)
    +  {
    +    super.setup(context);
    +
    +    //fieldInfo information
    +    fields = new ArrayList<Field>();
    +    String[] fieldInfoTuple = fieldInfo.split(",");
    +    for ( int i=0 ; i < fieldInfoTuple.length ; i++ ) {
    +      String[] fieldTuple = fieldInfoTuple[i].split(":");
    +      Field field = new Field();
    +      field.setName(fieldTuple[0]);
    +      String[] typeFormat = fieldTuple[1].split("\\|");
    +      field.setType(typeFormat[0].toUpperCase());
    +      if ( typeFormat.length > 1 ) {
    +        field.setFormat(typeFormat[1]);
    +      }
    +      getFields().add(field);
    +    }
    +    preference = new CsvPreference.Builder('"', fieldDelimiter, lineDelimiter).build();
    +    int countKeyValue = getFields().size();
    +    nameMapping = new String[countKeyValue];
    +    processors = new CellProcessor[countKeyValue];
    +    initialise(nameMapping, processors);
    +   
    +  }
    +
    +  private void initialise(String[] nameMapping, CellProcessor[] processors)
    +  {
    +    for (int i = 0; i < getFields().size(); i++) {
    +      FIELD_TYPE type = getFields().get(i).type;
    +      nameMapping[i] = getFields().get(i).name;
    +       if (type == FIELD_TYPE.DATE) {
    +        String dateFormat = getFields().get(i).format;
    +        processors[i] = new Optional(new FmtDate(dateFormat == null ? "dd/MM/yyyy" :
dateFormat));
    +      }else{
    +        processors[i] = new Optional();
    +      }
    +    }
    +
    +  }
    --- End diff --
    
    newline after function.


---
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