Search in sources :

Example 16 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class Analyzer method analyzeNonStdOutSink.

private void analyzeNonStdOutSink() {
    List<Pair<StructuredDataSource, String>> fromDataSources = analysis.getFromDataSources();
    StructuredDataSource intoStructuredDataSource = analysis.getInto();
    String intoKafkaTopicName = analysis.getIntoKafkaTopicName();
    if (intoKafkaTopicName == null) {
        intoKafkaTopicName = intoStructuredDataSource.getName();
    }
    KsqlTopicSerDe intoTopicSerde = fromDataSources.get(0).getLeft().getKsqlTopic().getKsqlTopicSerDe();
    if (analysis.getIntoFormat() != null) {
        switch(analysis.getIntoFormat().toUpperCase()) {
            case DataSource.AVRO_SERDE_NAME:
                intoTopicSerde = new KsqlAvroTopicSerDe();
                break;
            case DataSource.JSON_SERDE_NAME:
                intoTopicSerde = new KsqlJsonTopicSerDe();
                break;
            case DataSource.DELIMITED_SERDE_NAME:
                intoTopicSerde = new KsqlDelimitedTopicSerDe();
                break;
            default:
                throw new KsqlException(String.format("Unsupported format: %s", analysis.getIntoFormat()));
        }
    } else {
        if (intoTopicSerde instanceof KsqlAvroTopicSerDe) {
            intoTopicSerde = new KsqlAvroTopicSerDe();
        }
    }
    KsqlTopic newIntoKsqlTopic = new KsqlTopic(intoKafkaTopicName, intoKafkaTopicName, intoTopicSerde);
    KsqlStream intoKsqlStream = new KsqlStream(sqlExpression, intoStructuredDataSource.getName(), null, null, null, newIntoKsqlTopic);
    analysis.setInto(intoKsqlStream);
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) KsqlStream(io.confluent.ksql.metastore.KsqlStream) KsqlDelimitedTopicSerDe(io.confluent.ksql.serde.delimited.KsqlDelimitedTopicSerDe) KsqlAvroTopicSerDe(io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) KsqlJsonTopicSerDe(io.confluent.ksql.serde.json.KsqlJsonTopicSerDe) KsqlException(io.confluent.ksql.util.KsqlException) Pair(io.confluent.ksql.util.Pair) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic)

Example 17 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class Analyzer method visitAliasedRelation.

@Override
protected Node visitAliasedRelation(AliasedRelation node, AnalysisContext context) {
    String structuredDataSourceName = ((Table) node.getRelation()).getName().getSuffix();
    if (metaStore.getSource(structuredDataSourceName) == null) {
        throw new KsqlException(structuredDataSourceName + " does not exist.");
    }
    StructuredDataSource structuredDataSource = metaStore.getSource(structuredDataSourceName);
    if (((Table) node.getRelation()).getProperties() != null) {
        if (((Table) node.getRelation()).getProperties().get(DdlConfig.TIMESTAMP_NAME_PROPERTY) != null) {
            String timestampFieldName = ((Table) node.getRelation()).getProperties().get(DdlConfig.TIMESTAMP_NAME_PROPERTY).toString().toUpperCase();
            if (!timestampFieldName.startsWith("'") && !timestampFieldName.endsWith("'")) {
                throw new KsqlException("Property name should be String with single qoute.");
            }
            timestampFieldName = timestampFieldName.substring(1, timestampFieldName.length() - 1);
            structuredDataSource = structuredDataSource.cloneWithTimeField(timestampFieldName);
        }
    }
    Pair<StructuredDataSource, String> fromDataSource = new Pair<>(structuredDataSource, node.getAlias());
    analysis.addDataSource(fromDataSource);
    return node;
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) Table(io.confluent.ksql.parser.tree.Table) KsqlException(io.confluent.ksql.util.KsqlException) Pair(io.confluent.ksql.util.Pair)

Example 18 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class SchemaKStream method createSelectValueMapperAndSchema.

