lkuchars commented on code in PR #10105:
URL: https://github.com/apache/nifi/pull/10105#discussion_r2276438851
##########
nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-schema-registry-service/src/main/java/org/apache/nifi/confluent/schemaregistry/client/RestSchemaRegistryClient.java:
##########
@@ -211,6 +220,91 @@ public RecordSchema getSchema(final int schemaId) throws
IOException, SchemaNotF
return createRecordSchema(completeSchema);
}
+ @Override
+ public SchemaDefinition getSchemaDefinition(SchemaIdentifier identifier)
throws SchemaNotFoundException {
+ final JsonNode schemaJson;
+ String subject = null;
+ Integer version = null;
+
+ // If we have an ID, get the schema by ID first
+ // Using schemaVersionId, because that is what is set by
ConfluentEncodedSchemaReferenceReader.
+ // probably identifier field should be used, but I'm not changing
ConfluentEncodedSchemaReferenceReader for backward compatibility reasons.
+ if (identifier.getSchemaVersionId().isPresent()) {
+ long schemaId = identifier.getSchemaVersionId().getAsLong();
+ String schemaPath = getSchemaPath(schemaId);
+ schemaJson = fetchJsonResponse(schemaPath, "id " + schemaId);
+ } else if (identifier.getName().isPresent()) {
+ // If we have a name or (name and version), get the schema by those
+ subject = identifier.getName().get();
+ version = identifier.getVersion().isPresent() ?
identifier.getVersion().getAsInt() : null;
+ // if no version was specified, the latest version will be used.
See @getSubjectPath method.
+ String pathSuffix = getSubjectPath(subject, version);
+ schemaJson = fetchJsonResponse(pathSuffix, "name " + subject);
+ } else {
+ throw new SchemaNotFoundException("Schema identifier must contain
either a version identifier or a subject name");
+ }
+
+ // Extract schema information
+ String schemaText = schemaJson.get(SCHEMA_TEXT_FIELD_NAME).asText();
+ String schemaTypeText =
schemaJson.get(SCHEMA_TYPE_FIELD_NAME).asText();
+ SchemaType schemaType = toSchemaType(schemaTypeText);
+
+ long schemaId;
+ if (schemaJson.has(ID_FIELD_NAME)) {
+ schemaId = schemaJson.get(ID_FIELD_NAME).asLong();
+ } else {
+ schemaId = identifier.getSchemaVersionId().getAsLong();
+ }
+
+ if (subject == null && schemaJson.has(SUBJECT_FIELD_NAME)) {
+ subject = schemaJson.get(SUBJECT_FIELD_NAME).asText();
+ }
+
+ if (version == null && schemaJson.has(VERSION_FIELD_NAME)) {
+ version = schemaJson.get(VERSION_FIELD_NAME).asInt();
+ }
+
+ // Build schema identifier with all available information
+ SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder()
+ .id(schemaId)
+ .name(subject)
+ .version(version)
+ .build();
+
+ // Process references if present
+ Map<String, SchemaDefinition> references = new HashMap<>();
+ if (schemaJson.has(REFERENCES_FIELD_NAME) &&
!schemaJson.get(REFERENCES_FIELD_NAME).isNull()) {
+ ArrayNode refsArray = (ArrayNode)
schemaJson.get(REFERENCES_FIELD_NAME);
+ for (JsonNode ref : refsArray) {
+ String refName = ref.get(REFERENCE_NAME_FIELD_NAME).asText();
+ String refSubject =
ref.get(REFERENCE_SUBJECT_FIELD_NAME).asText();
+ int refVersion = ref.get(REFERENCE_VERSION_FIELD_NAME).asInt();
+
+ // Recursively get referenced schema
+ SchemaIdentifier refId = SchemaIdentifier.builder()
+ .name(refSubject)
+ .version(refVersion)
+ .build();
+ SchemaDefinition refSchema = getSchemaDefinition(refId);
+ references.put(refName, refSchema);
+ }
+ }
+
+ return new StandardSchemaDefinition(schemaIdentifier, schemaText,
schemaType, references);
+ }
+
+ private SchemaType toSchemaType(final String schemaTypeText) {
+ try {
+ if (schemaTypeText == null || schemaTypeText.isEmpty()) {
+ return SchemaType.AVRO; // Default schema type for confluent
schema registry is AVRO.
+ }
+ return SchemaType.valueOf(schemaTypeText.toUpperCase().trim());
+ } catch (final Exception e) {
+ final String message = String.format("Could not convert schema
type '%s' to SchemaType enum.", schemaTypeText);
Review Comment:
Done
##########
nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-protobuf-message-name-resolver/src/main/java/org/apache/nifi/confluent/schemaregistry/ConfluentProtobufMessageNameResolver.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.confluent.schemaregistry;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnDisabled;
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.confluent.schema.AntlrProtobufMessageSchemaParser;
+import org.apache.nifi.confluent.schema.ProtobufMessageSchema;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.schemaregistry.services.MessageName;
+import org.apache.nifi.schemaregistry.services.MessageNameResolver;
+import org.apache.nifi.schemaregistry.services.SchemaDefinition;
+import org.apache.nifi.schemaregistry.services.StandardMessageName;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+import static java.util.Collections.singletonList;
+import static
org.apache.nifi.confluent.schemaregistry.VarintUtils.decodeZigZag;
+import static
org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStream;
+import static
org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStreamAfterFirstByteConsumed;
+
+@Tags({"confluent", "schema", "registry", "protobuf", "message", "name",
"resolver"})
+@CapabilityDescription("""
+ Resolves Protobuf message names from Confluent Schema Registry wire format
by decoding message indexes and looking up the fully qualified name in the
schema definition
+ For Confluent wire format reference see:
https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html#wire-format
+ """)
+public class ConfluentProtobufMessageNameResolver extends
AbstractControllerService implements MessageNameResolver {
+
+ public static final int MAXIMUM_SUPPORTED_ARRAY_LENGTH = 100;
+ public static final int MAXIMUM_CACHE_SIZE = 1000;
+ private Cache<FindMessageNameArguments, MessageName> messageNameCache;
+
+ @OnEnabled
+ public void onEnabled(final ConfigurationContext context) {
+ messageNameCache =
Caffeine.newBuilder().maximumSize(MAXIMUM_CACHE_SIZE).expireAfterWrite(Duration.ofHours(1)).build();
+ }
+
+ @OnDisabled
+ public void onDisabled(final ConfigurationContext context) {
+ if (messageNameCache != null) {
+ messageNameCache.invalidateAll();
+ messageNameCache = null;
+ }
+ }
+
+
+ @Override
+ public MessageName getMessageName(final Map<String, String> variables,
final SchemaDefinition schemaDefinition, final InputStream inputStream) throws
IOException {
+ final ComponentLog logger = getLogger();
+
+ // Read message indexes directly from stream (Confluent wire format)
+ final List<Integer> messageIndexes =
readMessageIndexesFromStream(inputStream);
+ if (logger.isDebugEnabled()) {
Review Comment:
Done
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]