Search in sources :

Example 1 with GenericRecordFactory

use of io.confluent.ksql.engine.generic.GenericRecordFactory in project ksql by confluentinc.

the class AssertExecutor method assertContent.

private static void assertContent(final KsqlExecutionContext engine, final KsqlConfig config, final InsertValues values, final TestDriverPipeline driverPipeline, final boolean isTombstone) {
    final boolean compareTimestamp = values.getColumns().stream().anyMatch(SystemColumns.ROWTIME_NAME::equals);
    final DataSource dataSource = engine.getMetaStore().getSource(values.getTarget());
    KsqlGenericRecord expected = new GenericRecordFactory(config, engine.getMetaStore(), System::currentTimeMillis).build(values.getColumns(), values.getValues(), dataSource.getSchema(), dataSource.getDataSourceType());
    if (isTombstone) {
        if (expected.value.values().stream().anyMatch(Objects::nonNull)) {
            throw new AssertionError("Unexpected value columns specified in ASSERT NULL VALUES.");
        }
        expected = KsqlGenericRecord.of(expected.key, null, expected.ts);
    }
    final Iterator<TestRecord<GenericKey, GenericRow>> records = driverPipeline.getRecordsForTopic(dataSource.getKafkaTopicName());
    if (!records.hasNext()) {
        throwAssertionError("Expected another record, but all records have already been read:", dataSource, expected, driverPipeline.getAllRecordsForTopic(dataSource.getKafkaTopicName()).stream().map(rec -> KsqlGenericRecord.of(rec.key(), rec.value(), rec.timestamp())).collect(Collectors.toList()));
    }
    final TestRecord<GenericKey, GenericRow> actualTestRecord = records.next();
    final KsqlGenericRecord actual = KsqlGenericRecord.of(actualTestRecord.key(), actualTestRecord.value(), compareTimestamp ? actualTestRecord.timestamp() : expected.ts);
    if (!actual.equals(expected)) {
        throwAssertionError("Expected record does not match actual.", dataSource, expected, ImmutableList.of(actual));
    }
}
Also used : GenericRow(io.confluent.ksql.GenericRow) GenericRecordFactory(io.confluent.ksql.engine.generic.GenericRecordFactory) Objects(java.util.Objects) GenericKey(io.confluent.ksql.GenericKey) TestRecord(org.apache.kafka.streams.test.TestRecord) DataSource(io.confluent.ksql.metastore.model.DataSource) KsqlGenericRecord(io.confluent.ksql.engine.generic.KsqlGenericRecord)

Example 2 with GenericRecordFactory

use of io.confluent.ksql.engine.generic.GenericRecordFactory in project ksql by confluentinc.

the class KsqlTesterTest method pipeInput.

private void pipeInput(final ConfiguredStatement<InsertValues> statement) {
    final InsertValues insertValues = statement.getStatement();
    final DataSource dataSource = engine.getMetaStore().getSource(insertValues.getTarget());
    if (dataSource == null) {
        throw new KsqlException("Unknown data source " + insertValues.getTarget());
    }
    final KsqlGenericRecord record = new GenericRecordFactory(config, engine.getMetaStore(), System::currentTimeMillis).build(insertValues.getColumns(), insertValues.getValues(), dataSource.getSchema(), dataSource.getDataSourceType());
    driverPipeline.pipeInput(dataSource.getKafkaTopicName(), record.key, record.value, record.ts);
}
Also used : GenericRecordFactory(io.confluent.ksql.engine.generic.GenericRecordFactory) InsertValues(io.confluent.ksql.parser.tree.InsertValues) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource) KsqlGenericRecord(io.confluent.ksql.engine.generic.KsqlGenericRecord)

Example 3 with GenericRecordFactory

use of io.confluent.ksql.engine.generic.GenericRecordFactory 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)

Aggregations

GenericRecordFactory (io.confluent.ksql.engine.generic.GenericRecordFactory)3 KsqlGenericRecord (io.confluent.ksql.engine.generic.KsqlGenericRecord)3 DataSource (io.confluent.ksql.metastore.model.DataSource)2 InsertValues (io.confluent.ksql.parser.tree.InsertValues)2 KsqlException (io.confluent.ksql.util.KsqlException)2 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)1 GenericKey (io.confluent.ksql.GenericKey)1 GenericRow (io.confluent.ksql.GenericRow)1 KsqlSchemaAuthorizationException (io.confluent.ksql.exception.KsqlSchemaAuthorizationException)1 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)1 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)1 Objects (java.util.Objects)1 ExecutionException (java.util.concurrent.ExecutionException)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)1 TestRecord (org.apache.kafka.streams.test.TestRecord)1