Pair<Schema, SelectValueMapper> createSelectValueMapperAndSchema(final List<Pair<String, Expression>> expressionPairList) {
    try {
        final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, functionRegistry);
        final SchemaBuilder schemaBuilder = SchemaBuilder.struct();
        final List<ExpressionMetadata> expressionEvaluators = new ArrayList<>();
        for (Pair<String, Expression> expressionPair : expressionPairList) {
            final ExpressionMetadata expressionEvaluator = codeGenRunner.buildCodeGenFromParseTree(expressionPair.getRight());
            schemaBuilder.field(expressionPair.getLeft(), expressionEvaluator.getExpressionType());
            expressionEvaluators.add(expressionEvaluator);
        }
        return new Pair<>(schemaBuilder.build(), new SelectValueMapper(genericRowValueTypeEnforcer, expressionPairList, expressionEvaluators));
    } catch (Exception e) {
        throw new KsqlException("Code generation failed for SelectValueMapper", e);
    }
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) CodeGenRunner(io.confluent.ksql.codegen.CodeGenRunner) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) ArrayList(java.util.ArrayList) KsqlException(io.confluent.ksql.util.KsqlException) KsqlException(io.confluent.ksql.util.KsqlException) Pair(io.confluent.ksql.util.Pair)

Example 19 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class TestUtils method getAllPriorCommandRecords.

public List<Pair<CommandId, Command>> getAllPriorCommandRecords() {
    List<Pair<CommandId, Command>> priorCommands = new ArrayList<>();
    Command topicCommand = new Command("REGISTER TOPIC pageview_topic WITH " + "(value_format = 'json', " + "kafka_topic='pageview_topic_json');", new HashMap<>());
    CommandId topicCommandId = new CommandId(CommandId.Type.TOPIC, "_CSASTopicGen", CommandId.Action.CREATE);
    priorCommands.add(new Pair<>(topicCommandId, topicCommand));
    Command csCommand = new Command("CREATE STREAM pageview " + "(viewtime bigint, pageid varchar, userid varchar) " + "WITH (registered_topic = 'pageview_topic');", new HashMap<>());
    CommandId csCommandId = new CommandId(CommandId.Type.STREAM, "_CSASStreamGen", CommandId.Action.CREATE);
    priorCommands.add(new Pair<>(csCommandId, csCommand));
    Command csasCommand = new Command("CREATE STREAM user1pv " + " AS select * from pageview WHERE userid = 'user1';", new HashMap<>());
    CommandId csasCommandId = new CommandId(CommandId.Type.STREAM, "_CSASGen", CommandId.Action.CREATE);
    priorCommands.add(new Pair<>(csasCommandId, csasCommand));
    Command ctasCommand = new Command("CREATE TABLE user1pvtb " + " AS select * from pageview window tumbling(size 5 " + "second) WHERE userid = " + "'user1' group by pageid;", new HashMap<>());
    CommandId ctasCommandId = new CommandId(CommandId.Type.TABLE, "_CTASGen", CommandId.Action.CREATE);
    priorCommands.add(new Pair<>(ctasCommandId, ctasCommand));
    return priorCommands;
}
Also used : Command(io.confluent.ksql.rest.server.computation.Command) ArrayList(java.util.ArrayList) CommandId(io.confluent.ksql.rest.server.computation.CommandId) Pair(io.confluent.ksql.util.Pair)

Aggregations

Pair (io.confluent.ksql.util.Pair)19 ArrayList (java.util.ArrayList)8 KsqlException (io.confluent.ksql.util.KsqlException)6 MetaStore (io.confluent.ksql.metastore.MetaStore)3 Statement (io.confluent.ksql.parser.tree.Statement)3 PlanNode (io.confluent.ksql.planner.plan.PlanNode)3 Test (org.junit.Test)3 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)2 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)2 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)2 AbstractStreamCreateStatement (io.confluent.ksql.parser.tree.AbstractStreamCreateStatement)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 CreateTable (io.confluent.ksql.parser.tree.CreateTable)2 DdlStatement (io.confluent.ksql.parser.tree.DdlStatement)2 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)2 Expression (io.confluent.ksql.parser.tree.Expression)2 Query (io.confluent.ksql.parser.tree.Query)2 ProjectNode (io.confluent.ksql.planner.plan.ProjectNode)2 TestUtils (io.confluent.ksql.rest.server.utils.TestUtils)2 KsqlTopicSerDe (io.confluent.ksql.serde.KsqlTopicSerDe)2