Search in sources :

Example 1 with PostSchemaPayload

use of org.apache.pulsar.common.protocol.schema.PostSchemaPayload in project pulsar by apache.

the class AdminApiSchemaValidationEnforced method testDisableSchemaValidationEnforcedHasSchema.

@Test
public void testDisableSchemaValidationEnforcedHasSchema() throws Exception {
    admin.namespaces().createNamespace("schema-validation-enforced/default-has-schema");
    String namespace = "schema-validation-enforced/default-has-schema";
    String topicName = "persistent://schema-validation-enforced/default-has-schema/test";
    assertFalse(admin.namespaces().getSchemaValidationEnforced(namespace));
    admin.namespaces().setSchemaValidationEnforced(namespace, false);
    try {
        admin.schemas().getSchemaInfo(topicName);
    } catch (PulsarAdminException.NotFoundException e) {
        assertTrue(e.getMessage().contains("HTTP 404 Not Found"));
    }
    Map<String, String> properties = Maps.newHashMap();
    SchemaInfo schemaInfo = SchemaInfo.builder().type(SchemaType.STRING).properties(properties).name("test").schema("".getBytes()).build();
    PostSchemaPayload postSchemaPayload = new PostSchemaPayload("STRING", "", properties);
    admin.schemas().createSchema(topicName, postSchemaPayload);
    try (Producer p = pulsarClient.newProducer().topic(topicName).create()) {
        p.send("test schemaValidationEnforced".getBytes());
    }
    assertEquals(admin.schemas().getSchemaInfo(topicName), schemaInfo);
}
Also used : PostSchemaPayload(org.apache.pulsar.common.protocol.schema.PostSchemaPayload) Producer(org.apache.pulsar.client.api.Producer) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with PostSchemaPayload

use of org.apache.pulsar.common.protocol.schema.PostSchemaPayload in project pulsar by apache.

the class SchemasResourceBase method getVersionBySchema.

public void getVersionBySchema(PostSchemaPayload payload, boolean authoritative, AsyncResponse response) {
    validateDestinationAndAdminOperation(authoritative);
    String schemaId = getSchemaId();
    pulsar().getSchemaRegistryService().findSchemaVersion(schemaId, SchemaData.builder().data(payload.getSchema().getBytes(Charsets.UTF_8)).isDeleted(false).timestamp(clock.millis()).type(SchemaType.valueOf(payload.getType())).user(defaultIfEmpty(clientAppId(), "")).props(payload.getProperties()).build()).thenAccept(version -> response.resume(Response.accepted().entity(LongSchemaVersionResponse.builder().version(version).build()).build())).exceptionally(error -> {
        log.error("[{}] Failed to get version by schema for topic {}", clientAppId(), topicName, error);
        response.resume(new RestException(error));
        return null;
    });
}
Also used : GetAllVersionsSchemaResponse(org.apache.pulsar.common.protocol.schema.GetAllVersionsSchemaResponse) TopicName(org.apache.pulsar.common.naming.TopicName) IsCompatibilityResponse(org.apache.pulsar.common.protocol.schema.IsCompatibilityResponse) AdminResource(org.apache.pulsar.broker.admin.AdminResource) LoggerFactory(org.slf4j.LoggerFactory) PostSchemaResponse(org.apache.pulsar.common.protocol.schema.PostSchemaResponse) ByteBuffer(java.nio.ByteBuffer) SchemaType(org.apache.pulsar.common.schema.SchemaType) DefaultImplementation(org.apache.pulsar.client.internal.DefaultImplementation) MediaType(javax.ws.rs.core.MediaType) GetSchemaResponse(org.apache.pulsar.common.protocol.schema.GetSchemaResponse) RestException(org.apache.pulsar.broker.web.RestException) Objects.isNull(java.util.Objects.isNull) Charsets(com.google.common.base.Charsets) StringUtils.defaultIfEmpty(org.apache.commons.lang.StringUtils.defaultIfEmpty) Logger(org.slf4j.Logger) UTF_8(java.nio.charset.StandardCharsets.UTF_8) AsyncResponse(javax.ws.rs.container.AsyncResponse) IOException(java.io.IOException) DeleteSchemaResponse(org.apache.pulsar.common.protocol.schema.DeleteSchemaResponse) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Collectors(java.util.stream.Collectors) LongSchemaVersionResponse(org.apache.pulsar.common.protocol.schema.LongSchemaVersionResponse) List(java.util.List) Response(javax.ws.rs.core.Response) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) Clock(java.time.Clock) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PostSchemaPayload(org.apache.pulsar.common.protocol.schema.PostSchemaPayload) SchemaAndMetadata(org.apache.pulsar.broker.service.schema.SchemaRegistry.SchemaAndMetadata) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) RestException(org.apache.pulsar.broker.web.RestException)

Example 3 with PostSchemaPayload

use of org.apache.pulsar.common.protocol.schema.PostSchemaPayload in project pulsar-flink by streamnative.

the class SchemaUtils method uploadPulsarSchema.

