mirror of https://github.com/apache/nifi.git
NIFI-10222 Added Apicurio Schema Registry Service
This closes #7913 Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
parent
309061ca2d
commit
21387316e3
|
@ -900,7 +900,12 @@ language governing permissions and limitations under the License. -->
|
|||
<version>2.0.0-SNAPSHOT</version>
|
||||
<type>zip</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-apicurio-nar</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<type>nar</type>
|
||||
</dependency>
|
||||
<!-- AspectJ library needed by the Java Agent used for native library loading (see bootstrap.conf) -->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>nifi-apicurio-bundle</artifactId>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nifi-apicurio-nar</artifactId>
|
||||
|
||||
<packaging>nar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-apicurio-schema-registry-service</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-standard-shared-nar</artifactId>
|
||||
<type>nar</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,202 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
resultAttributes of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed 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.
|
|
@ -0,0 +1,19 @@
|
|||
nifi-apicurio-nar
|
||||
Copyright 2014-2023 The Apache Software Foundation
|
||||
|
||||
This product includes software developed at
|
||||
The Apache Software Foundation (http://www.apache.org/).
|
||||
|
||||
**************************
|
||||
Apache Software License v2
|
||||
**************************
|
||||
|
||||
(ASLv2) Apache Avro
|
||||
The following NOTICE information applies:
|
||||
Apache Avro
|
||||
Copyright 2009-2017 The Apache Software Foundation
|
||||
|
||||
(ASLv2) Apache Commons CSV
|
||||
The following NOTICE information applies:
|
||||
Apache Commons CSV
|
||||
Copyright 2005-2016 The Apache Software Foundation
|
|
@ -0,0 +1,70 @@
|
|||
<?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-apicurio-bundle</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>nifi-apicurio-schema-registry-service</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-record</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-web-client-provider-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-schema-registry-service-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-ssl-context-service-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-proxy-configuration-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-avro-record-utils</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- Test dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-mock</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-web-client-provider-service</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* 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.apicurio.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.apicurio.schemaregistry.client.ApicurioSchemaRegistryClient;
|
||||
import org.apache.nifi.apicurio.schemaregistry.client.CachingSchemaRegistryClient;
|
||||
import org.apache.nifi.apicurio.schemaregistry.client.SchemaRegistryApiClient;
|
||||
import org.apache.nifi.apicurio.schemaregistry.client.SchemaRegistryClient;
|
||||
import org.apache.nifi.components.PropertyDescriptor;
|
||||
import org.apache.nifi.controller.AbstractControllerService;
|
||||
import org.apache.nifi.controller.ConfigurationContext;
|
||||
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.record.RecordSchema;
|
||||
import org.apache.nifi.serialization.record.SchemaIdentifier;
|
||||
import org.apache.nifi.web.client.provider.api.WebClientServiceProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Tags({"schema", "registry", "apicurio", "avro"})
|
||||
@CapabilityDescription("Provides a Schema Registry that interacts with the Apicurio Schema Registry so that those Schemas that are stored in the Apicurio Schema "
|
||||
+ "Registry can be used in NiFi. When a Schema is looked up by name by this registry, it will find a Schema in the Apicurio Schema Registry with their names.")
|
||||
public class ApicurioSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
|
||||
|
||||
private static final Set<SchemaField> schemaFields = EnumSet.of(SchemaField.SCHEMA_NAME, SchemaField.SCHEMA_TEXT,
|
||||
SchemaField.SCHEMA_TEXT_FORMAT, SchemaField.SCHEMA_IDENTIFIER, SchemaField.SCHEMA_VERSION);
|
||||
|
||||
static final PropertyDescriptor SCHEMA_REGISTRY_URL = new PropertyDescriptor.Builder()
|
||||
.name("Schema Registry URL")
|
||||
.displayName("Schema Registry URL")
|
||||
.description("The URL of the Schema Registry e.g. http://localhost:8080")
|
||||
.addValidator(StandardValidators.URL_VALIDATOR)
|
||||
.required(true)
|
||||
.build();
|
||||
static final PropertyDescriptor CACHE_SIZE = new PropertyDescriptor.Builder()
|
||||
.name("Cache Size")
|
||||
.displayName("Cache Size")
|
||||
.description("Specifies how many Schemas should be cached from the Schema Registry. The cache size must be "
|
||||
+ "a non-negative integer. When it is set to 0, the cache is effectively disabled.")
|
||||
.addValidator(StandardValidators.NON_NEGATIVE_INTEGER_VALIDATOR)
|
||||
.defaultValue("1000")
|
||||
.required(true)
|
||||
.build();
|
||||
|
||||
static final PropertyDescriptor CACHE_EXPIRATION = new PropertyDescriptor.Builder()
|
||||
.name("Cache Expiration")
|
||||
.displayName("Cache Expiration")
|
||||
.description("Specifies how long a Schema that is cached should remain in the cache. Once this time period elapses, a "
|
||||
+ "cached version of a schema will no longer be used, and the service will have to communicate with the "
|
||||
+ "Schema Registry again in order to obtain the schema.")
|
||||
.addValidator(StandardValidators.TIME_PERIOD_VALIDATOR)
|
||||
.defaultValue("1 hour")
|
||||
.required(true)
|
||||
.build();
|
||||
public static final PropertyDescriptor WEB_CLIENT_PROVIDER = new PropertyDescriptor.Builder()
|
||||
.name("Web Client Service Provider")
|
||||
.displayName("Web Client Service Provider")
|
||||
.description("Controller service for HTTP client operations")
|
||||
.required(true)
|
||||
.identifiesControllerService(WebClientServiceProvider.class)
|
||||
.build();
|
||||
|
||||
private static final List<PropertyDescriptor> PROPERTIES = List.of(
|
||||
SCHEMA_REGISTRY_URL,
|
||||
CACHE_SIZE,
|
||||
CACHE_EXPIRATION,
|
||||
WEB_CLIENT_PROVIDER
|
||||
);
|
||||
|
||||
|
||||
@Override
|
||||
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
|
||||
return PROPERTIES;
|
||||
}
|
||||
|
||||
private volatile SchemaRegistryClient client;
|
||||
|
||||
@OnEnabled
|
||||
public void onEnabled(final ConfigurationContext context) {
|
||||
final String schemaRegistryUrl = context.getProperty(SCHEMA_REGISTRY_URL).getValue();
|
||||
final int cacheSize = context.getProperty(CACHE_SIZE).asInteger();
|
||||
final long cacheExpiration = context.getProperty(CACHE_EXPIRATION).asTimePeriod(TimeUnit.NANOSECONDS);
|
||||
|
||||
final WebClientServiceProvider webClientServiceProvider =
|
||||
context.getProperty(WEB_CLIENT_PROVIDER).asControllerService(WebClientServiceProvider.class);
|
||||
|
||||
final SchemaRegistryApiClient apiClient = new SchemaRegistryApiClient(webClientServiceProvider, schemaRegistryUrl);
|
||||
final SchemaRegistryClient schemaRegistryClient = new ApicurioSchemaRegistryClient(apiClient);
|
||||
client = new CachingSchemaRegistryClient(schemaRegistryClient, cacheSize, cacheExpiration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecordSchema retrieveSchema(SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException {
|
||||
final String schemaName = schemaIdentifier.getName().orElseThrow(
|
||||
() -> new SchemaNotFoundException("Cannot retrieve schema because Schema Name is not present")
|
||||
);
|
||||
final OptionalInt version = schemaIdentifier.getVersion();
|
||||
|
||||
if (version.isPresent()) {
|
||||
return client.getSchema(schemaName, version.getAsInt());
|
||||
} else {
|
||||
return client.getSchema(schemaName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<SchemaField> getSuppliedSchemaFields() {
|
||||
return schemaFields;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.client;
|
||||
|
||||
import org.apache.nifi.apicurio.schemaregistry.util.SchemaUtils;
|
||||
import org.apache.nifi.apicurio.schemaregistry.util.SchemaUtils.ResultAttributes;
|
||||
import org.apache.nifi.schema.access.SchemaNotFoundException;
|
||||
import org.apache.nifi.serialization.record.RecordSchema;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
|
||||
public class ApicurioSchemaRegistryClient implements SchemaRegistryClient {
|
||||
private final SchemaRegistryApiClient apiClient;
|
||||
|
||||
public ApicurioSchemaRegistryClient(SchemaRegistryApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecordSchema getSchema(final String schemaName) throws IOException, SchemaNotFoundException {
|
||||
final ResultAttributes attributes = getAttributesForSchemaName(schemaName);
|
||||
final int version = getVersionAttributeFromMetadata(attributes);
|
||||
return createRecordSchemaForAttributes(attributes, version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecordSchema getSchema(final String schemaName, final int version) throws IOException, SchemaNotFoundException {
|
||||
final ResultAttributes attributes = getAttributesForSchemaName(schemaName);
|
||||
return createRecordSchemaForAttributes(attributes, version);
|
||||
}
|
||||
|
||||
private ResultAttributes getAttributesForSchemaName(String schemaName) throws IOException {
|
||||
final URI searchUri = apiClient.buildSearchUri(schemaName);
|
||||
try (final InputStream searchResultStream = apiClient.retrieveResponse(searchUri)) {
|
||||
return SchemaUtils.getResultAttributes(searchResultStream);
|
||||
}
|
||||
}
|
||||
|
||||
private int getVersionAttributeFromMetadata(final ResultAttributes attributes) throws IOException {
|
||||
final URI metaDataUri = apiClient.buildMetaDataUri(attributes.groupId(), attributes.artifactId());
|
||||
try (final InputStream metadataResultStream = apiClient.retrieveResponse(metaDataUri)) {
|
||||
return SchemaUtils.extractVersionAttributeFromStream(metadataResultStream);
|
||||
}
|
||||
}
|
||||
|
||||
private RecordSchema createRecordSchemaForAttributes(ResultAttributes attributes, int version) throws IOException, SchemaNotFoundException {
|
||||
final URI schemaUri = apiClient.buildSchemaVersionUri(attributes.groupId(), attributes.artifactId(), version);
|
||||
|
||||
try (final InputStream schemaResultStream = apiClient.retrieveResponse(schemaUri)) {
|
||||
return SchemaUtils.createRecordSchema(schemaResultStream, attributes.name(), version);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.client;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.nifi.serialization.record.RecordSchema;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public class CachingSchemaRegistryClient implements SchemaRegistryClient {
|
||||
private final SchemaRegistryClient client;
|
||||
private final LoadingCache<Pair<String, Integer>, RecordSchema> schemaCache;
|
||||
|
||||
public CachingSchemaRegistryClient(final SchemaRegistryClient toWrap, final int cacheSize, final long expirationNanos) {
|
||||
this.client = toWrap;
|
||||
|
||||
schemaCache = Caffeine.newBuilder()
|
||||
.maximumSize(cacheSize)
|
||||
.expireAfterWrite(Duration.ofNanos(expirationNanos))
|
||||
.build(key -> {
|
||||
if (key.getRight() == -1) {
|
||||
// If the version in the key is -1, fetch the schema by name only.
|
||||
return client.getSchema(key.getLeft());
|
||||
} else {
|
||||
// If a specific version is provided in the key, fetch the schema with that version.
|
||||
return client.getSchema(key.getLeft(), key.getRight());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecordSchema getSchema(final String schemaName) {
|
||||
return schemaCache.get(Pair.of(schemaName, -1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecordSchema getSchema(final String schemaName, final int version) {
|
||||
return schemaCache.get(Pair.of(schemaName, version));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.client;
|
||||
|
||||
import org.apache.nifi.web.client.api.HttpUriBuilder;
|
||||
import org.apache.nifi.web.client.provider.api.WebClientServiceProvider;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
|
||||
public class SchemaRegistryApiClient {
|
||||
|
||||
private final WebClientServiceProvider webClientServiceProvider;
|
||||
private final String baseUrl;
|
||||
|
||||
public SchemaRegistryApiClient(final WebClientServiceProvider webClientServiceProvider, final String baseUrl) {
|
||||
this.webClientServiceProvider = webClientServiceProvider;
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
public InputStream retrieveResponse(final URI uri) {
|
||||
return webClientServiceProvider.getWebClientService()
|
||||
.get()
|
||||
.uri(uri)
|
||||
.retrieve()
|
||||
.body();
|
||||
}
|
||||
|
||||
public HttpUriBuilder buildBaseUri() {
|
||||
final URI uri = URI.create(baseUrl);
|
||||
return webClientServiceProvider.getHttpUriBuilder()
|
||||
.scheme(uri.getScheme())
|
||||
.host(uri.getHost())
|
||||
.port(uri.getPort())
|
||||
.addPathSegment("apis")
|
||||
.addPathSegment("registry")
|
||||
.addPathSegment("v2");
|
||||
}
|
||||
|
||||
public URI buildSearchUri(final String schemaName) {
|
||||
return buildBaseUri()
|
||||
.addPathSegment("search")
|
||||
.addPathSegment("artifacts")
|
||||
.addQueryParameter("name", schemaName)
|
||||
.addQueryParameter("limit", "1")
|
||||
.build();
|
||||
}
|
||||
|
||||
public URI buildMetaDataUri(final String groupId, final String artifactId) {
|
||||
return buildGroupArtifactsUri(groupId, artifactId)
|
||||
.addPathSegment("meta")
|
||||
.build();
|
||||
}
|
||||
|
||||
public URI buildSchemaUri(final String groupId, final String artifactId) {
|
||||
return buildGroupArtifactsUri(groupId, artifactId).build();
|
||||
}
|
||||
|
||||
public URI buildSchemaVersionUri(final String groupId, final String artifactId, final int version) {
|
||||
return buildGroupArtifactsUri(groupId, artifactId)
|
||||
.addPathSegment("versions")
|
||||
.addPathSegment(String.valueOf(version))
|
||||
.build();
|
||||
}
|
||||
|
||||
private HttpUriBuilder buildGroupArtifactsUri(final String groupId, final String artifactId) {
|
||||
return buildBaseUri()
|
||||
.addPathSegment("groups")
|
||||
.addPathSegment(groupId)
|
||||
.addPathSegment("artifacts")
|
||||
.addPathSegment(artifactId);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.client;
|
||||
|
||||
import org.apache.nifi.schema.access.SchemaNotFoundException;
|
||||
import org.apache.nifi.serialization.record.RecordSchema;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface SchemaRegistryClient {
|
||||
|
||||
RecordSchema getSchema(final String schemaName) throws IOException, SchemaNotFoundException;
|
||||
|
||||
RecordSchema getSchema(final String schemaName, final int version) throws IOException, SchemaNotFoundException;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.avro.Schema;
|
||||
import org.apache.avro.SchemaParseException;
|
||||
import org.apache.nifi.avro.AvroTypeUtil;
|
||||
import org.apache.nifi.schema.access.SchemaNotFoundException;
|
||||
import org.apache.nifi.serialization.record.RecordSchema;
|
||||
import org.apache.nifi.serialization.record.SchemaIdentifier;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
|
||||
public class SchemaUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SchemaUtils.class);
|
||||
private static final ObjectMapper OBJECT_MAPPER = setObjectMapper();
|
||||
|
||||
private SchemaUtils() {
|
||||
}
|
||||
|
||||
public static RecordSchema createRecordSchema(final InputStream schemaStream, final String name, final int version) throws SchemaNotFoundException, IOException {
|
||||
final JsonNode schemaNode = OBJECT_MAPPER.readTree(schemaStream);
|
||||
final String schemaText = schemaNode.toString();
|
||||
|
||||
try {
|
||||
final Schema avroSchema = new Schema.Parser().parse(schemaText);
|
||||
final SchemaIdentifier schemaId = SchemaIdentifier.builder()
|
||||
.name(name)
|
||||
.version(version)
|
||||
.build();
|
||||
return AvroTypeUtil.createSchema(avroSchema, schemaText, schemaId);
|
||||
} catch (final SchemaParseException e) {
|
||||
final String errorMessage = String.format("Obtained Schema with name [%s] from Apicurio Schema Registry but the Schema Text " +
|
||||
"that was returned is not a valid Avro Schema", name);
|
||||
throw new SchemaNotFoundException(errorMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static int extractVersionAttributeFromStream(InputStream in) {
|
||||
final JsonNode metadataNode;
|
||||
try {
|
||||
metadataNode = OBJECT_MAPPER.readTree(in);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Failed to read version from HTTP response stream", e);
|
||||
}
|
||||
return Integer.parseInt(metadataNode.get("version").asText());
|
||||
}
|
||||
|
||||
public static ResultAttributes getResultAttributes(InputStream in) {
|
||||
final JsonNode jsonNode;
|
||||
try {
|
||||
jsonNode = OBJECT_MAPPER.readTree(in);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Failed to read result attributes from HTTP response stream", e);
|
||||
}
|
||||
final JsonNode artifactNode = jsonNode.get("artifacts").get(0);
|
||||
final String groupId = artifactNode.get("groupId").asText();
|
||||
final String artifactId = artifactNode.get("id").asText();
|
||||
final String name = artifactNode.get("name").asText();
|
||||
return new ResultAttributes(groupId, artifactId, name);
|
||||
}
|
||||
|
||||
public record ResultAttributes(String groupId, String artifactId, String name) {
|
||||
}
|
||||
|
||||
static ObjectMapper setObjectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
# 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.
|
||||
|
||||
org.apache.nifi.apicurio.schemaregistry.ApicurioSchemaRegistry
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.client;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.nifi.schema.access.SchemaNotFoundException;
|
||||
import org.apache.nifi.serialization.record.RecordSchema;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ApicurioSchemaRegistryClientTest {
|
||||
|
||||
private static final String TEST_URL = "http://test.apicurio-schema-registry.com:8888";
|
||||
private static final String SCHEMA_NAME = "schema1";
|
||||
private static final int VERSION = 3;
|
||||
private static final String SEARCH_URL = TEST_URL + "/search";
|
||||
private static final String METADATA_URL = TEST_URL + "/meta";
|
||||
private static final String SCHEMA_VERSION_URL = TEST_URL + "/schema/versions/" + VERSION;
|
||||
private static final String GROUP_ID = "groupId1";
|
||||
private static final String ARTIFACT_ID = "artifactId1";
|
||||
@Mock
|
||||
private SchemaRegistryApiClient apiClient;
|
||||
private ApicurioSchemaRegistryClient schemaRegistryClient;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
doReturn(URI.create(SEARCH_URL)).when(apiClient).buildSearchUri(SCHEMA_NAME);
|
||||
doReturn(URI.create(SCHEMA_VERSION_URL)).when(apiClient).buildSchemaVersionUri(GROUP_ID, ARTIFACT_ID, VERSION);
|
||||
doReturn(getResource("search_response.json")).when(apiClient).retrieveResponse(URI.create(SEARCH_URL));
|
||||
doReturn(getResource("schema_response.json")).when(apiClient).retrieveResponse(URI.create(SCHEMA_VERSION_URL));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSchemaWithNameInvokesApiClientAndReturnsRecordSchema() throws IOException, SchemaNotFoundException {
|
||||
doReturn(URI.create(METADATA_URL)).when(apiClient).buildMetaDataUri(GROUP_ID, ARTIFACT_ID);
|
||||
doReturn(getResource("metadata_response.json")).when(apiClient).retrieveResponse(URI.create(METADATA_URL));
|
||||
|
||||
schemaRegistryClient = new ApicurioSchemaRegistryClient(apiClient);
|
||||
|
||||
final RecordSchema schema = schemaRegistryClient.getSchema(SCHEMA_NAME);
|
||||
|
||||
verify(apiClient).buildSearchUri(SCHEMA_NAME);
|
||||
verify(apiClient).buildMetaDataUri(GROUP_ID, ARTIFACT_ID);
|
||||
verify(apiClient).buildSchemaVersionUri(GROUP_ID, ARTIFACT_ID, VERSION);
|
||||
|
||||
final String expectedSchemaText = IOUtils.toString(getResource("schema_response.json"), Charset.defaultCharset())
|
||||
.replace("\n", "")
|
||||
.replaceAll(" +", "");
|
||||
assertEquals(expectedSchemaText, schema.getSchemaText().orElseThrow(() -> new AssertionError("Schema Text is empty")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSchemaWithNameAndVersionInvokesApiClientAndReturnsRecordSchema() throws IOException, SchemaNotFoundException {
|
||||
schemaRegistryClient = new ApicurioSchemaRegistryClient(apiClient);
|
||||
|
||||
final RecordSchema schema = schemaRegistryClient.getSchema(SCHEMA_NAME, 3);
|
||||
|
||||
verify(apiClient).buildSearchUri(SCHEMA_NAME);
|
||||
verify(apiClient, never()).buildMetaDataUri(GROUP_ID, ARTIFACT_ID);
|
||||
verify(apiClient).buildSchemaVersionUri(GROUP_ID, ARTIFACT_ID, VERSION);
|
||||
|
||||
final String expectedSchemaText = IOUtils.toString(getResource("schema_response.json"), Charset.defaultCharset())
|
||||
.replace("\n", "")
|
||||
.replaceAll(" +", "");
|
||||
assertEquals(expectedSchemaText, schema.getSchemaText().orElseThrow(() -> new AssertionError("Schema Text is empty")));
|
||||
}
|
||||
|
||||
private InputStream getResource(final String resourceName) {
|
||||
return this.getClass().getClassLoader().getResourceAsStream(resourceName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.client;
|
||||
|
||||
import org.apache.nifi.schema.access.SchemaNotFoundException;
|
||||
import org.apache.nifi.serialization.SimpleRecordSchema;
|
||||
import org.apache.nifi.serialization.record.RecordField;
|
||||
import org.apache.nifi.serialization.record.RecordFieldType;
|
||||
import org.apache.nifi.serialization.record.RecordSchema;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CachingSchemaRegistryClientTest {
|
||||
private static final String SCHEMA_NAME = "schema";
|
||||
|
||||
private static final RecordSchema TEST_SCHEMA = new SimpleRecordSchema(Arrays.asList(
|
||||
new RecordField("fieldName1", RecordFieldType.INT.getDataType()),
|
||||
new RecordField("fieldName2", RecordFieldType.STRING.getDataType())
|
||||
));
|
||||
|
||||
private static final RecordSchema TEST_SCHEMA_2 = new SimpleRecordSchema(Arrays.asList(
|
||||
new RecordField("fieldName3", RecordFieldType.INT.getDataType()),
|
||||
new RecordField("fieldName4", RecordFieldType.STRING.getDataType())
|
||||
));
|
||||
|
||||
@Mock
|
||||
private SchemaRegistryClient mockClient;
|
||||
private CachingSchemaRegistryClient cachingClient;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
cachingClient = new CachingSchemaRegistryClient(mockClient, 100, TimeUnit.SECONDS.toNanos(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSchemaWithNameInvokesClientAndCacheResult() throws IOException, SchemaNotFoundException {
|
||||
when(mockClient.getSchema(SCHEMA_NAME)).thenReturn(TEST_SCHEMA);
|
||||
|
||||
RecordSchema actualSchema1 = cachingClient.getSchema(SCHEMA_NAME);
|
||||
RecordSchema actualSchema2 = cachingClient.getSchema(SCHEMA_NAME);
|
||||
|
||||
assertEquals(TEST_SCHEMA, actualSchema1);
|
||||
assertEquals(TEST_SCHEMA, actualSchema2);
|
||||
verify(mockClient).getSchema(SCHEMA_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSchemaWithNameAndVersionInvokesClientAndCacheResult() throws IOException, SchemaNotFoundException {
|
||||
String schemaName = "schema";
|
||||
int version = 1;
|
||||
|
||||
when(mockClient.getSchema(schemaName, version)).thenReturn(TEST_SCHEMA);
|
||||
|
||||
RecordSchema actualSchema1 = cachingClient.getSchema(schemaName, version);
|
||||
RecordSchema actualSchema2 = cachingClient.getSchema(schemaName, version);
|
||||
|
||||
assertEquals(TEST_SCHEMA, actualSchema1);
|
||||
assertEquals(TEST_SCHEMA, actualSchema2);
|
||||
verify(mockClient).getSchema(schemaName, version);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSchemaWithNameAndVersionDoesNotCacheDifferentVersions() throws IOException, SchemaNotFoundException {
|
||||
int version1 = 1;
|
||||
int version2 = 2;
|
||||
RecordSchema expectedSchema1 = TEST_SCHEMA;
|
||||
RecordSchema expectedSchema2 = TEST_SCHEMA_2;
|
||||
|
||||
when(mockClient.getSchema(SCHEMA_NAME, version1)).thenReturn(expectedSchema1);
|
||||
when(mockClient.getSchema(SCHEMA_NAME, version2)).thenReturn(expectedSchema2);
|
||||
|
||||
RecordSchema actualSchema1 = cachingClient.getSchema(SCHEMA_NAME, version1);
|
||||
RecordSchema actualSchema2 = cachingClient.getSchema(SCHEMA_NAME, version2);
|
||||
|
||||
assertEquals(expectedSchema1, actualSchema1);
|
||||
assertEquals(expectedSchema2, actualSchema2);
|
||||
verify(mockClient).getSchema(SCHEMA_NAME, version1);
|
||||
verify(mockClient).getSchema(SCHEMA_NAME, version2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.client;
|
||||
|
||||
import org.apache.nifi.web.client.StandardHttpUriBuilder;
|
||||
import org.apache.nifi.web.client.api.HttpUriBuilder;
|
||||
import org.apache.nifi.web.client.provider.api.WebClientServiceProvider;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SchemaRegistryApiClientTest {
|
||||
|
||||
private static final String BASE_URL = "http://test.apicurio-schema-registry.com:8888";
|
||||
private static final String API_PATH = "/apis/registry/v2";
|
||||
private static final String METADATA_PATH = "/meta";
|
||||
private static final String GROUP_ID = "groupId1";
|
||||
private static final String ARTIFACT_ID = "artifactId1";
|
||||
private static final String SCHEMA_PATH = String.format("/groups/%s/artifacts/%s", GROUP_ID, ARTIFACT_ID);
|
||||
private static final String SCHEMA_NAME = "schema1";
|
||||
private static final String SEARCH_PATH = String.format("/search/artifacts?name=%s&limit=1", SCHEMA_NAME);
|
||||
|
||||
@Mock
|
||||
private WebClientServiceProvider webClientServiceProvider;
|
||||
private SchemaRegistryApiClient client;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
doReturn(new StandardHttpUriBuilder()).when(webClientServiceProvider).getHttpUriBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildBaseUrl() {
|
||||
client = new SchemaRegistryApiClient(webClientServiceProvider, BASE_URL);
|
||||
|
||||
final HttpUriBuilder httpUriBuilder = client.buildBaseUri();
|
||||
|
||||
assertEquals(BASE_URL + API_PATH, httpUriBuilder.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildSearchUri() {
|
||||
client = new SchemaRegistryApiClient(webClientServiceProvider, BASE_URL);
|
||||
|
||||
final URI uri = client.buildSearchUri(SCHEMA_NAME);
|
||||
|
||||
assertEquals(BASE_URL + API_PATH + SEARCH_PATH, uri.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildMetadataUri() {
|
||||
client = new SchemaRegistryApiClient(webClientServiceProvider, BASE_URL);
|
||||
|
||||
final URI uri = client.buildMetaDataUri(GROUP_ID, ARTIFACT_ID);
|
||||
|
||||
assertEquals(BASE_URL + API_PATH + SCHEMA_PATH + METADATA_PATH, uri.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildSchemaUri() {
|
||||
client = new SchemaRegistryApiClient(webClientServiceProvider, BASE_URL);
|
||||
|
||||
final URI uri = client.buildSchemaUri(GROUP_ID, ARTIFACT_ID);
|
||||
|
||||
assertEquals(BASE_URL + API_PATH + SCHEMA_PATH, uri.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* 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.apicurio.schemaregistry.util;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.nifi.apicurio.schemaregistry.util.SchemaUtils.ResultAttributes;
|
||||
import org.apache.nifi.schema.access.SchemaNotFoundException;
|
||||
import org.apache.nifi.serialization.record.RecordSchema;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class SchemaUtilsTest {
|
||||
|
||||
@Test
|
||||
void testCreateRecordSchema() throws SchemaNotFoundException, IOException {
|
||||
final InputStream in = getResource("schema_response.json");
|
||||
|
||||
final RecordSchema schema = SchemaUtils.createRecordSchema(in, "schema1", 3);
|
||||
|
||||
assertEquals("schema1", schema.getSchemaName().orElseThrow(() -> new AssertionError("Schema Name is empty")));
|
||||
assertEquals("schema_namespace_1", schema.getSchemaNamespace().orElseThrow(() -> new AssertionError("Schema Namespace is empty")));
|
||||
assertEquals("avro", schema.getSchemaFormat().orElseThrow(() -> new AssertionError("Schema Format is empty")));
|
||||
|
||||
final String expectedSchemaText = IOUtils.toString(getResource("schema_response.json"), Charset.defaultCharset())
|
||||
.replace("\n", "")
|
||||
.replaceAll(" +", "");
|
||||
assertEquals(expectedSchemaText, schema.getSchemaText().orElseThrow(() -> new AssertionError("Schema Text is empty")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetVersionAttribute() {
|
||||
final InputStream in = getResource("metadata_response.json");
|
||||
|
||||
int version = SchemaUtils.extractVersionAttributeFromStream(in);
|
||||
|
||||
assertEquals(3, version);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetResultAttributes() {
|
||||
final InputStream in = getResource("search_response.json");
|
||||
|
||||
final ResultAttributes resultAttributes = SchemaUtils.getResultAttributes(in);
|
||||
|
||||
final ResultAttributes expectedAttributes = new ResultAttributes("groupId1", "artifactId1", "schema1");
|
||||
assertEquals(expectedAttributes, resultAttributes);
|
||||
}
|
||||
|
||||
private InputStream getResource(final String resourceName) {
|
||||
return this.getClass().getClassLoader().getResourceAsStream(resourceName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "schema1",
|
||||
"createdBy": "",
|
||||
"createdOn": "2023-10-16T14:19:21+0000",
|
||||
"modifiedBy": "",
|
||||
"modifiedOn": "2023-10-16T14:53:12+0000",
|
||||
"id": "artifactId1",
|
||||
"version": "3",
|
||||
"type": "AVRO",
|
||||
"globalId": 3,
|
||||
"state": "ENABLED",
|
||||
"groupId": "groupId1",
|
||||
"contentId": 2,
|
||||
"references": []
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"type": "record",
|
||||
"namespace": "schema_namespace_1",
|
||||
"name": "schema1",
|
||||
"fields": [
|
||||
{
|
||||
"name": "name",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"name": "age",
|
||||
"type": "int"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"artifacts": [
|
||||
{
|
||||
"id": "artifactId1",
|
||||
"name": "schema1",
|
||||
"createdOn": "2023-10-16T14:19:21+0000",
|
||||
"createdBy": "",
|
||||
"type": "AVRO",
|
||||
"state": "ENABLED",
|
||||
"modifiedOn": "2023-10-16T14:19:31+0000",
|
||||
"modifiedBy": "",
|
||||
"groupId": "groupId1"
|
||||
}
|
||||
],
|
||||
"count": 1
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>nifi-standard-shared-bom</artifactId>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<relativePath>../nifi-standard-shared-bundle/nifi-standard-shared-bom</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nifi-apicurio-bundle</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>nifi-apicurio-nar</module>
|
||||
<module>nifi-apicurio-schema-registry-service</module>
|
||||
</modules>
|
||||
</project>
|
|
@ -115,6 +115,7 @@
|
|||
<module>nifi-py4j-bundle</module>
|
||||
<module>nifi-compress-bundle</module>
|
||||
<module>nifi-opentelemetry-bundle</module>
|
||||
<module>nifi-apicurio-bundle</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
|
Loading…
Reference in New Issue