Search in sources :

Example 1 with SubjectVersion

use of io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion in project schema-registry by confluentinc.

the class RestService method getAllVersionsById.

public List<SubjectVersion> getAllVersionsById(Map<String, String> requestProperties, int id, String subject, boolean lookupDeleted) throws IOException, RestClientException {
    UriBuilder builder = UriBuilder.fromPath("/schemas/ids/{id}/versions");
    builder.queryParam("deleted", lookupDeleted);
    if (subject != null) {
        builder.queryParam("subject", subject);
    }
    String path = builder.build(id).toString();
    List<SubjectVersion> response = httpRequest(path, "GET", null, requestProperties, GET_VERSIONS_RESPONSE_TYPE);
    return response;
}
Also used : SubjectVersion(io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)

Example 2 with SubjectVersion

use of io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion in project schema-registry by confluentinc.

the class RestApiTest method testGetVersionsAssociatedWithSchemaId.

@Test
public void testGetVersionsAssociatedWithSchemaId() throws Exception {
    String subject1 = "testTopic1";
    String subject2 = "testTopic2";
    String schema = TestUtils.getRandomCanonicalAvroString(1).get(0);
    TestUtils.registerAndVerifySchema(restApp.restClient, schema, 1, subject1);
    TestUtils.registerAndVerifySchema(restApp.restClient, schema, 1, subject2);
    List<SubjectVersion> associatedSubjects = restApp.restClient.getAllVersionsById(1);
    assertEquals(associatedSubjects.size(), 2);
    assertTrue(associatedSubjects.contains(new SubjectVersion(subject1, 1)));
    assertTrue(associatedSubjects.contains(new SubjectVersion(subject2, 1)));
    assertEquals("Deleting Schema Version Success", (Integer) 1, restApp.restClient.deleteSchemaVersion(RestService.DEFAULT_REQUEST_PROPERTIES, subject2, "1"));
    associatedSubjects = restApp.restClient.getAllVersionsById(1);
    assertEquals(associatedSubjects.size(), 1);
    assertTrue(associatedSubjects.contains(new SubjectVersion(subject1, 1)));
    associatedSubjects = restApp.restClient.getAllVersionsById(RestService.DEFAULT_REQUEST_PROPERTIES, 1, null, true);
    assertEquals(associatedSubjects.size(), 2);
    assertTrue(associatedSubjects.contains(new SubjectVersion(subject1, 1)));
    assertTrue(associatedSubjects.contains(new SubjectVersion(subject2, 1)));
}
Also used : SubjectVersion(io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) Test(org.junit.Test)

Example 3 with SubjectVersion

use of io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion in project schema-registry by confluentinc.

the class KafkaSchemaRegistry method listVersionsForId.

