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);
}
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;
});
}
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");
}
}
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);
}
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;
});
}
Aggregations