nifi-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [nifi] tpalfy commented on a change in pull request #3977: NIFI-7007 Add update functionality to the PutCassandraRecord processor.
Date Tue, 21 Jan 2020 13:31:40 GMT
tpalfy commented on a change in pull request #3977: NIFI-7007 Add update functionality to the
PutCassandraRecord processor.
URL: https://github.com/apache/nifi/pull/3977#discussion_r368970533
 
 

 ##########
 File path: nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/main/java/org/apache/nifi/processors/cassandra/PutCassandraRecord.java
 ##########
 @@ -193,6 +303,81 @@ public void onTrigger(ProcessContext context, ProcessSession session)
throws Pro
 
     }
 
+    private Statement generateUpdate(String cassandraTable, RecordSchema schema, String updateKeys,
String updateMethod, Map<String, Object> recordContentMap) {
+        Update updateQuery;
+
+        // Split up the update key names separated by a comma, should not be empty
+        final Set<String> updateKeyNames;
+        updateKeyNames = Arrays.stream(updateKeys.split(","))
+                .map(String::trim)
+                .filter(StringUtils::isNotEmpty)
+                .collect(Collectors.toSet());
+        if (updateKeyNames.isEmpty()) {
+            throw new IllegalArgumentException("No Update Keys were specified");
+        }
+
+        // Verify if all update keys are present in the record
+        for (String updateKey : updateKeyNames) {
+            if (!schema.getFieldNames().contains(updateKey)) {
+                throw new IllegalArgumentException("Update key '" + updateKey + "' is not
present in the record schema");
+            }
+        }
+
+        // Prepare keyspace/table names
+        if (cassandraTable.contains(".")) {
+            String[] keyspaceAndTable = cassandraTable.split("\\.");
+            updateQuery = QueryBuilder.update(keyspaceAndTable[0], keyspaceAndTable[1]);
+        } else {
+            updateQuery = QueryBuilder.update(cassandraTable);
+        }
+
+        // Loop through the field names, setting those that are not in the update key set,
and using those
+        // in the update key set as conditions.
+        for (String fieldName : schema.getFieldNames()) {
+            Object fieldValue = recordContentMap.get(fieldName);
+
+            if (updateKeyNames.contains(fieldName)) {
+                updateQuery.where(QueryBuilder.eq(fieldName, fieldValue));
+            } else {
+                Assignment assignment;
+                if (SET_TYPE.getValue().equalsIgnoreCase(updateMethod)) {
+                    assignment = QueryBuilder.set(fieldName, fieldValue);
+                } else {
+                    // Check if the fieldValue is of type long, as this is the only type
that is can be used,
+                    // to increment or decrement.
+                    if (!(fieldValue instanceof Long)) {
 
 Review comment:
   Why can only Long types be used to increment or decrement?
   This schema could be inferred from a JSON or a CSV and some fields could be integers.
   The Cassandra update statement could still be valid couldn't it?

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