public List<SubjectVersion> listVersionsForId(int id, String subject, boolean lookupDeleted) throws SchemaRegistryException {
    SchemaValue schema = null;
    try {
        SchemaKey subjectVersionKey = getSchemaKeyUsingContexts(id, subject);
        if (subjectVersionKey == null) {
            return null;
        }
        schema = (SchemaValue) kafkaStore.get(subjectVersionKey);
        if (schema == null) {
            return null;
        }
        return lookupCache.schemaIdAndSubjects(new Schema(schema.getSubject(), schema.getVersion(), schema.getId(), schema.getSchemaType(), schema.getReferences().stream().map(ref -> new io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference(ref.getName(), ref.getSubject(), ref.getVersion())).collect(Collectors.toList()), schema.getSchema())).allSubjectVersions().entrySet().stream().flatMap(e -> {
            try {
                SchemaValue schemaValue = (SchemaValue) kafkaStore.get(new SchemaKey(e.getKey(), e.getValue()));
                if ((schemaValue != null && !schemaValue.isDeleted()) || lookupDeleted) {
                    return Stream.of(new SubjectVersion(e.getKey(), e.getValue()));
                } else {
                    return Stream.empty();
                }
            } catch (StoreException ex) {
                return Stream.empty();
            }
        }).collect(Collectors.toList());
    } catch (StoreException e) {
        throw new SchemaRegistryStoreException("Error while retrieving schema with id " + id + " from the backend Kafka store", e);
    }
}
Also used : IncompatibleSchemaException(io.confluent.kafka.schemaregistry.exceptions.IncompatibleSchemaException) Arrays(java.util.Arrays) LoadingCache(com.google.common.cache.LoadingCache) DEFAULT_CONTEXT(io.confluent.kafka.schemaregistry.utils.QualifiedSubject.DEFAULT_CONTEXT) IdGenerator(io.confluent.kafka.schemaregistry.id.IdGenerator) RestException(io.confluent.rest.exceptions.RestException) LoggerFactory(org.slf4j.LoggerFactory) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) TimeoutException(java.util.concurrent.TimeoutException) ReferenceExistsException(io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException) AdminClient(org.apache.kafka.clients.admin.AdminClient) InvalidSchemaException(io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException) Map(java.util.Map) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) RegisterSchemaRequest(io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaRequest) QualifiedSubject(io.confluent.kafka.schemaregistry.utils.QualifiedSubject) URI(java.net.URI) IncrementalIdGenerator(io.confluent.kafka.schemaregistry.id.IncrementalIdGenerator) KafkaGroupLeaderElector(io.confluent.kafka.schemaregistry.leaderelector.kafka.KafkaGroupLeaderElector) SchemaRegistryConfig(io.confluent.kafka.schemaregistry.rest.SchemaRegistryConfig) HostnameVerifier(javax.net.ssl.HostnameVerifier) ConfigDef(org.apache.kafka.common.config.ConfigDef) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) ModeUpdateRequest(io.confluent.kafka.schemaregistry.client.rest.entities.requests.ModeUpdateRequest) StoreTimeoutException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreTimeoutException) UnknownLeaderException(io.confluent.kafka.schemaregistry.exceptions.UnknownLeaderException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MetricsContainer(io.confluent.kafka.schemaregistry.metrics.MetricsContainer) Set(java.util.Set) SubjectVersion(io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Serializer(io.confluent.kafka.schemaregistry.storage.serialization.Serializer) Collectors(java.util.stream.Collectors) EntryTooLargeException(io.confluent.kafka.schemaregistry.storage.exceptions.EntryTooLargeException) CONTEXT_WILDCARD(io.confluent.kafka.schemaregistry.utils.QualifiedSubject.CONTEXT_WILDCARD) CacheLoader(com.google.common.cache.CacheLoader) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException) ConfigUpdateRequest(io.confluent.kafka.schemaregistry.client.rest.entities.requests.ConfigUpdateRequest) CacheBuilder(com.google.common.cache.CacheBuilder) RestService(io.confluent.kafka.schemaregistry.client.rest.RestService) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) HashMap(java.util.HashMap) Application(io.confluent.rest.Application) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) OperationNotPermittedException(io.confluent.kafka.schemaregistry.exceptions.OperationNotPermittedException) ArrayList(java.util.ArrayList) SchemaVersionNotSoftDeletedException(io.confluent.kafka.schemaregistry.exceptions.SchemaVersionNotSoftDeletedException) CompatibilityLevel(io.confluent.kafka.schemaregistry.CompatibilityLevel) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) CONTEXT_DELIMITER(io.confluent.kafka.schemaregistry.utils.QualifiedSubject.CONTEXT_DELIMITER) RestConfig(io.confluent.rest.RestConfig) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) LinkedHashSet(java.util.LinkedHashSet) IdGenerationException(io.confluent.kafka.schemaregistry.exceptions.IdGenerationException) VersionId(io.confluent.kafka.schemaregistry.rest.VersionId) CONTEXT_PREFIX(io.confluent.kafka.schemaregistry.utils.QualifiedSubject.CONTEXT_PREFIX) SchemaRegistryTimeoutException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException) SubjectNotSoftDeletedException(io.confluent.kafka.schemaregistry.exceptions.SubjectNotSoftDeletedException) UrlList(io.confluent.kafka.schemaregistry.client.rest.utils.UrlList) Logger(org.slf4j.Logger) Properties(java.util.Properties) Iterator(java.util.Iterator) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) IOException(java.io.IOException) SchemaRegistryInitializationException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryInitializationException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Nullable(org.apache.avro.reflect.Nullable) StoreInitializationException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreInitializationException) SchemaRegistryRequestForwardingException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryRequestForwardingException) SchemaTooLargeException(io.confluent.kafka.schemaregistry.exceptions.SchemaTooLargeException) Collections(java.util.Collections) SslFactory(io.confluent.kafka.schemaregistry.client.security.SslFactory) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SubjectVersion(io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException)

Example 4 with SubjectVersion

use of io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion in project schema-registry by confluentinc.

the class SchemasResource method getVersions.

@GET
@Path("/ids/{id}/versions")
@DocumentedName("getAllVersionsById")
@Operation(summary = "List subject-versions associated to schema ID", description = "Get all the subject-version pairs associated with the input ID.", responses = { @ApiResponse(responseCode = "200", description = "The subject versions matching the specified parameters", content = @Content(array = @ArraySchema(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = SubjectVersion.class)))), @ApiResponse(responseCode = "404", description = "Error code 40403 -- Schema not " + "found\n"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the " + "backend data store\n") })
public List<SubjectVersion> getVersions(@Parameter(description = "Globally unique identifier of the schema", required = true) @PathParam("id") Integer id, @Parameter(description = "Filters results by the respective subject") @QueryParam("subject") String subject, @Parameter(description = "Whether to include subject versions where the schema was deleted") @QueryParam("deleted") boolean lookupDeletedSchema) {
    List<SubjectVersion> versions;
    String errorMessage = "Error while retrieving all subjects associated with schema id " + id + " from the schema registry";
    try {
        versions = schemaRegistry.listVersionsForId(id, subject, lookupDeletedSchema);
    } catch (SchemaRegistryStoreException e) {
        log.debug(errorMessage, e);
        throw Errors.storeException(errorMessage, e);
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException(errorMessage, e);
    }
    if (versions == null) {
        throw Errors.schemaNotFoundException();
    }
    return versions;
}
Also used : SubjectVersion(io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)4 SubjectVersion (io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion)4 SchemaRegistryException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException)2 SchemaRegistryStoreException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 CompatibilityLevel (io.confluent.kafka.schemaregistry.CompatibilityLevel)1 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)1 SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)1 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)1 AvroSchemaProvider (io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider)1 RestService (io.confluent.kafka.schemaregistry.client.rest.RestService)1 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)1 ConfigUpdateRequest (io.confluent.kafka.schemaregistry.client.rest.entities.requests.ConfigUpdateRequest)1 ModeUpdateRequest (io.confluent.kafka.schemaregistry.client.rest.entities.requests.ModeUpdateRequest)1 RegisterSchemaRequest (io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaRequest)1 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)1 UrlList (io.confluent.kafka.schemaregistry.client.rest.utils.UrlList)1 SslFactory (io.confluent.kafka.schemaregistry.client.security.SslFactory)1