Return-Path: X-Original-To: apmail-apex-dev-archive@minotaur.apache.org Delivered-To: apmail-apex-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E3C8C18EA8 for ; Mon, 25 Apr 2016 07:26:16 +0000 (UTC) Received: (qmail 91032 invoked by uid 500); 25 Apr 2016 07:26:16 -0000 Delivered-To: apmail-apex-dev-archive@apex.apache.org Received: (qmail 90966 invoked by uid 500); 25 Apr 2016 07:26:16 -0000 Mailing-List: contact dev-help@apex.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@apex.incubator.apache.org Delivered-To: mailing list dev@apex.incubator.apache.org Received: (qmail 90955 invoked by uid 99); 25 Apr 2016 07:26:16 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Apr 2016 07:26:16 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id CC3ABC19EF for ; Mon, 25 Apr 2016 07:26:15 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -5.016 X-Spam-Level: X-Spam-Status: No, score=-5.016 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.996] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id d6yq10p5i94Y for ; Mon, 25 Apr 2016 07:26:13 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with SMTP id 80A9D5F2F1 for ; Mon, 25 Apr 2016 07:26:13 +0000 (UTC) Received: (qmail 90495 invoked by uid 99); 25 Apr 2016 07:26:12 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Apr 2016 07:26:12 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id D24982C1F5C for ; Mon, 25 Apr 2016 07:26:12 +0000 (UTC) Date: Mon, 25 Apr 2016 07:26:12 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: dev@apex.incubator.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (APEXMALHAR-2023) Adding Enrichment Operator to Malhar MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/APEXMALHAR-2023?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15256002#comment-15256002 ] 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_r60870827 --- Diff: contrib/src/main/java/com/datatorrent/contrib/enrich/POJOEnricher.java --- @@ -0,0 +1,264 @@ +/** + * 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.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.commons.lang3.ClassUtils; +import org.apache.hadoop.classification.InterfaceStability; + +import com.datatorrent.api.Context; +import com.datatorrent.api.DefaultInputPort; +import com.datatorrent.api.DefaultOutputPort; +import com.datatorrent.api.annotation.InputPortFieldAnnotation; +import com.datatorrent.api.annotation.OutputPortFieldAnnotation; +import com.datatorrent.lib.util.FieldInfo; +import com.datatorrent.lib.util.PojoUtils; + + +/** + * This class takes a POJO 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 fieldMap are added to original tuple. + * This operator is App Builder schema support enabled.
+ *

+ * Properties:
+ * inputClass: Class to be loaded for the incoming data type
+ * outputClass: Class to be loaded for the emitted data type
+ *
+ *

+ * 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" + * { "productId": 1, "productCategory": 3 } + * { "productId": 4, "productCategory": 10 } + * { "productId": 3, "productCategory": 1 } + *

+ * And input tuple is + * { amount=10.0, channelId=4, productId=3 } + *

+ * The tuple is modified as below before operator emits it on output port. + * { amount=10.0, channelId=4, productId=3, productCategory=1 } + * + * @displayName BeanEnrichment + * @category Database + * @tags enrichment, pojo, schema, lookup + */ +@InterfaceStability.Evolving +public class POJOEnricher extends AbstractEnricher +{ + private static final Logger logger = LoggerFactory.getLogger(POJOEnricher.class); + + /** + * Helper fields + */ + protected Class inputClass; + protected Class outputClass; + private transient Map fieldMap = new HashMap<>(); + private transient List includeSetters = new ArrayList<>(); + private transient List lookupGetters = new ArrayList<>(); + + @InputPortFieldAnnotation(schemaRequired = true) + public final transient DefaultInputPort input = new DefaultInputPort() + { + @Override + public void setup(Context.PortContext context) + { + inputClass = context.getValue(Context.PortContext.TUPLE_CLASS); + } + + @Override + public void process(Object object) + { + processTuple(object); + } + }; + + @OutputPortFieldAnnotation(schemaRequired = true) + public final transient DefaultOutputPort output = new DefaultOutputPort() + { + @Override + public void setup(Context.PortContext context) + { + outputClass = context.getValue(Context.PortContext.TUPLE_CLASS); + } + }; + + protected void processTuple(Object object) + { + enrichTuple(object); + } + + @Override + protected Object getKey(Object tuple) + { + ArrayList keyList = new ArrayList<>(); + for (PojoUtils.Getter lookupGetter : lookupGetters) { + keyList.add(lookupGetter.get(tuple)); + } + return keyList; + } + + @Override + protected Object convert(Object in, Object cached) + { + Object o; + + try { + o = outputClass.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + logger.error("Failed to create new instance of output POJO", e); + return null; --- End diff -- Can the input tuple be redirected to error port? > 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)