Github user rickysaltzer commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2020#discussion_r130412143
  
    --- Diff: 
nifi-nar-bundles/nifi-kudu-bundle/nifi-kudu-processors/src/main/java/org/apache/nifi/processors/kudu/PutKudu.java
 ---
    @@ -0,0 +1,120 @@
    +/*
    + * 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.kudu;
    +
    +import org.apache.kudu.Schema;
    +import org.apache.kudu.Type;
    +import org.apache.kudu.client.Insert;
    +import org.apache.kudu.client.PartialRow;
    +import org.apache.kudu.client.KuduTable;
    +
    +import org.apache.nifi.annotation.behavior.EventDriven;
    +import org.apache.nifi.annotation.behavior.InputRequirement;
    +import org.apache.nifi.annotation.behavior.SupportsBatching;
    +import org.apache.nifi.annotation.documentation.CapabilityDescription;
    +import org.apache.nifi.annotation.documentation.Tags;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.processor.Relationship;
    +import org.apache.nifi.serialization.record.Record;
    +
    +import java.util.ArrayList;
    +import java.util.HashSet;
    +import java.util.List;
    +import java.util.Set;
    +
    +@EventDriven
    +@SupportsBatching
    +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED)
    +@Tags({"put", "database", "NoSQL", "kudu", "HDFS"})
    +@CapabilityDescription("Reads records from an incoming FlowFile using the 
provided Record Reader, and writes those records " +
    +        "to the specified Kudu's table. The schema for the table must be 
provided in the processor properties or from your source." +
    +        " If any error occurs while reading records from the input, or 
writing records to Kudu, the FlowFile will be routed to failure")
    +
    +public class PutKudu extends AbstractKudu {
    +
    +    @Override
    +    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
    +        final List<PropertyDescriptor> properties = new ArrayList<>();
    +        properties.add(KUDU_MASTERS);
    +        properties.add(TABLE_NAME);
    +        properties.add(SKIP_HEAD_LINE);
    +        properties.add(RECORD_READER);
    +
    +        return properties;
    +    }
    +
    +    @Override
    +    public Set<Relationship> getRelationships() {
    +        final Set<Relationship> rels = new HashSet<>();
    +        rels.add(REL_SUCCESS);
    +        rels.add(REL_FAILURE);
    +        return rels;
    +    }
    +
    +    @Override
    +    protected Insert insertRecordToKudu(KuduTable kuduTable, Record 
record, List<String> fieldNames) throws IllegalStateException, Exception {
    +        Insert insert = kuduTable.newInsert();
    +        PartialRow row = insert.getRow();
    +        Schema colSchema = kuduTable.getSchema();
    +
    +        for (String colName : fieldNames) {
    +            int colIdx = this.getColumnIndex(colSchema, colName);
    +            if (colIdx != -1) {
    +                Type colType = 
colSchema.getColumnByIndex(colIdx).getType();
    +
    +                switch (colType.getDataType()) {
    +                    case BOOL:
    +                        row.addBoolean(colIdx, 
record.getAsBoolean(colName));
    +                        break;
    +                    case FLOAT:
    +                        row.addFloat(colIdx, record.getAsFloat(colName));
    +                        break;
    +                    case DOUBLE:
    +                        row.addDouble(colIdx, record.getAsDouble(colName));
    +                        break;
    +                    case BINARY:
    +                        row.addBinary(colIdx, 
record.getAsString(colName).getBytes());
    +                        break;
    +                    case INT8:
    +                    case INT16:
    --- End diff --
    
    You can cast it from `int` to `short` before calling `addShort(..)`. If the 
user is using an `int` field to store a value inclusive of a `short` range then 
it's safe to cast. If the user is storing something outside the range of a 
`short` then an `Exception` will be thrown. I think this is a better approach 
since it allows users to utilize the `INT8` and `INT16` column types if they 
are able to guarantee that they're in contract with those value ranges. 


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to