apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sandeepdeshmukh <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: APEXMALHAR-2023 Enricher
Date Tue, 19 Apr 2016 10:43:38 GMT
Github user sandeepdeshmukh commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/235#discussion_r60209083
  
    --- Diff: contrib/src/main/java/com/datatorrent/contrib/enrich/AbstractEnricher.java ---
    @@ -0,0 +1,319 @@
    +/**
    + * 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.enrich;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.List;
    +
    +import org.apache.hadoop.classification.InterfaceStability;
    +
    +import com.esotericsoftware.kryo.NotNull;
    +import com.datatorrent.api.Context;
    +import com.datatorrent.api.Operator;
    +import com.datatorrent.common.util.BaseOperator;
    +import com.datatorrent.lib.db.cache.CacheManager;
    +import com.datatorrent.lib.db.cache.CacheStore;
    +import com.datatorrent.lib.util.FieldInfo;
    +import com.datatorrent.lib.util.FieldInfo.SupportType;
    +
    +/**
    + * Base class for Enrichment Operator.&nbsp; Subclasses should provide implementation
to getKey and convert.
    + * The operator receives a tuple and emits enriched tuple based on includeFields and
lookupFields. <br/>
    + * <p>
    + * Properties:<br>
    + * <b>lookupFields</b>: List of comma separated keys for quick searching.
Ex: Field1,Field2,Field3<br>
    + * <b>includeFields</b>: List of comma separated fields to be replaced/added
to the input tuple. Ex: Field1,Field2,Field3<br>
    + * <b>store</b>: Specify the type of loader for looking data<br>
    + * <br>
    + *
    + * @param <INPUT>  Type of tuples which are received by this operator</T>
    + * @param <OUTPUT> Type of tuples which are emitted by this operator</T>
    + * @displayName Abstract Enrichment Operator
    + * @tags Enrichment
    + */
    +@InterfaceStability.Evolving
    +public abstract class AbstractEnricher<INPUT, OUTPUT> extends BaseOperator implements
Operator.ActivationListener
    +{
    +  /**
    +   * Mandatory parameters for Enricher
    +   */
    +  @NotNull
    +  protected List<String> lookupFields;
    +  @NotNull
    +  protected List<String> includeFields;
    +  @NotNull
    +  private BackendLoader store;
    +
    +  /**
    +   * Optional parameters for enricher.
    +   */
    +  private int cacheExpirationInterval = 24 * 60 * 60 * 1000;
    +  private int cacheCleanupInterval = 24 * 60 * 60 * 1000;
    +  private int cacheSize = 1024;
    +
    +  /**
    +   * Helper variables.
    +   */
    +  private transient CacheManager cacheManager;
    +  protected transient List<FieldInfo> lookupFieldInfo = new ArrayList<>();
    +  protected transient List<FieldInfo> includeFieldInfo = new ArrayList<>();
    +
    +  /**
    +   * This method needs to be called by implementing class for processing a tuple for
enrichment.
    +   * The method will take th tuple through following stages:
    +   * <ol>
    +   * <li>Call {@link #getKey(Object)} to retrieve key fields for lookup</li>
    +   * <li>Using key fields call cache manager to retrieve for any key that is cached
already</li>
    +   * <li>If not found in cache, it'll do a lookup in configured backend store</li>
    +   * <li>The retrieved fields will be passed to {@link #convert(Object, Object)}
method to create the final object</li>
    +   * <li>Finally {@link #emitTuple(Object)} is called for emitting the tuple</li>
    +   * </ol>
    +   *
    +   * @param tuple Input tuple that needs to get processed for enrichment.
    +   */
    +  protected void enrichTuple(INPUT tuple)
    +  {
    +    Object key = getKey(tuple);
    +    if (key != null) {
    +      Object result = cacheManager.get(key);
    +      OUTPUT out = convert(tuple, result);
    +      emitTuple(out);
    +    }
    +  }
    +
    +  /**
    +   * The method should be implemented by concrete class which returns an ArrayList<Object>
containing all the fields
    +   * which forms key part of lookup.
    +   * The order of field values should be same as the one set in {@link #lookupFields}
variable.
    +   *
    +   * @param tuple Input tuple from which fields values for key needs to be fetched.
    +   * @return Should return ArrayList<Object> which has fields values forming keys
in same order as {@link #lookupFields}
    +   */
    +  protected abstract Object getKey(INPUT tuple);
    +
    +  /**
    +   * The method should be implemented by concrete class.
    +   * This method is expected to take input tuple and a externally fetched object contained
field to be enriched, and
    +   * return a Enriched tuple which is ready to be emitted.
    --- End diff --
    
    an Enriched, 


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