granthenke commented on a change in pull request #3732: NIFI-6662: Adding Kudu Lookup Service URL: https://github.com/apache/nifi/pull/3732#discussion_r324428929
########## File path: nifi-nar-bundles/nifi-kudu-bundle/nifi-kudu-controller-service/src/main/java/org/apache/nifi/controller/kudu/KuduLookupService.java ########## @@ -0,0 +1,323 @@ +/* + * 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.controller.kudu; + +import org.apache.kudu.ColumnSchema; +import org.apache.kudu.Schema; +import org.apache.kudu.Type; +import org.apache.kudu.client.AsyncKuduClient; +import org.apache.kudu.client.KuduClient; +import org.apache.kudu.client.KuduException; +import org.apache.kudu.client.KuduPredicate; +import org.apache.kudu.client.KuduScanner; +import org.apache.kudu.client.KuduTable; +import org.apache.kudu.client.RowResult; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.controller.ControllerServiceInitializationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.kerberos.KerberosCredentialsService; +import org.apache.nifi.lookup.RecordLookupService; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.reporting.InitializationException; +import org.apache.nifi.security.krb.KerberosAction; +import org.apache.nifi.security.krb.KerberosKeytabUser; +import org.apache.nifi.security.krb.KerberosUser; +import org.apache.nifi.serialization.SimpleRecordSchema; +import org.apache.nifi.serialization.record.MapRecord; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.serialization.record.RecordField; +import org.apache.nifi.serialization.record.RecordFieldType; +import org.apache.nifi.serialization.record.RecordSchema; + +import javax.security.auth.login.LoginException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + + +@CapabilityDescription("Lookup a record from Kudu Server associated with the specified key. Binary columns are base64 encoded. Only one matched row will be returned") +@Tags({"lookup", "enrich", "key", "value", "kudu"}) +public class KuduLookupService extends AbstractControllerService implements RecordLookupService { + + static final PropertyDescriptor KUDU_MASTERS = new PropertyDescriptor.Builder() + .name("kudu-lu-masters") + .displayName("Kudu Masters") + .description("Comma separated addresses of the Kudu masters to connect to.") + .required(true) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + static final PropertyDescriptor KERBEROS_CREDENTIALS_SERVICE = new PropertyDescriptor.Builder() + .name("kudu-lu-kerberos-credentials-service") + .displayName("Kerberos Credentials Service") + .description("Specifies the Kerberos Credentials to use for authentication") + .required(false) + .identifiesControllerService(KerberosCredentialsService.class) + .build(); + + static final PropertyDescriptor KUDU_OPERATION_TIMEOUT_MS = new PropertyDescriptor.Builder() + .name("kudu-lu-operations-timeout-ms") + .displayName("Kudu Operation Timeout") + .description("Default timeout used for user operations (using sessions and scanners)") + .required(false) + .defaultValue(AsyncKuduClient.DEFAULT_OPERATION_TIMEOUT_MS + "ms") + .addValidator(StandardValidators.TIME_PERIOD_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor TABLE_NAME = new PropertyDescriptor.Builder() + .name("kudu-lu-table-name") + .displayName("Kudu Table Name") + .description("Name of the table to access.") + .required(true) + .defaultValue("default") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + static final PropertyDescriptor RETURN_COLUMNS = new PropertyDescriptor.Builder() + .name("kudu-lu-return-cols") + .displayName("Kudu Return Columns") + .description("A comma-separated list of columns to return when scanning. To return all columns set to \"*\"") + .required(true) + .defaultValue("*") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + + protected List<PropertyDescriptor> properties; + + protected KerberosCredentialsService credentialsService; + private volatile KerberosUser kerberosUser; + + protected String kuduMasters; + protected KuduClient kuduClient; + protected volatile String tableName; + protected volatile KuduTable table; + protected volatile List<String> columnNames; + + protected volatile RecordSchema resultSchema; + protected volatile Schema tableSchema; + + @Override + protected void init(final ControllerServiceInitializationContext context) { + final List<PropertyDescriptor> properties = new ArrayList<>(); + properties.add(KUDU_MASTERS); + properties.add(KERBEROS_CREDENTIALS_SERVICE); + properties.add(KUDU_OPERATION_TIMEOUT_MS); + properties.add(TABLE_NAME); + properties.add(RETURN_COLUMNS); + addProperties(properties); + this.properties = Collections.unmodifiableList(properties); + } + + protected void addProperties(List<PropertyDescriptor> properties) { + } + + protected void createKuduClient(ConfigurationContext context) throws LoginException { + final String kuduMasters = context.getProperty(KUDU_MASTERS).evaluateAttributeExpressions().getValue(); + final KerberosCredentialsService credentialsService = context.getProperty(KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class); + + if (credentialsService == null) { + return; + } + + final String keytab = credentialsService.getKeytab(); + final String principal = credentialsService.getPrincipal(); + kerberosUser = loginKerberosUser(principal, keytab); + + final KerberosAction<KuduClient> kerberosAction = new KerberosAction<>(kerberosUser, () -> buildClient(kuduMasters, context), getLogger()); + this.kuduClient = kerberosAction.execute(); + } + protected KerberosUser loginKerberosUser(final String principal, final String keytab) throws LoginException { + final KerberosUser kerberosUser = new KerberosKeytabUser(principal, keytab); + kerberosUser.login(); + return kerberosUser; + } + protected KuduClient buildClient(final String masters, final ConfigurationContext context) { + final Integer operationTimeout = context.getProperty(KUDU_OPERATION_TIMEOUT_MS).asTimePeriod(TimeUnit.MILLISECONDS).intValue(); + + return new KuduClient.KuduClientBuilder(masters) + .defaultOperationTimeoutMs(operationTimeout) + .build(); + } + /** + * Establish a connection to a Kudu cluster. + * @param context the configuration context + * @throws InitializationException if unable to connect a Kudu cluster + */ + @OnEnabled + public void onEnabled(final ConfigurationContext context) throws InitializationException { + + try { + kuduMasters = context.getProperty(KUDU_MASTERS).evaluateAttributeExpressions().getValue(); + credentialsService = context.getProperty(KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class); + + if (kuduClient == null) { + getLogger().debug("Setting up Kudu connection..."); + + createKuduClient(context); + getLogger().debug("Kudu connection successfully initialized"); + } + } catch(Exception ex){ + getLogger().error("Exception occurred while interacting with Kudu due to " + ex.getMessage(), ex); + throw new InitializationException(ex); + } + + tableName = context.getProperty(TABLE_NAME).evaluateAttributeExpressions().getValue(); + try { + table = kuduClient.openTable(tableName); + tableSchema = table.getSchema(); + columnNames = getColumns(context.getProperty(RETURN_COLUMNS).getValue()); + + //Result Schema + resultSchema = kuduSchemaToNiFiSchema(tableSchema, columnNames); + + } catch (KuduException e) { + throw new IllegalArgumentException(e); + } + } + + @Override + public Set<String> getRequiredKeys() { + return new HashSet<>(); + } + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return properties; + } + @Override Review comment: nit: add blank line above, ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services