private CommandId getTopicCommandId(final RegisterTopic registerTopic) { final String topicName = registerTopic.getName().toString(); if (metaStore.getAllKsqlTopics().containsKey(topicName)) { throw new RuntimeException(String.format("Topic %s already exists", topicName)); } return new CommandId(CommandId.Type.TOPIC, topicName, CommandId.Action.CREATE); }
public Optional<String> getAvroSchemaIfAvroTopic( final SqlBaseParser.RegisterTopicContext registerTopicContext ) { final AstBuilder astBuilder = new AstBuilder(null); final RegisterTopic registerTopic = (RegisterTopic) astBuilder.visitRegisterTopic(registerTopicContext); if (registerTopic.getProperties().get(DdlConfig.VALUE_FORMAT_PROPERTY) == null) { throw new KsqlException("VALUE_FORMAT is not set for the topic."); } if (registerTopic.getProperties().get(DdlConfig.VALUE_FORMAT_PROPERTY).toString() .equalsIgnoreCase("'AVRO'")) { if (registerTopic.getProperties().containsKey(DdlConfig.AVRO_SCHEMA_FILE)) { final String avroSchema = getAvroSchema(AstBuilder.unquote( registerTopic.getProperties().get(DdlConfig.AVRO_SCHEMA_FILE).toString(), "'") ); return Optional.of(avroSchema); } else { throw new KsqlException( "You need to provide avro schema file path for topics in avro format."); } } return Optional.empty(); }
@Test public void testRegisterTopic() { final String queryStr = "REGISTER TOPIC orders_topic WITH (value_format = 'avro', " + "avroschemafile='/Users/hojjat/avro_order_schema.avro',kafka_topic='orders_topic');"; final Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0).getStatement(); Assert.assertTrue("testRegisterTopic failed.", statement instanceof RegisterTopic); final RegisterTopic registerTopic = (RegisterTopic)statement; Assert.assertTrue("testRegisterTopic failed.", registerTopic .getName().toString().equalsIgnoreCase("ORDERS_TOPIC")); Assert.assertTrue("testRegisterTopic failed.", registerTopic.getProperties().size() == 3); Assert.assertTrue("testRegisterTopic failed.", registerTopic.getProperties().get(DdlConfig.VALUE_FORMAT_PROPERTY).toString().equalsIgnoreCase("'avro'")); }
@Override public Node visitRegisterTopic(final SqlBaseParser.RegisterTopicContext context) { return new RegisterTopic( getLocation(context), getQualifiedName(context.qualifiedName()), context.EXISTS() != null, processTableProperties(context.tableProperties()) ); }
@Test /* TODO: Handle so-called identifier expressions as values in table properties (right now, the lack of single quotes around in the variables <format> and <kafkaTopic> cause things to break). */ @Ignore public void testCreateTopicFormatWithoutQuotes() { final String ksqlTopic = "unquoted_topic"; final String format = "json"; final String kafkaTopic = "case_insensitive_kafka_topic"; final String queryStr = format( "REGISTER TOPIC %s WITH (value_format = %s, kafka_topic = %s);", ksqlTopic, format, kafkaTopic ); final Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0).getStatement(); Assert.assertTrue(statement instanceof RegisterTopic); final RegisterTopic registerTopic = (RegisterTopic) statement; Assert.assertTrue(registerTopic.getName().toString().equalsIgnoreCase(ksqlTopic)); Assert.assertTrue(registerTopic.getProperties().size() == 2); Assert.assertTrue(registerTopic .getProperties().get(DdlConfig.VALUE_FORMAT_PROPERTY).toString().equalsIgnoreCase(format)); Assert.assertTrue(registerTopic.getProperties().get(DdlConfig.KAFKA_TOPIC_NAME_PROPERTY).toString().equalsIgnoreCase(kafkaTopic)); }
); ksqlEngine.getDdlCommandExec().execute(new RegisterTopicCommand(new RegisterTopic( QualifiedName.of(COMMANDS_KSQL_TOPIC_NAME), false,