public static void uploadPulsarSchema(PulsarAdmin admin, String topic, SchemaInfo schemaInfo) {
    checkNotNull(schemaInfo);
    SchemaInfo existingSchema;
    try {
        existingSchema = admin.schemas().getSchemaInfo(TopicName.get(topic).toString());
    } catch (PulsarAdminException pae) {
        if (pae.getStatusCode() == 404) {
            existingSchema = null;
        } else {
            throw new IllegalStateException(String.format("Failed to get schema information for %s", TopicName.get(topic).toString()), pae);
        }
    } catch (Throwable e) {
        throw new IllegalStateException(String.format("Failed to get schema information for %s", TopicName.get(topic).toString()), e);
    }
    if (existingSchema == null) {
        PostSchemaPayload pl = new PostSchemaPayload();
        pl.setType(schemaInfo.getType().name());
        pl.setSchema(getSchemaString(schemaInfo));
        pl.setProperties(schemaInfo.getProperties());
        try {
            admin.schemas().createSchema(TopicName.get(topic).toString(), pl);
        } catch (PulsarAdminException pae) {
            if (pae.getStatusCode() == 404) {
                throw new IllegalStateException(String.format("Create schema for %s get 404", TopicName.get(topic).toString()), pae);
            } else {
                throw new IllegalStateException(String.format("Failed to create schema information for %s", TopicName.get(topic).toString()), pae);
            }
        } catch (Throwable e) {
            throw new IllegalStateException(String.format("Failed to create schema information for %s", TopicName.get(topic).toString()), e);
        }
    } else if (!schemaEqualsIgnoreProperties(schemaInfo, existingSchema) && !compatibleSchema(existingSchema, schemaInfo)) {
        throw new IllegalStateException("Writing to a topic which have incompatible schema");
    }
}
Also used : PostSchemaPayload(org.apache.pulsar.common.protocol.schema.PostSchemaPayload) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo)

Example 4 with PostSchemaPayload

use of org.apache.pulsar.common.protocol.schema.PostSchemaPayload in project pulsar by yahoo.

the class PulsarAdminToolTest method schemas.

@Test
void schemas() throws Exception {
    PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
    Schemas schemas = Mockito.mock(Schemas.class);
    doReturn(schemas).when(admin).schemas();
    CmdSchemas cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("get -v 1 persistent://tn1/ns1/tp1"));
    verify(schemas).getSchemaInfo("persistent://tn1/ns1/tp1", 1);
    cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("get -a persistent://tn1/ns1/tp1"));
    verify(schemas).getAllSchemas("persistent://tn1/ns1/tp1");
    cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("get persistent://tn1/ns1/tp1"));
    verify(schemas).getSchemaInfoWithVersion("persistent://tn1/ns1/tp1");
    cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("delete persistent://tn1/ns1/tp1"));
    verify(schemas).deleteSchema("persistent://tn1/ns1/tp1", false);
    cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("delete persistent://tn1/ns1/tp1 -f"));
    verify(schemas).deleteSchema("persistent://tn1/ns1/tp1", true);
    cmdSchemas = new CmdSchemas(() -> admin);
    String schemaFile = PulsarAdminToolTest.class.getClassLoader().getResource("test_schema_create.json").getFile();
    cmdSchemas.run(split("upload -f " + schemaFile + " persistent://tn1/ns1/tp1"));
    PostSchemaPayload input = new ObjectMapper().readValue(new File(schemaFile), PostSchemaPayload.class);
    verify(schemas).createSchema("persistent://tn1/ns1/tp1", input);
    cmdSchemas = new CmdSchemas(() -> admin);
    String jarFile = PulsarAdminToolTest.class.getClassLoader().getResource("dummyexamples.jar").getFile();
    String className = SchemaDemo.class.getName();
    cmdSchemas.run(split("extract -j " + jarFile + " -c " + className + " -t json persistent://tn1/ns1/tp1"));
    File file = new File(jarFile);
    ClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });
    Class cls = cl.loadClass(className);
    SchemaDefinition<Object> schemaDefinition = SchemaDefinition.builder().withPojo(cls).withAlwaysAllowNull(true).build();
    PostSchemaPayload postSchemaPayload = new PostSchemaPayload();
    postSchemaPayload.setType("JSON");
    postSchemaPayload.setSchema(SchemaExtractor.getJsonSchemaInfo(schemaDefinition));
    postSchemaPayload.setProperties(schemaDefinition.getProperties());
    verify(schemas).createSchema("persistent://tn1/ns1/tp1", postSchemaPayload);
}
Also used : PostSchemaPayload(org.apache.pulsar.common.protocol.schema.PostSchemaPayload) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Schemas(org.apache.pulsar.client.admin.Schemas) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 5 with PostSchemaPayload

use of org.apache.pulsar.common.protocol.schema.PostSchemaPayload in project pulsar by yahoo.

the class SchemasResourceBase method postSchema.

