From 7e1c143b2871dbf4795f281b246152d0d2b4010a Mon Sep 17 00:00:00 2001 From: Yi Tang Date: Wed, 28 Aug 2019 13:29:37 +0800 Subject: [PATCH] [admin] extract schema support no null (#5048) ### Motivation admin schemas extract schema with always-allow-null by default which should be optional. ### Modifications * provide a option to disable always-allow-null; * provide a option to dry run and not apply to registry. --- .../apache/pulsar/admin/cli/CmdSchemas.java | 31 +++++++++++++++---- .../admin/cli/utils/SchemaExtractor.java | 11 ++++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSchemas.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSchemas.java index a863f982e6060..0b75cceb6fc76 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSchemas.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSchemas.java @@ -26,6 +26,7 @@ import java.net.URLClassLoader; import org.apache.pulsar.admin.cli.utils.SchemaExtractor; import org.apache.pulsar.client.admin.PulsarAdmin; +import org.apache.pulsar.client.api.schema.SchemaDefinition; import org.apache.pulsar.common.protocol.schema.PostSchemaPayload; @Parameters(commandDescription = "Operations about schemas") @@ -101,29 +102,47 @@ private class ExtractSchema extends CliCommand { @Parameter(names = { "-c", "--classname" }, description = "class name of pojo", required = true) private String className; + @Parameter(names = { "--always-allow-null" }, arity = 1, + description = "set schema whether always allow null or not") + private boolean alwaysAllowNull = true; + + @Parameter(names = { "-n", "--dry-run"}, + description = "dost not apply to schema registry, " + + "just prints the post schema payload") + private boolean dryRun = false; + @Override void run() throws Exception { String topic = validateTopicName(params); File file = new File(jarFilePath); ClassLoader cl = new URLClassLoader(new URL[]{ file.toURI().toURL() }); - Class cls = cl.loadClass(className); PostSchemaPayload input = new PostSchemaPayload(); - + SchemaDefinition schemaDefinition = + SchemaDefinition.builder() + .withPojo(cls) + .withAlwaysAllowNull(alwaysAllowNull) + .build(); if (type.toLowerCase().equalsIgnoreCase("avro")) { input.setType("AVRO"); - input.setSchema(SchemaExtractor.getAvroSchemaInfo(cls)); + input.setSchema(SchemaExtractor.getAvroSchemaInfo(schemaDefinition)); } else if (type.toLowerCase().equalsIgnoreCase("json")){ input.setType("JSON"); - input.setSchema(SchemaExtractor.getJsonSchemaInfo(cls)); + input.setSchema(SchemaExtractor.getJsonSchemaInfo(schemaDefinition)); } else { throw new Exception("Unknown schema type specified as type"); } - - admin.schemas().createSchema(topic, input); + input.setProperties(schemaDefinition.getProperties()); + if (dryRun) { + System.out.println(topic); + System.out.println(MAPPER.writerWithDefaultPrettyPrinter() + .writeValueAsString(input)); + } else { + admin.schemas().createSchema(topic, input); + } } } diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/utils/SchemaExtractor.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/utils/SchemaExtractor.java index 53269a820047c..977861cbf359e 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/utils/SchemaExtractor.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/utils/SchemaExtractor.java @@ -19,19 +19,22 @@ package org.apache.pulsar.admin.cli.utils; import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.client.api.schema.SchemaDefinition; import java.nio.charset.StandardCharsets; public class SchemaExtractor { - public static String getJsonSchemaInfo(Class clazz) { + public static String getJsonSchemaInfo(SchemaDefinition schemaDefinition) { - return new String(Schema.JSON(clazz).getSchemaInfo().getSchema(), StandardCharsets.UTF_8); + return new String(Schema.JSON(schemaDefinition).getSchemaInfo().getSchema(), + StandardCharsets.UTF_8); } - public static String getAvroSchemaInfo(Class clazz) { + public static String getAvroSchemaInfo(SchemaDefinition schemaDefinition) { - return new String(Schema.AVRO(clazz).getSchemaInfo().getSchema(), StandardCharsets.UTF_8); + return new String(Schema.AVRO(schemaDefinition).getSchemaInfo().getSchema(), + StandardCharsets.UTF_8); } }