From issues-return-95951-archive-asf-public=cust-asf.ponee.io@nifi.apache.org Thu Apr 16 21:02:14 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 5407A180638 for ; Thu, 16 Apr 2020 23:02:14 +0200 (CEST) Received: (qmail 92796 invoked by uid 500); 16 Apr 2020 21:02:13 -0000 Mailing-List: contact issues-help@nifi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@nifi.apache.org Delivered-To: mailing list issues@nifi.apache.org Received: (qmail 92787 invoked by uid 99); 16 Apr 2020 21:02:13 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Apr 2020 21:02:13 +0000 From: GitBox To: issues@nifi.apache.org Subject: [GitHub] [nifi] sjyang18 commented on a change in pull request #4200: NIFI-7335: PutAzureCosmosDBDocument to provide the cosmos sql api support for Azure Cosmos DB Message-ID: <158707093363.28832.5827769281721176306.gitbox@gitbox.apache.org> References: In-Reply-To: Date: Thu, 16 Apr 2020 21:02:13 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit sjyang18 commented on a change in pull request #4200: NIFI-7335: PutAzureCosmosDBDocument to provide the cosmos sql api support for Azure Cosmos DB URL: https://github.com/apache/nifi/pull/4200#discussion_r409846155 ########## File path: nifi-nar-bundles/nifi-azure-bundle/nifi-azure-processors/src/main/java/org/apache/nifi/processors/azure/cosmos/document/PutAzureCosmosDBDocument.java ########## @@ -0,0 +1,145 @@ +/* + * 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 org.apache.nifi.processors.azure.cosmos.document; + +import org.apache.nifi.annotation.behavior.EventDriven; +import org.apache.nifi.annotation.behavior.SystemResourceConsideration; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; +import org.apache.nifi.annotation.behavior.SystemResource; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.logging.ComponentLog; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.stream.io.StreamUtils; + +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +@EventDriven +@Tags({ "azure", "cosmos", "document", "insert", "update", "write", "put" }) +@InputRequirement(Requirement.INPUT_REQUIRED) +@CapabilityDescription("Writes the contents of a FlowFile to Azure Cosmos DB with Core SQL API. For CosmosDB with Mongo API, use PutMongo instead.") +@SystemResourceConsideration(resource = SystemResource.MEMORY) +public class PutAzureCosmosDBDocument extends AbstractAzureCosmosDBProcessor { + + private final static Set relationships; + private final static List propertyDescriptors; + + private final static ObjectMapper mapper; + + static { + List _propertyDescriptors = new ArrayList<>(); + _propertyDescriptors.addAll(descriptors); + _propertyDescriptors.add(CHARACTER_SET); + propertyDescriptors = Collections.unmodifiableList(_propertyDescriptors); + + final Set _relationships = new HashSet<>(); + _relationships.add(REL_SUCCESS); + _relationships.add(REL_FAILURE); + relationships = Collections.unmodifiableSet(_relationships); + mapper = new ObjectMapper(); + } + + @Override + public Set getRelationships() { + return relationships; + } + + @Override + public List getSupportedPropertyDescriptors() { + return propertyDescriptors; + } + + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + final FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + final ComponentLog logger = getLogger(); + + final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).getValue()); + final String partitionKeyField = context.getProperty(PARTITION_KEY).getValue(); + JsonNode doc = null; + + try { + // Read the contents of the FlowFile into a byte array + final byte[] content = new byte[(int) flowFile.getSize()]; + session.read(flowFile, in -> StreamUtils.fillBuffer(in, content, true)); + + // parse conten into JsonNode object + doc = mapper.readTree(new String(content, charset)); + + // make sure type of id is String type if exists + + if(doc.has("id")) { + JsonNode idNode = doc.get("id"); + if(idNode.isNumber()) { + logger.debug("coverting number id into string..."); + ((ObjectNode) doc).put("id", doc.get("id").asText()); + } + } else { + ((ObjectNode) doc).put("id",flowFile.getAttribute("uuid")); + } + logger.debug("after document ID verification"); + if(!doc.has(partitionKeyField)){ + logger.error("Required parition key field value not found from input flow file content with doc.id=" + doc.get("id")); + session.transfer(flowFile, REL_FAILURE); + context.yield(); + } else { + // insert doc if this is a new, otherwise update the existing document with upsertDocument API + logger.debug("Inserting/Updating document with upsertItem"); + this.container.upsertItem(doc); + + session.getProvenanceReporter().send(flowFile, getURI(context)); + session.transfer(flowFile, REL_SUCCESS); + logger.debug("Inserting/Updating document completed..."); + } + } catch (Exception e) { + logger.error("Failed to upsertDocument {} into CosmosDB due to {}", new Object[] {flowFile, e}, e); + if(doc != null){ + logger.error("Failed doc content : " + doc.toPrettyString()); Review comment: removed the line of logging failed document. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services