public void postSchema(PostSchemaPayload payload, boolean authoritative, AsyncResponse response) {
    validateDestinationAndAdminOperation(authoritative);
    getSchemaCompatibilityStrategyAsync().thenAccept(schemaCompatibilityStrategy -> {
        byte[] data;
        if (SchemaType.KEY_VALUE.name().equals(payload.getType())) {
            try {
                data = DefaultImplementation.getDefaultImplementation().convertKeyValueDataStringToSchemaInfoSchema(payload.getSchema().getBytes(Charsets.UTF_8));
            } catch (IOException conversionError) {
                log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, conversionError);
                response.resume(new RestException(conversionError));
                return;
            }
        } else {
            data = payload.getSchema().getBytes(Charsets.UTF_8);
        }
        pulsar().getSchemaRegistryService().putSchemaIfAbsent(getSchemaId(), SchemaData.builder().data(data).isDeleted(false).timestamp(clock.millis()).type(SchemaType.valueOf(payload.getType())).user(defaultIfEmpty(clientAppId(), "")).props(payload.getProperties()).build(), schemaCompatibilityStrategy).thenAccept(version -> response.resume(Response.accepted().entity(PostSchemaResponse.builder().version(version).build()).build())).exceptionally(error -> {
            Throwable throwable = FutureUtil.unwrapCompletionException(error);
            if (throwable instanceof IncompatibleSchemaException) {
                response.resume(Response.status(Response.Status.CONFLICT.getStatusCode(), throwable.getMessage()).build());
            } else if (throwable instanceof InvalidSchemaDataException) {
                response.resume(Response.status(422, /* Unprocessable Entity */
                throwable.getMessage()).build());
            } else {
                log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
                response.resume(new RestException(throwable));
            }
            return null;
        });
    }).exceptionally(error -> {
        Throwable throwable = FutureUtil.unwrapCompletionException(error);
        if (throwable instanceof RestException) {
            // Unprocessable Entity
            response.resume(Response.status(((RestException) throwable).getResponse().getStatus(), throwable.getMessage()).build());
        } else {
            log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
            response.resume(new RestException(throwable));
        }
        return null;
    });
}
Also used : GetAllVersionsSchemaResponse(org.apache.pulsar.common.protocol.schema.GetAllVersionsSchemaResponse) TopicName(org.apache.pulsar.common.naming.TopicName) IsCompatibilityResponse(org.apache.pulsar.common.protocol.schema.IsCompatibilityResponse) AdminResource(org.apache.pulsar.broker.admin.AdminResource) LoggerFactory(org.slf4j.LoggerFactory) PostSchemaResponse(org.apache.pulsar.common.protocol.schema.PostSchemaResponse) ByteBuffer(java.nio.ByteBuffer) SchemaType(org.apache.pulsar.common.schema.SchemaType) DefaultImplementation(org.apache.pulsar.client.internal.DefaultImplementation) MediaType(javax.ws.rs.core.MediaType) GetSchemaResponse(org.apache.pulsar.common.protocol.schema.GetSchemaResponse) RestException(org.apache.pulsar.broker.web.RestException) Objects.isNull(java.util.Objects.isNull) Charsets(com.google.common.base.Charsets) StringUtils.defaultIfEmpty(org.apache.commons.lang.StringUtils.defaultIfEmpty) Logger(org.slf4j.Logger) UTF_8(java.nio.charset.StandardCharsets.UTF_8) AsyncResponse(javax.ws.rs.container.AsyncResponse) IOException(java.io.IOException) DeleteSchemaResponse(org.apache.pulsar.common.protocol.schema.DeleteSchemaResponse) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Collectors(java.util.stream.Collectors) LongSchemaVersionResponse(org.apache.pulsar.common.protocol.schema.LongSchemaVersionResponse) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) Clock(java.time.Clock) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PostSchemaPayload(org.apache.pulsar.common.protocol.schema.PostSchemaPayload) SchemaAndMetadata(org.apache.pulsar.broker.service.schema.SchemaRegistry.SchemaAndMetadata) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) RestException(org.apache.pulsar.broker.web.RestException) IOException(java.io.IOException)

Aggregations

PostSchemaPayload (org.apache.pulsar.common.protocol.schema.PostSchemaPayload)25 IOException (java.io.IOException)12 Test (org.testng.annotations.Test)12 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)10 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 Charsets (com.google.common.base.Charsets)9 ByteBuffer (java.nio.ByteBuffer)9 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)9 Clock (java.time.Clock)9 List (java.util.List)9 Objects.isNull (java.util.Objects.isNull)9 Collectors (java.util.stream.Collectors)9 AsyncResponse (javax.ws.rs.container.AsyncResponse)9 MediaType (javax.ws.rs.core.MediaType)9 Response (javax.ws.rs.core.Response)9 StringUtils.defaultIfEmpty (org.apache.commons.lang.StringUtils.defaultIfEmpty)9 AdminResource (org.apache.pulsar.broker.admin.AdminResource)9 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)9 SchemaAndMetadata (org.apache.pulsar.broker.service.schema.SchemaRegistry.SchemaAndMetadata)9