nifi-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@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
Date Thu, 16 Apr 2020 19:21:38 GMT
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_r409793723
 
 

 ##########
 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<Relationship> relationships;
+    private final static List<PropertyDescriptor> propertyDescriptors;
+
+    private final static ObjectMapper mapper;
+
+    static {
+        List<PropertyDescriptor> _propertyDescriptors = new ArrayList<>();
+        _propertyDescriptors.addAll(descriptors);
+        _propertyDescriptors.add(CHARACTER_SET);
+        propertyDescriptors = Collections.unmodifiableList(_propertyDescriptors);
+
+        final Set<Relationship> _relationships = new HashSet<>();
+        _relationships.add(REL_SUCCESS);
+        _relationships.add(REL_FAILURE);
+        relationships = Collections.unmodifiableSet(_relationships);
+        mapper = new ObjectMapper();
+    }
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return relationships;
+    }
+
+    @Override
+    public List<PropertyDescriptor> 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:
   wrapped with logger.isDebugEnabled().

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

Mime
View raw message