mattyb149 commented on a change in pull request #3341: NIFI-6082: Added 
DatabaseRecordLookupService, refactored common DB utils
URL: https://github.com/apache/nifi/pull/3341#discussion_r264000936
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/db/DatabaseRecordLookupService.java
 ##########
 @@ -0,0 +1,150 @@
+/*
+ * 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.lookup.db;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.avro.Schema;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.avro.AvroTypeUtil;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.controller.ControllerServiceInitializationContext;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.lookup.LookupFailureException;
+import org.apache.nifi.lookup.RecordLookupService;
+import org.apache.nifi.serialization.record.Record;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.ResultSetRecordSet;
+import org.apache.nifi.util.Tuple;
+import org.apache.nifi.util.db.JdbcCommon;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+@Tags({"lookup", "cache", "enrich", "join", "rdbms", "database", "reloadable", 
"key", "value", "record"})
+@CapabilityDescription("A relational-database-based lookup service. When the 
lookup key is found in the database, " +
+        "the columns are returned as a Record. Only one row will be returned 
for each lookup, duplicate database entries are ignored.")
+public class DatabaseRecordLookupService extends AbstractDatabaseLookupService 
implements RecordLookupService {
+
+    private volatile Cache<Tuple<String, Object>, Record> cache;
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+
+    @Override
+    protected void init(final ControllerServiceInitializationContext context) {
+        final List<PropertyDescriptor> properties = new ArrayList<>();
+        properties.add(DBCP_SERVICE);
+        properties.add(TABLE_NAME);
+        properties.add(LOOKUP_KEY_COLUMN);
+        properties.add(CACHE_SIZE);
+        properties.add(CLEAR_CACHE_ON_ENABLED);
+        this.properties = Collections.unmodifiableList(properties);
+    }
+
+    @OnEnabled
+    public void onEnabled(final ConfigurationContext context) {
+        this.dbcpService = 
context.getProperty(DBCP_SERVICE).asControllerService(DBCPService.class);
+        this.lookupKeyColumn = 
context.getProperty(LOOKUP_KEY_COLUMN).evaluateAttributeExpressions().getValue();
+        int cacheSize = 
context.getProperty(CACHE_SIZE).evaluateAttributeExpressions().asInteger();
+        boolean clearCache = 
context.getProperty(CLEAR_CACHE_ON_ENABLED).asBoolean();
+        if(this.cache == null || (cacheSize > 0 && clearCache)) {
+            this.cache = Caffeine.newBuilder()
+                    .maximumSize(cacheSize)
+                    .build();
+        }
+
+        options = JdbcCommon.AvroConversionOptions.builder()
+                .recordName("NiFi_DB_Record_Lookup")
+                // Ignore duplicates
+                .maxRows(1)
+                // Keep column names as field names
+                .convertNames(false)
+                .useLogicalTypes(true)
+                .build();
+    }
+
+    @Override
+    public Optional<Record> lookup(Map<String, Object> coordinates) throws 
LookupFailureException {
+        return lookup(coordinates, null);
+    }
+
+    @Override
+    public Optional<Record> lookup(final Map<String, Object> coordinates, 
Map<String, String> context) throws LookupFailureException {
+        if (coordinates == null) {
+            return Optional.empty();
+        }
+
+        final Object key = coordinates.get(KEY);
+        if (StringUtils.isBlank(key.toString())) {
+            return Optional.empty();
+        }
+
+        final String tableName = 
getProperty(TABLE_NAME).evaluateAttributeExpressions(context).getValue();
+
+        Tuple<String, Object> cacheLookupKey = new Tuple<>(tableName, key);
+
+        // Not using the function param of cache.get so we can catch and 
handle the checked exceptions
+        Record foundRecord = cache.get(cacheLookupKey, k -> null);
+
+        if (foundRecord == null) {
+            final String selectQuery = "SELECT * FROM " + tableName + " WHERE 
" + lookupKeyColumn + " = ?";
+            try (final Connection con = dbcpService.getConnection(context);
+                 final PreparedStatement st = 
con.prepareStatement(selectQuery)) {
+
+                st.setObject(1, key);
+                ResultSet resultSet = st.executeQuery();
+                final Schema avroSchema = JdbcCommon.createSchema(resultSet, 
options);
+                final RecordSchema recordAvroSchema = 
AvroTypeUtil.createSchema(avroSchema);
+                ResultSetRecordSet resultSetRecordSet = new 
ResultSetRecordSet(resultSet, recordAvroSchema, true);
 
 Review comment:
   I added this because I ran into an issue somewhere, where the result set 
says the column is not nullable, but the overall record schema needed to 
support nullability. I'll try to reproduce, I should've documented it more 
carefully.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to