apex-dev mailing list archives

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

    https://github.com/apache/incubator-apex-malhar/pull/22#discussion_r41476959
  
    --- Diff: contrib/src/main/java/com/datatorrent/contrib/schema/formatter/CsvFormatter.java
---
    @@ -0,0 +1,268 @@
    +/**
    + * Copyright (C) 2015 DataTorrent, Inc.
    + *
    + * Licensed 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.io.CsvBeanWriter;
    +import org.supercsv.io.ICsvBeanWriter;
    +import org.supercsv.prefs.CsvPreference;
    +
    +import com.datatorrent.api.Context;
    +
    +/**
    + * 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
    + */
    +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[] field_info = fieldInfo.split(",");
    +    for ( int i=0 ; i < field_info.length ; i++ ) {
    +      String[] field_tuple = field_info[i].split(":");
    +      Field field = new Field();
    +      field.setName(field_tuple[0]);
    +      String[] type_format = field_tuple[1].split("\\|");
    +      field.setType(type_format[0].toUpperCase());
    +      if ( type_format.length > 1 ) {
    +        field.setFormat(type_format[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();
    +      }
    +    }
    +
    +  }
    +  @Override
    +  public void activate(Context context)
    +  {
    +    // TODO Auto-generated method stub
    +    
    +  }
    +
    +  @Override
    +  public void deactivate()
    +  {
    +    // TODO Auto-generated method stub
    +    
    +  }
    +
    +  @Override
    +  public String convert(Object tuple)
    +  {
    +    try {
    +      StringWriter stringWriter = new StringWriter();
    +      ICsvBeanWriter beanWriter = new CsvBeanWriter(stringWriter, preference);
    +      beanWriter.write(tuple, nameMapping,processors);
    +      beanWriter.flush();
    +      beanWriter.close();
    +      return stringWriter.toString();
    +    }
    +    catch (IOException e) {
    +      logger.error("Error while processing {} ", e.getMessage());
    --- End diff --
    
    Propagate exception.


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