You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "mattyb149 (via GitHub)" <gi...@apache.org> on 2023/11/16 21:56:53 UTC
[PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
mattyb149 opened a new pull request, #8042:
URL: https://github.com/apache/nifi/pull/8042
# Summary
[NIFI-12382](https://issues.apache.org/jira/browse/NIFI-12382) This PR adds a DatabaseSchemaRegistry controller service which gets the metadata for a specified database table and creates a RecordSchema from it.
# Tracking
Please complete the following tracking steps prior to pull request creation.
### Issue Tracking
- [x] [Apache NiFi Jira](https://issues.apache.org/jira/browse/NIFI) issue created
### Pull Request Tracking
- [x] Pull Request title starts with Apache NiFi Jira issue number, such as `NIFI-00000`
- [x] Pull Request commit message starts with Apache NiFi Jira issue number, as such `NIFI-00000`
### Pull Request Formatting
- [x] Pull Request based on current revision of the `main` branch
- [x] Pull Request refers to a feature branch with one commit containing changes
# Verification
Please indicate the verification steps performed prior to pull request creation.
### Build
- [ ] Build completed using `mvn clean install -P contrib-check`
- [x] JDK 21
### Licensing
- [ ] New dependencies are compatible with the [Apache License 2.0](https://apache.org/licenses/LICENSE-2.0) according to the [License Policy](https://www.apache.org/legal/resolved.html)
- [ ] New dependencies are documented in applicable `LICENSE` and `NOTICE` files
### Documentation
- [x] Documentation formatting appears as expected in rendered files
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "mattyb149 (via GitHub)" <gi...@apache.org>.
mattyb149 commented on code in PR #8042:
URL: https://github.com/apache/nifi/pull/8042#discussion_r1397661227
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/main/java/org/apache/nifi/db/schemaregistry/DatabaseSchemaRegistry.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.db.schemaregistry;
+
+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.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.schema.access.SchemaField;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.schemaregistry.services.SchemaRegistry;
+import org.apache.nifi.serialization.SimpleRecordSchema;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.util.DataTypeUtils;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+@Tags({"schema", "registry", "database", "table"})
+@CapabilityDescription("Provides a service for generating a record schema from a database table definition. The service is configured "
+ + "to use a table name and a database connection fetches the table metadata (i.e. table definition) such as column names, data types, "
+ + "nullability, etc.")
+public class DatabaseSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
+
+ private static final Set<SchemaField> schemaFields = EnumSet.of(SchemaField.SCHEMA_NAME);
+
+ static final PropertyDescriptor DBCP_SERVICE = new PropertyDescriptor.Builder()
+ .name("dbcp-service")
Review Comment:
Is that the new convention? We used to do the opposite when requiring both `name()` and `displayName()` to be set, `name` was a more machine-friendly name for things like internationalization and `displayName` was the display name in English.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "mattyb149 (via GitHub)" <gi...@apache.org>.
mattyb149 commented on code in PR #8042:
URL: https://github.com/apache/nifi/pull/8042#discussion_r1397658749
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/pom.xml:
##########
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-bundle</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>nifi-db-schema-registry-service</artifactId>
+ <packaging>jar</packaging>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-schema-registry-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-base</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-kerberos-credentials-service-api</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-kerberos-user-service-api</artifactId>
+ </dependency>
Review Comment:
I got an error when running tests, but it might be because of including nifi-dbcp-base. If I can refactor out the common properties I probably don't need these either.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #8042:
URL: https://github.com/apache/nifi/pull/8042#discussion_r1398201746
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/pom.xml:
##########
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-bundle</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>nifi-db-schema-registry-service</artifactId>
+ <packaging>jar</packaging>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-schema-registry-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-base</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </dependency>
Review Comment:
Thanks for clarifying, defining the properties for the test class looks good.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on PR #8042:
URL: https://github.com/apache/nifi/pull/8042#issuecomment-1816579844
Thanks for the additional input @pvillard31, that is helpful, and makes more sense now.
I realize that I was not evaluating all the implementation details. Now I see that the implementation uses the Database Metadata results, and that is what defines the information. Sorry for missing that detail earlier @mattyb149.
With that in mind, I can now see how this makes more sense as a way to use database table metadata for the schema definition. Perhaps including `Metadata` in Service class name would also help describe how this works.
With that background, are there any concerns about JDBC driver support across vendors? I would expect that more popular database vendors should work with this approach, but some may not, so that may be worth highlighting under additional details.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "mattyb149 (via GitHub)" <gi...@apache.org>.
mattyb149 commented on PR #8042:
URL: https://github.com/apache/nifi/pull/8042#issuecomment-1815409306
I'll add more documentation around the use case and what this service does, but I didn't see any use for a DB-backed registry that just stores schema text (such as a String column containing an Avro schema) since we have other implementations that basically do that. I can rename the service DatabaseTableSchemaRegistry if that helps make it clearer what this service is for, but the idea is to generate the schema from metadata rather than store and retrieve schemas. This can be used by things like ValidateRecord in order to ensure that records will successfully go into a database with a downstream PutDatabaseRecord for example.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on PR #8042:
URL: https://github.com/apache/nifi/pull/8042#issuecomment-1816528314
Thanks for the reply @mattyb149. Renaming the class could be helpful.
Another potential concern is using the schema name as the table name. Aside from schema naming conventions potentially conflicting with database table naming requirements, this would also require a table for every schema, which seems like it could be difficult to maintain. Although this could make sense in some specific environments, it seems like this might not be a good fit for inclusion as a component for general usage.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #8042:
URL: https://github.com/apache/nifi/pull/8042#discussion_r1397647623
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/main/java/org/apache/nifi/db/schemaregistry/DatabaseSchemaRegistry.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.db.schemaregistry;
+
+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.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.schema.access.SchemaField;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.schemaregistry.services.SchemaRegistry;
+import org.apache.nifi.serialization.SimpleRecordSchema;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.util.DataTypeUtils;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+@Tags({"schema", "registry", "database", "table"})
+@CapabilityDescription("Provides a service for generating a record schema from a database table definition. The service is configured "
+ + "to use a table name and a database connection fetches the table metadata (i.e. table definition) such as column names, data types, "
+ + "nullability, etc.")
+public class DatabaseSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
+
+ private static final Set<SchemaField> schemaFields = EnumSet.of(SchemaField.SCHEMA_NAME);
+
+ static final PropertyDescriptor DBCP_SERVICE = new PropertyDescriptor.Builder()
+ .name("dbcp-service")
Review Comment:
As a new Service, this is a good opportunity to use the same value for the name and display name.
```suggestion
.name("Database Connection Pooling Service")
```
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/main/java/org/apache/nifi/db/schemaregistry/DatabaseSchemaRegistry.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.db.schemaregistry;
+
+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.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.schema.access.SchemaField;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.schemaregistry.services.SchemaRegistry;
+import org.apache.nifi.serialization.SimpleRecordSchema;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.util.DataTypeUtils;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+@Tags({"schema", "registry", "database", "table"})
+@CapabilityDescription("Provides a service for generating a record schema from a database table definition. The service is configured "
+ + "to use a table name and a database connection fetches the table metadata (i.e. table definition) such as column names, data types, "
+ + "nullability, etc.")
+public class DatabaseSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
+
+ private static final Set<SchemaField> schemaFields = EnumSet.of(SchemaField.SCHEMA_NAME);
+
+ static final PropertyDescriptor DBCP_SERVICE = new PropertyDescriptor.Builder()
+ .name("dbcp-service")
+ .displayName("Database Connection Pooling Service")
+ .description("The Controller Service that is used to obtain a connection to the database for retrieving table information.")
+ .required(true)
+ .identifiesControllerService(DBCPService.class)
+ .build();
+
+ static final PropertyDescriptor CATALOG_NAME = new PropertyDescriptor.Builder()
+ .name("catalog-name")
+ .displayName("Catalog Name")
+ .description("The name of the catalog used to locate the desired table. This may not apply for the database that you are querying. In this case, leave the field empty. Note that if the "
+ + "property is set and the database is case-sensitive, the catalog name must match the database's catalog name exactly.")
+ .required(false)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+
+ static final PropertyDescriptor SCHEMA_NAME = new PropertyDescriptor.Builder()
+ .name("schema-name")
+ .displayName("Schema Name")
+ .description("The name of the schema that the table belongs to. This may not apply for the database that you are updating. In this case, leave the field empty. Note that if the "
+ + "property is set and the database is case-sensitive, the schema name must match the database's schema name exactly. Also notice that if the same table name exists in multiple "
+ + "schemas and Schema Name is not specified, the service will find those tables and give an error if the different tables have the same column name(s).")
+ .required(false)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+
+ protected List<PropertyDescriptor> propDescriptors = Collections.unmodifiableList(Arrays.asList(
+ DBCP_SERVICE,
+ CATALOG_NAME,
+ SCHEMA_NAME
+ ));
+
+ private volatile DBCPService dbcpService;
+ private volatile String dbCatalogName;
+ private volatile String dbSchemaName;
+
+ @Override
+ protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+ return propDescriptors;
+ }
+
+ @OnEnabled
+ public void onEnabled(final ConfigurationContext context) {
+ dbcpService = context.getProperty(DBCP_SERVICE).asControllerService(DBCPService.class);
+ dbCatalogName = context.getProperty(CATALOG_NAME).evaluateAttributeExpressions().getValue();
+ dbSchemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions().getValue();
+ }
+
+ @Override
+ public RecordSchema retrieveSchema(SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException {
+ if (schemaIdentifier.getName().isPresent()) {
+ return retrieveSchemaByName(schemaIdentifier);
+ } else {
+ throw new SchemaNotFoundException("This Schema Registry only supports retrieving a schema by name.");
+ }
+ }
+
+ @Override
+ public Set<SchemaField> getSuppliedSchemaFields() {
+ return schemaFields;
+ }
+
+ RecordSchema retrieveSchemaByName(final SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException {
+ final Optional<String> schemaName = schemaIdentifier.getName();
+ if (schemaName.isEmpty()) {
+ throw new SchemaNotFoundException("Cannot retrieve schema because Schema Name is not present");
+ }
+
+ final String tableName = schemaName.get();
+ try {
+ try (final Connection conn = dbcpService.getConnection()) {
+ final DatabaseMetaData dmd = conn.getMetaData();
+ try (final ResultSet colrs = dmd.getColumns(dbCatalogName, dbSchemaName, tableName, "%")) {
+ final List<RecordField> recordFields = new ArrayList<>();
+ while (colrs.next()) {
+ // COLUMN_DEF must be read first to work around Oracle bug, see NIFI-4279 for details
+ final String defaultValue = colrs.getString("COLUMN_DEF");
+ final String columnName = colrs.getString("COLUMN_NAME");
+ final int dataType = colrs.getInt("DATA_TYPE");
+ final String nullableValue = colrs.getString("IS_NULLABLE");
+ final boolean isNullable = "YES".equalsIgnoreCase(nullableValue) || nullableValue.isEmpty();
+ recordFields.add(new RecordField(
+ columnName,
+ DataTypeUtils.getDataTypeFromSQLTypeValue(dataType),
+ defaultValue,
+ isNullable));
+ }
+
+ // If no columns are found, check that the table exists
+ if (recordFields.isEmpty()) {
+ try (final ResultSet tblrs = dmd.getTables(dbCatalogName, dbSchemaName, tableName, null)) {
+ List<String> qualifiedNameSegments = new ArrayList<>();
+ if (dbCatalogName != null) {
+ qualifiedNameSegments.add(dbCatalogName);
+ }
+ if (dbSchemaName != null) {
+ qualifiedNameSegments.add(dbSchemaName);
+ }
+ qualifiedNameSegments.add(tableName);
+
+ if (!tblrs.next()) {
+ throw new SchemaNotFoundException("Table "
+ + String.join(".", qualifiedNameSegments)
+ + " not found");
+ } else {
+ getLogger().warn("Table "
+ + String.join(".", qualifiedNameSegments)
+ + " found but no columns were found, if this is not expected then check the user permissions for getting table metadata from the database");
+ }
Review Comment:
To avoid deep levels of code nesting, what do you think about breaking this out to a separate method?
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/pom.xml:
##########
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-bundle</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>nifi-db-schema-registry-service</artifactId>
+ <packaging>jar</packaging>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-schema-registry-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-base</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </dependency>
Review Comment:
Is this dependency necessary? It should only be required for DB Controller Service implementation, but I may be missing a reference.
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/main/java/org/apache/nifi/db/schemaregistry/DatabaseSchemaRegistry.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.db.schemaregistry;
+
+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.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.schema.access.SchemaField;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.schemaregistry.services.SchemaRegistry;
+import org.apache.nifi.serialization.SimpleRecordSchema;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.util.DataTypeUtils;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+@Tags({"schema", "registry", "database", "table"})
+@CapabilityDescription("Provides a service for generating a record schema from a database table definition. The service is configured "
+ + "to use a table name and a database connection fetches the table metadata (i.e. table definition) such as column names, data types, "
+ + "nullability, etc.")
+public class DatabaseSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
+
+ private static final Set<SchemaField> schemaFields = EnumSet.of(SchemaField.SCHEMA_NAME);
+
+ static final PropertyDescriptor DBCP_SERVICE = new PropertyDescriptor.Builder()
+ .name("dbcp-service")
+ .displayName("Database Connection Pooling Service")
+ .description("The Controller Service that is used to obtain a connection to the database for retrieving table information.")
+ .required(true)
+ .identifiesControllerService(DBCPService.class)
+ .build();
+
+ static final PropertyDescriptor CATALOG_NAME = new PropertyDescriptor.Builder()
+ .name("catalog-name")
+ .displayName("Catalog Name")
+ .description("The name of the catalog used to locate the desired table. This may not apply for the database that you are querying. In this case, leave the field empty. Note that if the "
+ + "property is set and the database is case-sensitive, the catalog name must match the database's catalog name exactly.")
+ .required(false)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+
+ static final PropertyDescriptor SCHEMA_NAME = new PropertyDescriptor.Builder()
+ .name("schema-name")
+ .displayName("Schema Name")
+ .description("The name of the schema that the table belongs to. This may not apply for the database that you are updating. In this case, leave the field empty. Note that if the "
+ + "property is set and the database is case-sensitive, the schema name must match the database's schema name exactly. Also notice that if the same table name exists in multiple "
+ + "schemas and Schema Name is not specified, the service will find those tables and give an error if the different tables have the same column name(s).")
+ .required(false)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+
+ protected List<PropertyDescriptor> propDescriptors = Collections.unmodifiableList(Arrays.asList(
+ DBCP_SERVICE,
+ CATALOG_NAME,
+ SCHEMA_NAME
+ ));
+
+ private volatile DBCPService dbcpService;
+ private volatile String dbCatalogName;
+ private volatile String dbSchemaName;
+
+ @Override
+ protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+ return propDescriptors;
+ }
+
+ @OnEnabled
+ public void onEnabled(final ConfigurationContext context) {
+ dbcpService = context.getProperty(DBCP_SERVICE).asControllerService(DBCPService.class);
+ dbCatalogName = context.getProperty(CATALOG_NAME).evaluateAttributeExpressions().getValue();
+ dbSchemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions().getValue();
+ }
+
+ @Override
+ public RecordSchema retrieveSchema(SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException {
+ if (schemaIdentifier.getName().isPresent()) {
+ return retrieveSchemaByName(schemaIdentifier);
+ } else {
+ throw new SchemaNotFoundException("This Schema Registry only supports retrieving a schema by name.");
+ }
+ }
+
+ @Override
+ public Set<SchemaField> getSuppliedSchemaFields() {
+ return schemaFields;
+ }
+
+ RecordSchema retrieveSchemaByName(final SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException {
+ final Optional<String> schemaName = schemaIdentifier.getName();
+ if (schemaName.isEmpty()) {
+ throw new SchemaNotFoundException("Cannot retrieve schema because Schema Name is not present");
+ }
+
+ final String tableName = schemaName.get();
+ try {
+ try (final Connection conn = dbcpService.getConnection()) {
+ final DatabaseMetaData dmd = conn.getMetaData();
+ try (final ResultSet colrs = dmd.getColumns(dbCatalogName, dbSchemaName, tableName, "%")) {
+ final List<RecordField> recordFields = new ArrayList<>();
+ while (colrs.next()) {
+ // COLUMN_DEF must be read first to work around Oracle bug, see NIFI-4279 for details
+ final String defaultValue = colrs.getString("COLUMN_DEF");
+ final String columnName = colrs.getString("COLUMN_NAME");
+ final int dataType = colrs.getInt("DATA_TYPE");
+ final String nullableValue = colrs.getString("IS_NULLABLE");
+ final boolean isNullable = "YES".equalsIgnoreCase(nullableValue) || nullableValue.isEmpty();
+ recordFields.add(new RecordField(
+ columnName,
+ DataTypeUtils.getDataTypeFromSQLTypeValue(dataType),
+ defaultValue,
+ isNullable));
+ }
+
+ // If no columns are found, check that the table exists
+ if (recordFields.isEmpty()) {
+ try (final ResultSet tblrs = dmd.getTables(dbCatalogName, dbSchemaName, tableName, null)) {
Review Comment:
Recommend adjusting the variable name for readability:
```suggestion
try (final ResultSet tablesResultSet = dmd.getTables(dbCatalogName, dbSchemaName, tableName, null)) {
```
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/pom.xml:
##########
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-bundle</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>nifi-db-schema-registry-service</artifactId>
+ <packaging>jar</packaging>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-schema-registry-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-base</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-kerberos-credentials-service-api</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-kerberos-user-service-api</artifactId>
+ </dependency>
Review Comment:
Are these dependencies used?
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #8042:
URL: https://github.com/apache/nifi/pull/8042#discussion_r1415801708
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/main/java/org/apache/nifi/db/schemaregistry/DatabaseTableSchemaRegistry.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.db.schemaregistry;
+
+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.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.schema.access.SchemaField;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.schemaregistry.services.SchemaRegistry;
+import org.apache.nifi.serialization.SimpleRecordSchema;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.util.DataTypeUtils;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+@Tags({"schema", "registry", "database", "table"})
+@CapabilityDescription("Provides a service for generating a record schema from a database table definition. The service is configured "
+ + "to use a table name and a database connection fetches the table metadata (i.e. table definition) such as column names, data types, "
+ + "nullability, etc.")
+public class DatabaseTableSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
+
+ private static final Set<SchemaField> schemaFields = EnumSet.of(SchemaField.SCHEMA_NAME);
+
+ static final PropertyDescriptor DBCP_SERVICE = new PropertyDescriptor.Builder()
+ .name("Database Connection Pooling Service")
+ .displayName("Database Connection Pooling Service")
+ .description("The Controller Service that is used to obtain a connection to the database for retrieving table information.")
+ .required(true)
+ .identifiesControllerService(DBCPService.class)
+ .build();
+
+ static final PropertyDescriptor CATALOG_NAME = new PropertyDescriptor.Builder()
+ .name("Catalog Name")
+ .displayName("Catalog Name")
+ .description("The name of the catalog used to locate the desired table. This may not apply for the database that you are querying. In this case, leave the field empty. Note that if the "
+ + "property is set and the database is case-sensitive, the catalog name must match the database's catalog name exactly.")
+ .required(false)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+
+ static final PropertyDescriptor SCHEMA_NAME = new PropertyDescriptor.Builder()
+ .name("Schema Name")
+ .displayName("Schema Name")
+ .description("The name of the schema that the table belongs to. This may not apply for the database that you are updating. In this case, leave the field empty. Note that if the "
+ + "property is set and the database is case-sensitive, the schema name must match the database's schema name exactly. Also notice that if the same table name exists in multiple "
+ + "schemas and Schema Name is not specified, the service will find those tables and give an error if the different tables have the same column name(s).")
+ .required(false)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+
+ protected List<PropertyDescriptor> propDescriptors = Collections.unmodifiableList(Arrays.asList(
+ DBCP_SERVICE,
+ CATALOG_NAME,
+ SCHEMA_NAME
+ ));
+
+ private volatile DBCPService dbcpService;
+ private volatile String dbCatalogName;
+ private volatile String dbSchemaName;
+
+ @Override
+ protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+ return propDescriptors;
+ }
+
+ @OnEnabled
+ public void onEnabled(final ConfigurationContext context) {
+ dbcpService = context.getProperty(DBCP_SERVICE).asControllerService(DBCPService.class);
+ dbCatalogName = context.getProperty(CATALOG_NAME).evaluateAttributeExpressions().getValue();
+ dbSchemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions().getValue();
+ }
+
+ @Override
+ public RecordSchema retrieveSchema(SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException {
+ if (schemaIdentifier.getName().isPresent()) {
+ return retrieveSchemaByName(schemaIdentifier);
+ } else {
+ throw new SchemaNotFoundException("This Schema Registry only supports retrieving a schema by name.");
+ }
+ }
+
+ @Override
+ public Set<SchemaField> getSuppliedSchemaFields() {
+ return schemaFields;
+ }
+
+ RecordSchema retrieveSchemaByName(final SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException {
+ final Optional<String> schemaName = schemaIdentifier.getName();
+ if (schemaName.isEmpty()) {
+ throw new SchemaNotFoundException("Cannot retrieve schema because Schema Name is not present");
+ }
+
+ final String tableName = schemaName.get();
+ try {
+ try (final Connection conn = dbcpService.getConnection()) {
+ final DatabaseMetaData databaseMetaData = conn.getMetaData();
+ return getRecordSchemaFromMetadata(databaseMetaData, tableName);
+ }
+ } catch (SQLException sqle) {
+ throw new IOException("Error retrieving schema for table " + schemaName.get(), sqle);
+ }
+ }
+
+ private RecordSchema getRecordSchemaFromMetadata(final DatabaseMetaData databaseMetaData, final String tableName) throws SQLException, SchemaNotFoundException {
+ try (final ResultSet columnResultSet = databaseMetaData.getColumns(dbCatalogName, dbSchemaName, tableName, "%")) {
+
+ final List<RecordField> recordFields = new ArrayList<>();
+ while (columnResultSet.next()) {
+ recordFields.add(createRecordFieldFromColumn(columnResultSet));
+ }
+
+ // If no columns are found, check that the table exists
+ if (recordFields.isEmpty()) {
+ checkTableExists(databaseMetaData, tableName);
+ }
+ return new SimpleRecordSchema(recordFields);
+ }
+ }
+
+ private RecordField createRecordFieldFromColumn(final ResultSet columnResultSet) throws SQLException {
+ // COLUMN_DEF must be read first to work around Oracle bug, see NIFI-4279 for details
+ final String defaultValue = columnResultSet.getString("COLUMN_DEF");
+ final String columnName = columnResultSet.getString("COLUMN_NAME");
+ final int dataType = columnResultSet.getInt("DATA_TYPE");
+ final String nullableValue = columnResultSet.getString("IS_NULLABLE");
+ final boolean isNullable = "YES".equalsIgnoreCase(nullableValue) || nullableValue.isEmpty();
+ return new RecordField(
+ columnName,
+ DataTypeUtils.getDataTypeFromSQLTypeValue(dataType),
+ defaultValue,
+ isNullable);
+ }
+
+ private void checkTableExists(final DatabaseMetaData databaseMetaData, final String tableName) throws SchemaNotFoundException, SQLException {
+ try (final ResultSet tblrs = databaseMetaData.getTables(dbCatalogName, dbSchemaName, tableName, null)) {
Review Comment:
Recommend renaming this variable for readability.
```suggestion
try (final ResultSet tables = databaseMetaData.getTables(dbCatalogName, dbSchemaName, tableName, null)) {
```
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/test/java/org/apache/nifi/db/schemaregistry/DatabaseTableSchemaRegistryTest.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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.db.schemaregistry;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.dbcp.ConnectionUrlValidator;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.dbcp.DriverClassValidator;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordFieldType;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.StandardSchemaIdentifier;
+import org.apache.nifi.util.NoOpProcessor;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.apache.nifi.util.file.FileUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.spy;
+
+public class DatabaseTableSchemaRegistryTest {
+
+ private static final List<String> CREATE_TABLE_STATEMENTS = Arrays.asList(
+ "CREATE TABLE PERSONS (id integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE SCHEMA1.PERSONS (id integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE SCHEMA2.PERSONS (id2 integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE UUID_TEST (id integer primary key, name VARCHAR(100))",
+ "CREATE TABLE LONGVARBINARY_TEST (id integer primary key, name LONG VARCHAR FOR BIT DATA)"
+ );
+
+ private static final String SERVICE_ID = DBCPServiceSimpleImpl.class.getName();
+
+ private final static String DB_LOCATION = "target/db_schema_reg";
+
+ // This is to mimic those in DBCPProperties to avoid adding the dependency to nifi-dbcp-base
+ private static final PropertyDescriptor DATABASE_URL = new PropertyDescriptor.Builder()
+ .name("Database Connection URL")
+ .addValidator(new ConnectionUrlValidator())
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private static final PropertyDescriptor DB_USER = new PropertyDescriptor.Builder()
+ .name("Database User")
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private static final PropertyDescriptor DB_PASSWORD = new PropertyDescriptor.Builder()
+ .name("Password")
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+ private static final PropertyDescriptor DB_DRIVERNAME = new PropertyDescriptor.Builder()
+ .name("Database Driver Class Name")
+ .addValidator(new DriverClassValidator())
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private TestRunner runner;
+
+ @BeforeAll
+ public static void setupDatabase() throws Exception {
+ System.setProperty("derby.stream.error.file", "target/derby.log");
+
+ // remove previous test database, if any
+ final File dbLocation = new File(DB_LOCATION);
+ try {
+ FileUtils.deleteFile(dbLocation, true);
+ } catch (IOException ignore) {
+ // Do nothing, may not have existed
+ }
+
+ // Mock the DBCP Controller Service so we can control the Results
+ DBCPService setupDbService = spy(new DBCPServiceSimpleImpl(DB_LOCATION));
+
+ final Connection conn = setupDbService.getConnection();
+ final Statement stmt = conn.createStatement();
+ for (String createTableStatement : CREATE_TABLE_STATEMENTS) {
+ stmt.execute(createTableStatement);
+ }
+
+ stmt.close();
+ conn.close();
+ }
+
+ @AfterAll
+ public static void shutdownDatabase() throws Exception {
Review Comment:
The `Exception` is not thrown and can be removed:
```suggestion
public static void shutdownDatabase() {
```
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/test/java/org/apache/nifi/db/schemaregistry/DatabaseTableSchemaRegistryTest.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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.db.schemaregistry;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.dbcp.ConnectionUrlValidator;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.dbcp.DriverClassValidator;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordFieldType;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.StandardSchemaIdentifier;
+import org.apache.nifi.util.NoOpProcessor;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.apache.nifi.util.file.FileUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.spy;
+
+public class DatabaseTableSchemaRegistryTest {
+
+ private static final List<String> CREATE_TABLE_STATEMENTS = Arrays.asList(
+ "CREATE TABLE PERSONS (id integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE SCHEMA1.PERSONS (id integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE SCHEMA2.PERSONS (id2 integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE UUID_TEST (id integer primary key, name VARCHAR(100))",
+ "CREATE TABLE LONGVARBINARY_TEST (id integer primary key, name LONG VARCHAR FOR BIT DATA)"
+ );
+
+ private static final String SERVICE_ID = DBCPServiceSimpleImpl.class.getName();
+
+ private final static String DB_LOCATION = "target/db_schema_reg";
+
+ // This is to mimic those in DBCPProperties to avoid adding the dependency to nifi-dbcp-base
+ private static final PropertyDescriptor DATABASE_URL = new PropertyDescriptor.Builder()
+ .name("Database Connection URL")
+ .addValidator(new ConnectionUrlValidator())
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private static final PropertyDescriptor DB_USER = new PropertyDescriptor.Builder()
+ .name("Database User")
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private static final PropertyDescriptor DB_PASSWORD = new PropertyDescriptor.Builder()
+ .name("Password")
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+ private static final PropertyDescriptor DB_DRIVERNAME = new PropertyDescriptor.Builder()
+ .name("Database Driver Class Name")
+ .addValidator(new DriverClassValidator())
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private TestRunner runner;
+
+ @BeforeAll
+ public static void setupDatabase() throws Exception {
+ System.setProperty("derby.stream.error.file", "target/derby.log");
+
+ // remove previous test database, if any
+ final File dbLocation = new File(DB_LOCATION);
+ try {
+ FileUtils.deleteFile(dbLocation, true);
+ } catch (IOException ignore) {
+ // Do nothing, may not have existed
+ }
+
+ // Mock the DBCP Controller Service so we can control the Results
+ DBCPService setupDbService = spy(new DBCPServiceSimpleImpl(DB_LOCATION));
+
+ final Connection conn = setupDbService.getConnection();
+ final Statement stmt = conn.createStatement();
+ for (String createTableStatement : CREATE_TABLE_STATEMENTS) {
+ stmt.execute(createTableStatement);
+ }
+
+ stmt.close();
+ conn.close();
+ }
+
+ @AfterAll
+ public static void shutdownDatabase() throws Exception {
+ try {
+ DriverManager.getConnection("jdbc:derby:" + DB_LOCATION + ";shutdown=true");
+ } catch (Exception ignore) {
+ // Do nothing, this is what happens at Derby shutdown
+ }
+ // remove previous test database, if any
+ final File dbLocation = new File(DB_LOCATION);
+ try {
+ FileUtils.deleteFile(dbLocation, true);
+ } catch (IOException ignore) {
+ // Do nothing, may not have existed
+ }
+ System.clearProperty("derby.stream.error.file");
+ }
+
+ @BeforeEach
+ public void setService() throws InitializationException {
+ runner = TestRunners.newTestRunner(NoOpProcessor.class);
+ DBCPService dbcp = new DBCPServiceSimpleImpl(DB_LOCATION);
+ runner.addControllerService(SERVICE_ID, dbcp);
+
+ final String url = String.format("jdbc:derby:%s;create=false", DB_LOCATION);
+ runner.setProperty(dbcp, DATABASE_URL, url);
+ runner.setProperty(dbcp, DB_USER, String.class.getSimpleName());
+ runner.setProperty(dbcp, DB_PASSWORD, String.class.getName());
+ runner.setProperty(dbcp, DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver");
+ runner.enableControllerService(dbcp);
+ }
+
+ @Test
+ public void testGetSchemaExists() throws Exception {
+ DatabaseTableSchemaRegistry dbSchemaRegistry = new DatabaseTableSchemaRegistry();
+ runner.addControllerService("schemaRegistry", dbSchemaRegistry);
+ runner.setProperty(dbSchemaRegistry, DatabaseTableSchemaRegistry.DBCP_SERVICE, SERVICE_ID);
+ runner.setProperty(dbSchemaRegistry, DatabaseTableSchemaRegistry.SCHEMA_NAME, "SCHEMA1");
+ runner.enableControllerService(dbSchemaRegistry);
+ SchemaIdentifier schemaIdentifier = new StandardSchemaIdentifier.Builder()
+ .name("PERSONS")
+ .build();
+ RecordSchema recordSchema = dbSchemaRegistry.retrieveSchema(schemaIdentifier);
+ assertNotNull(recordSchema);
+ Optional<RecordField> recordField = recordSchema.getField("ID");
+ assertTrue(recordField.isPresent());
+ assertEquals(RecordFieldType.INT.getDataType(), recordField.get().getDataType());
+ recordField = recordSchema.getField("NAME");
+ assertTrue(recordField.isPresent());
+ assertEquals(RecordFieldType.STRING.getDataType(), recordField.get().getDataType());
+ recordField = recordSchema.getField("CODE");
+ assertTrue(recordField.isPresent());
+ assertEquals(RecordFieldType.INT.getDataType(), recordField.get().getDataType());
+ recordField = recordSchema.getField("DT");
+ assertTrue(recordField.isPresent());
+ assertEquals(RecordFieldType.DATE.getDataType(), recordField.get().getDataType());
+ // Get nonexistent field
+ recordField = recordSchema.getField("NOT_A_FIELD");
+ assertFalse(recordField.isPresent());
+ }
+
+ @Test
+ public void testGetSchemaNotExists() throws Exception {
+ DatabaseTableSchemaRegistry dbSchemaRegistry = new DatabaseTableSchemaRegistry();
+ runner.addControllerService("schemaRegistry", dbSchemaRegistry);
+ runner.setProperty(dbSchemaRegistry, DatabaseTableSchemaRegistry.DBCP_SERVICE, SERVICE_ID);
+ runner.setProperty(dbSchemaRegistry, DatabaseTableSchemaRegistry.SCHEMA_NAME, "SCHEMA1");
+ runner.enableControllerService(dbSchemaRegistry);
+ SchemaIdentifier schemaIdentifier = new StandardSchemaIdentifier.Builder()
+ .name("NOT_A_TABLE")
+ .build();
+ assertThrows(SchemaNotFoundException.class, () -> dbSchemaRegistry.retrieveSchema(schemaIdentifier));
+ }
+
+ private static class DBCPServiceSimpleImpl extends AbstractControllerService implements DBCPService {
+
+ private final String databaseLocation;
+
+ public DBCPServiceSimpleImpl(final String databaseLocation) {
+ this.databaseLocation = databaseLocation;
+ }
+
+ @Override
+ public String getIdentifier() {
+ return this.getClass().getName();
+ }
+
+ @Override
+ public Connection getConnection() throws ProcessException {
+ try {
+ Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Review Comment:
Recommend declaring this class name once and reusing throughout the test class.
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/test/java/org/apache/nifi/db/schemaregistry/DatabaseTableSchemaRegistryTest.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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.db.schemaregistry;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.dbcp.ConnectionUrlValidator;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.dbcp.DriverClassValidator;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordFieldType;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.StandardSchemaIdentifier;
+import org.apache.nifi.util.NoOpProcessor;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.apache.nifi.util.file.FileUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.spy;
+
+public class DatabaseTableSchemaRegistryTest {
+
+ private static final List<String> CREATE_TABLE_STATEMENTS = Arrays.asList(
+ "CREATE TABLE PERSONS (id integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE SCHEMA1.PERSONS (id integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE SCHEMA2.PERSONS (id2 integer primary key, name varchar(100)," +
+ " code integer CONSTRAINT CODE_RANGE CHECK (code >= 0 AND code < 1000), dt date)",
+ "CREATE TABLE UUID_TEST (id integer primary key, name VARCHAR(100))",
+ "CREATE TABLE LONGVARBINARY_TEST (id integer primary key, name LONG VARCHAR FOR BIT DATA)"
+ );
+
+ private static final String SERVICE_ID = DBCPServiceSimpleImpl.class.getName();
+
+ private final static String DB_LOCATION = "target/db_schema_reg";
+
+ // This is to mimic those in DBCPProperties to avoid adding the dependency to nifi-dbcp-base
+ private static final PropertyDescriptor DATABASE_URL = new PropertyDescriptor.Builder()
+ .name("Database Connection URL")
+ .addValidator(new ConnectionUrlValidator())
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private static final PropertyDescriptor DB_USER = new PropertyDescriptor.Builder()
+ .name("Database User")
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private static final PropertyDescriptor DB_PASSWORD = new PropertyDescriptor.Builder()
+ .name("Password")
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+ private static final PropertyDescriptor DB_DRIVERNAME = new PropertyDescriptor.Builder()
+ .name("Database Driver Class Name")
+ .addValidator(new DriverClassValidator())
+ .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+ .build();
+
+ private TestRunner runner;
+
+ @BeforeAll
+ public static void setupDatabase() throws Exception {
+ System.setProperty("derby.stream.error.file", "target/derby.log");
+
+ // remove previous test database, if any
+ final File dbLocation = new File(DB_LOCATION);
+ try {
+ FileUtils.deleteFile(dbLocation, true);
+ } catch (IOException ignore) {
+ // Do nothing, may not have existed
+ }
+
+ // Mock the DBCP Controller Service so we can control the Results
+ DBCPService setupDbService = spy(new DBCPServiceSimpleImpl(DB_LOCATION));
+
+ final Connection conn = setupDbService.getConnection();
+ final Statement stmt = conn.createStatement();
+ for (String createTableStatement : CREATE_TABLE_STATEMENTS) {
+ stmt.execute(createTableStatement);
+ }
+
+ stmt.close();
+ conn.close();
+ }
+
+ @AfterAll
+ public static void shutdownDatabase() throws Exception {
+ try {
+ DriverManager.getConnection("jdbc:derby:" + DB_LOCATION + ";shutdown=true");
+ } catch (Exception ignore) {
+ // Do nothing, this is what happens at Derby shutdown
+ }
+ // remove previous test database, if any
+ final File dbLocation = new File(DB_LOCATION);
+ try {
+ FileUtils.deleteFile(dbLocation, true);
+ } catch (IOException ignore) {
+ // Do nothing, may not have existed
+ }
+ System.clearProperty("derby.stream.error.file");
+ }
+
+ @BeforeEach
+ public void setService() throws InitializationException {
+ runner = TestRunners.newTestRunner(NoOpProcessor.class);
+ DBCPService dbcp = new DBCPServiceSimpleImpl(DB_LOCATION);
+ runner.addControllerService(SERVICE_ID, dbcp);
+
+ final String url = String.format("jdbc:derby:%s;create=false", DB_LOCATION);
+ runner.setProperty(dbcp, DATABASE_URL, url);
+ runner.setProperty(dbcp, DB_USER, String.class.getSimpleName());
+ runner.setProperty(dbcp, DB_PASSWORD, String.class.getName());
+ runner.setProperty(dbcp, DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver");
+ runner.enableControllerService(dbcp);
+ }
+
+ @Test
+ public void testGetSchemaExists() throws Exception {
+ DatabaseTableSchemaRegistry dbSchemaRegistry = new DatabaseTableSchemaRegistry();
+ runner.addControllerService("schemaRegistry", dbSchemaRegistry);
+ runner.setProperty(dbSchemaRegistry, DatabaseTableSchemaRegistry.DBCP_SERVICE, SERVICE_ID);
+ runner.setProperty(dbSchemaRegistry, DatabaseTableSchemaRegistry.SCHEMA_NAME, "SCHEMA1");
+ runner.enableControllerService(dbSchemaRegistry);
+ SchemaIdentifier schemaIdentifier = new StandardSchemaIdentifier.Builder()
+ .name("PERSONS")
+ .build();
+ RecordSchema recordSchema = dbSchemaRegistry.retrieveSchema(schemaIdentifier);
+ assertNotNull(recordSchema);
+ Optional<RecordField> recordField = recordSchema.getField("ID");
+ assertTrue(recordField.isPresent());
+ assertEquals(RecordFieldType.INT.getDataType(), recordField.get().getDataType());
+ recordField = recordSchema.getField("NAME");
+ assertTrue(recordField.isPresent());
+ assertEquals(RecordFieldType.STRING.getDataType(), recordField.get().getDataType());
+ recordField = recordSchema.getField("CODE");
+ assertTrue(recordField.isPresent());
+ assertEquals(RecordFieldType.INT.getDataType(), recordField.get().getDataType());
+ recordField = recordSchema.getField("DT");
+ assertTrue(recordField.isPresent());
+ assertEquals(RecordFieldType.DATE.getDataType(), recordField.get().getDataType());
+ // Get nonexistent field
+ recordField = recordSchema.getField("NOT_A_FIELD");
+ assertFalse(recordField.isPresent());
+ }
+
+ @Test
+ public void testGetSchemaNotExists() throws Exception {
+ DatabaseTableSchemaRegistry dbSchemaRegistry = new DatabaseTableSchemaRegistry();
+ runner.addControllerService("schemaRegistry", dbSchemaRegistry);
+ runner.setProperty(dbSchemaRegistry, DatabaseTableSchemaRegistry.DBCP_SERVICE, SERVICE_ID);
+ runner.setProperty(dbSchemaRegistry, DatabaseTableSchemaRegistry.SCHEMA_NAME, "SCHEMA1");
+ runner.enableControllerService(dbSchemaRegistry);
+ SchemaIdentifier schemaIdentifier = new StandardSchemaIdentifier.Builder()
+ .name("NOT_A_TABLE")
+ .build();
+ assertThrows(SchemaNotFoundException.class, () -> dbSchemaRegistry.retrieveSchema(schemaIdentifier));
+ }
+
+ private static class DBCPServiceSimpleImpl extends AbstractControllerService implements DBCPService {
Review Comment:
Recommend avoiding the use of `Impl` on class names, even for testing.
```suggestion
private static class SimpleDBCPService extends AbstractControllerService implements DBCPService {
```
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/pom.xml:
##########
@@ -25,6 +25,7 @@
<modules>
<module>nifi-dbcp-service</module>
<module>nifi-hikari-dbcp-service</module>
+ <module>nifi-db-schema-registry-service</module>
Review Comment:
Although this uses Database Connection Pools, it does not seem like it fits in this bundle and NAR. As it relies only on standard service interfaces, it should be a small addition as a separate NAR. What do you think?
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "pvillard31 (via GitHub)" <gi...@apache.org>.
pvillard31 commented on PR #8042:
URL: https://github.com/apache/nifi/pull/8042#issuecomment-1816560628
I'm jumping in the conversation here but I don't think this is a very specific use case. The idea is that, in many cases, data would be sent into a database by NiFi and some users may not want to deal with schemas in NiFi or introduce an external Schema Registry. The idea here is to be able to retrieve the schema of the destination table in the NiFi flow so that it can be leveraged instead of the Infer Schema option which comes with the limitations and risks that we know.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "mattyb149 (via GitHub)" <gi...@apache.org>.
mattyb149 commented on code in PR #8042:
URL: https://github.com/apache/nifi/pull/8042#discussion_r1397657327
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/pom.xml:
##########
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-bundle</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>nifi-db-schema-registry-service</artifactId>
+ <packaging>jar</packaging>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-schema-registry-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-service-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-dbcp-base</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </dependency>
Review Comment:
It's only for the common properties. If there's a better spot to move those to I can refactor it as part of this.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #8042:
URL: https://github.com/apache/nifi/pull/8042#discussion_r1397676848
##########
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-db-schema-registry-service/src/main/java/org/apache/nifi/db/schemaregistry/DatabaseSchemaRegistry.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.db.schemaregistry;
+
+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.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.dbcp.DBCPService;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.schema.access.SchemaField;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.schemaregistry.services.SchemaRegistry;
+import org.apache.nifi.serialization.SimpleRecordSchema;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+import org.apache.nifi.serialization.record.util.DataTypeUtils;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+@Tags({"schema", "registry", "database", "table"})
+@CapabilityDescription("Provides a service for generating a record schema from a database table definition. The service is configured "
+ + "to use a table name and a database connection fetches the table metadata (i.e. table definition) such as column names, data types, "
+ + "nullability, etc.")
+public class DatabaseSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
+
+ private static final Set<SchemaField> schemaFields = EnumSet.of(SchemaField.SCHEMA_NAME);
+
+ static final PropertyDescriptor DBCP_SERVICE = new PropertyDescriptor.Builder()
+ .name("dbcp-service")
Review Comment:
Yes, it is newer with part of the changes for migrating properties in NiFi 2.0. I know there have been past discussions about internationalization, but unfortunately it appears there was never any significant progress in that direction. For now, the best approach seems to be using the same name in both places, and if there is ever momentum around internationalization, then we could revisit a migration strategy. Unfortunately the disconnect between property and display names right now makes it more difficult to translate from a UI to programmatic approach, so keeping the same names will help under the current circumstances.
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12382: Add DatabaseSchemaRegistry service [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory closed pull request #8042: NIFI-12382: Add DatabaseSchemaRegistry service
URL: https://github.com/apache/nifi/pull/8042
--
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.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org