[
https://issues.apache.org/jira/browse/APEXMALHAR-2023?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15255985#comment-15255985
]
ASF GitHub Bot commented on APEXMALHAR-2023:
--------------------------------------------
Github user sandeepdeshmukh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/235#discussion_r60869315
--- Diff:
contrib/src/main/java/com/datatorrent/contrib/enrich/MapEnricher.java ---
@@ -0,0 +1,150 @@
+/**
+ * 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.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.datatorrent.api.DefaultInputPort;
+import com.datatorrent.api.DefaultOutputPort;
+import com.datatorrent.lib.util.FieldInfo;
+
+/**
+ * This class takes a HashMap tuple as input and extract the value of the
lookupKey configured
+ * for this operator. It then does a lookup in file/DB to find matching
entry and all key-value pairs
+ * specified in the file/DB or based on include fields are added to
original tuple.
+ * <p>
+ * Example
+ * The file contains data in json format, one entry per line. during setup
entire file is read and
+ * kept in memory for quick lookup.
+ * If file contains following lines, and operator is configured with
lookup key "productId"
+ * <p>
+ * { "productId": 1, "productCategory": 3 }
+ * { "productId": 4, "productCategory": 10 }
+ * { "productId": 3, "productCategory": 1 }
+ * </p>
+ * And input tuple is
+ * <p>
+ * { amount=10.0, channelId=4, productId=3 }
+ * </p>
+ * The tuple is modified as below before operator emits it on output port.
+ * <p>
+ * { amount=10.0, channelId=4, productId=3, productCategory=1 }
+ * </p>
+ *
+ * @displayName MapEnrichment
+ * @category Database
+ * @tags enrichment, lookup, map
+ */
[email protected]
+public class MapEnricher extends AbstractEnricher<Map<String, Object>,
Map<String, Object>>
+{
+ public final transient DefaultInputPort<Map<String, Object>> input = new
DefaultInputPort<Map<String, Object>>()
+ {
+ @Override
+ public void process(Map<String, Object> obj)
+ {
+ processTuple(obj);
+ }
+ };
+
+ public final transient DefaultOutputPort<Map<String, Object>> output =
new DefaultOutputPort<>();
+
+ private void processTuple(Map<String, Object> obj)
+ {
+ enrichTuple(obj);
+ }
+
+ @Override
+ protected Object getKey(Map<String, Object> tuple)
+ {
+ ArrayList<Object> keyList = new ArrayList<Object>();
+
+ for (FieldInfo fieldInfo : lookupFieldInfo) {
+ keyList.add(tuple.get(fieldInfo.getColumnName()));
+ }
+
+ return keyList;
+ }
+
+ @Override
+ protected Map<String, Object> convert(Map<String, Object> in, Object
cached)
+ {
+ if (cached == null) {
+ return in;
+ }
+
+ ArrayList<Object> newAttributes = (ArrayList<Object>)cached;
+ if (newAttributes != null) {
+ for (int i = 0; i < includeFieldInfo.size(); i++) {
+ in.put(includeFieldInfo.get(i).getColumnName(),
newAttributes.get(i));
+ }
+ }
+ return in;
+ }
+
+ @Override
+ protected void emitTuple(Map<String, Object> tuple)
+ {
+ output.emit(tuple);
+ }
+
+ @Override
+ protected Class<?> getIncludeFieldType(String fieldName)
+ {
+ return Object.class;
+ }
+
+ @Override
+ protected Class<?> getLookupFieldType(String fieldName)
+ {
+ return Object.class;
+ }
+
+ /**
+ * Set fields on which lookup needs to happen in external store.
+ * This is a mandatory parameter to set.
+ *
+ * @param lookupFields List of fields on which lookup happens.
+ * @description $[] Field which become part of lookup key
+ * @useSchema $[] input.fields[].name
+ */
+ @Override
+ public void setLookupFields(List<String> lookupFields)
+ {
+ super.setLookupFields(lookupFields);
+ }
+
+ /**
+ * Set fields on which lookup needs to happen in external store.
--- End diff --
Correct this.
> Adding Enrichment Operator to Malhar
> ------------------------------------
>
> Key: APEXMALHAR-2023
> URL: https://issues.apache.org/jira/browse/APEXMALHAR-2023
> Project: Apache Apex Malhar
> Issue Type: New Feature
> Components: adapters database
> Affects Versions: 3.3.1
> Reporter: Chinmay Kolhatkar
> Assignee: Chinmay Kolhatkar
>
> Add Enrichment Operator to Apex Malhar.
> Discussion is happening in mailing list here:
> http://mail-archives.apache.org/mod_mbox/incubator-apex-dev/201603.mbox/%3CCAKJfLDMo24-Gcvum2ZL8-0JOnE8QLryAy0Zu_R5zhMd_bsJyHw%40mail.gmail.com%3E
> Ponymail permalink:
> https://pony-poc.apache.org/thread.html/Z8t5ut5pu5vprgt
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)