ijokarumawak 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_r263678021
########## 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); Review comment: Further, we could add `CUSTOM_LOOKUP_SQL`. To support custom where conditions and order by ... etc. Design: - If CUSTOM_LOOKUP_SQL is specified, TABLE_NAME and LOOKUP_KEY_COLUMN are not required - User would specify a custom query like `select firstName, lastName from users where companyName = ${companyName} and department = ${department} order by job_responsibility` - Then DatabaseRecordLookupService can understand what coordinates are required by using VariableImpact.isAffected. ProcessGroup can be a reference for how to do this. https://github.com/apache/nifi/blob/master/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/groups/StandardProcessGroup.java#L2975 - Then, the ControllerService replaces EL part into NamedParameter. Should note on the doc that EL function can not be used here. If needed, it should be done at Processor's dynamic properties. - The generated SQL would look like `select firstName, lastName from users where companyName = :companyName and department = :department order by job_responsibility` - Then user would configure LookupRecord with two dynamic properties: - `companyName`: `/companyName` - `departmentName`: `/departmentName` This way, user can lookup record very flexibly. We can work on this with separate JIRA if preferred. ---------------------------------------------------------------- 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
