Search in sources :

Example 6 with RegisterTopic

use of io.confluent.ksql.parser.tree.RegisterTopic in project ksql by confluentinc.

the class CliUtils method getAvroSchemaIfAvroTopic.

public Optional<String> getAvroSchemaIfAvroTopic(SqlBaseParser.RegisterTopicContext registerTopicContext) {
    AstBuilder astBuilder = new AstBuilder(null);
    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)) {
            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();
}
Also used : AstBuilder(io.confluent.ksql.parser.AstBuilder) RegisterTopic(io.confluent.ksql.parser.tree.RegisterTopic)

Example 7 with RegisterTopic

use of io.confluent.ksql.parser.tree.RegisterTopic in project ksql by confluentinc.

the class KsqlResourceTest method testInstantRegisterTopic.

@Test
public void testInstantRegisterTopic() throws Exception {
    KsqlResource testResource = TestKsqlResourceUtil.get(ksqlEngine, ksqlRestConfig);
    final String ksqlTopic = "FOO";
    final String kafkaTopic = "bar";
    final String format = "json";
    final String ksqlString = String.format("REGISTER TOPIC %s WITH (kafka_topic='%s', value_format='%s');", ksqlTopic, kafkaTopic, format);
    final Map<String, Expression> createTopicProperties = new HashMap<>();
    createTopicProperties.put(DdlConfig.KAFKA_TOPIC_NAME_PROPERTY, new StringLiteral(kafkaTopic));
    createTopicProperties.put(DdlConfig.VALUE_FORMAT_PROPERTY, new StringLiteral(format));
    final RegisterTopic ksqlStatement = new RegisterTopic(QualifiedName.of(ksqlTopic), false, createTopicProperties);
    final CommandId commandId = new CommandId(CommandId.Type.TOPIC, ksqlTopic, CommandId.Action.CREATE);
    final CommandStatus commandStatus = new CommandStatus(CommandStatus.Status.QUEUED, "Statement written to command topic");
    final CommandStatusEntity expectedCommandStatusEntity = new CommandStatusEntity(ksqlString, commandId, commandStatus);
    final Map<String, Object> streamsProperties = Collections.emptyMap();
    KsqlEntity testKsqlEntity = makeSingleRequest(testResource, ksqlString, ksqlStatement, streamsProperties, KsqlEntity.class);
    assertEquals(expectedCommandStatusEntity, testKsqlEntity);
}
Also used : HashMap(java.util.HashMap) RegisterTopic(io.confluent.ksql.parser.tree.RegisterTopic) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) Expression(io.confluent.ksql.parser.tree.Expression) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) CommandId(io.confluent.ksql.rest.server.computation.CommandId) Test(org.junit.Test)

Aggregations

RegisterTopic (io.confluent.ksql.parser.tree.RegisterTopic)7 Test (org.junit.Test)4 CreateStreamCommand (io.confluent.ksql.ddl.commands.CreateStreamCommand)2 RegisterTopicCommand (io.confluent.ksql.ddl.commands.RegisterTopicCommand)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 Expression (io.confluent.ksql.parser.tree.Expression)2 Statement (io.confluent.ksql.parser.tree.Statement)2 StringLiteral (io.confluent.ksql.parser.tree.StringLiteral)2 CommandId (io.confluent.ksql.rest.server.computation.CommandId)2 HashMap (java.util.HashMap)2 KsqlEngine (io.confluent.ksql.KsqlEngine)1 CreateTableCommand (io.confluent.ksql.ddl.commands.CreateTableCommand)1 DropSourceCommand (io.confluent.ksql.ddl.commands.DropSourceCommand)1 DropTopicCommand (io.confluent.ksql.ddl.commands.DropTopicCommand)1 AstBuilder (io.confluent.ksql.parser.AstBuilder)1 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)1 CreateTable (io.confluent.ksql.parser.tree.CreateTable)1 DropStream (io.confluent.ksql.parser.tree.DropStream)1 DropTable (io.confluent.ksql.parser.tree.DropTable)1 DropTopic (io.confluent.ksql.parser.tree.DropTopic)1