Search in sources :

Example 31 with InsertValues

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

the class InsertValuesExecutor method buildRecord.

private ProducerRecord<byte[], byte[]> buildRecord(final ConfiguredStatement<InsertValues> statement, final MetaStore metaStore, final DataSource dataSource, final ServiceContext serviceContext) {
    throwIfDisabled(statement.getSessionConfig().getConfig(false));
    final InsertValues insertValues = statement.getStatement();
    final KsqlConfig config = statement.getSessionConfig().getConfig(true);
    try {
        final KsqlGenericRecord row = new GenericRecordFactory(config, metaStore, clock).build(insertValues.getColumns(), insertValues.getValues(), dataSource.getSchema(), dataSource.getDataSourceType());
        final byte[] key = serializeKey(row.key, dataSource, config, serviceContext);
        final byte[] value = serializeValue(row.value, dataSource, config, serviceContext);
        final String topicName = dataSource.getKafkaTopicName();
        return new ProducerRecord<>(topicName, null, row.ts, key, value);
    } catch (final Exception e) {
        throw new KsqlStatementException(createInsertFailedExceptionMessage(insertValues) + " " + e.getMessage(), statement.getStatementText(), e);
    }
}
Also used : GenericRecordFactory(io.confluent.ksql.engine.generic.GenericRecordFactory) InsertValues(io.confluent.ksql.parser.tree.InsertValues) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KsqlConfig(io.confluent.ksql.util.KsqlConfig) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) ExecutionException(java.util.concurrent.ExecutionException) KsqlSchemaAuthorizationException(io.confluent.ksql.exception.KsqlSchemaAuthorizationException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) KsqlGenericRecord(io.confluent.ksql.engine.generic.KsqlGenericRecord)

Example 32 with InsertValues

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

the class CommandParser method getInsertValuesStatement.

private static SqlInsertValues getInsertValuesStatement(final String sql, final Map<String, String> variables) {
    final InsertValues parsedStatement;
    try {
        final String substituted = VariableSubstitutor.substitute(KSQL_PARSER.parse(sql).get(0), variables);
        parsedStatement = (InsertValues) new AstBuilder(TypeRegistry.EMPTY).buildStatement(KSQL_PARSER.parse(substituted).get(0).getStatement());
    } catch (ParseFailedException e) {
        throw new MigrationException(String.format("Failed to parse INSERT VALUES statement. Statement: %s. Reason: %s", sql, e.getMessage()));
    }
    return new SqlInsertValues(sql, preserveCase(parsedStatement.getTarget().text()), parsedStatement.getValues(), parsedStatement.getColumns().stream().map(ColumnName::text).collect(Collectors.toList()));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) AstBuilder(io.confluent.ksql.parser.AstBuilder) ColumnName(io.confluent.ksql.name.ColumnName) InsertValues(io.confluent.ksql.parser.tree.InsertValues) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException)

Aggregations

InsertValues (io.confluent.ksql.parser.tree.InsertValues)32 Test (org.junit.Test)25 KsqlException (io.confluent.ksql.util.KsqlException)24 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)22 ExecutionException (java.util.concurrent.ExecutionException)22 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)22 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)21 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)20 SerializationException (org.apache.kafka.common.errors.SerializationException)20 KsqlConfig (io.confluent.ksql.util.KsqlConfig)8 SessionProperties (io.confluent.ksql.rest.SessionProperties)5 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)4 GenericRecordFactory (io.confluent.ksql.engine.generic.GenericRecordFactory)3 KsqlGenericRecord (io.confluent.ksql.engine.generic.KsqlGenericRecord)3 NullLiteral (io.confluent.ksql.execution.expression.tree.NullLiteral)3 DataSource (io.confluent.ksql.metastore.model.DataSource)3 KsqlSchemaAuthorizationException (io.confluent.ksql.exception.KsqlSchemaAuthorizationException)2 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)2 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)2 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)2