Search in sources :

Example 1 with FieldInfo

use of org.apache.storm.sql.runtime.FieldInfo in project storm by apache.

the class StormSqlImpl method handleCreateTableForTrident.

private void handleCreateTableForTrident(SqlCreateTable n, Map<String, ISqlTridentDataSource> dataSources) {
    List<FieldInfo> fields = updateSchema(n);
    ISqlTridentDataSource ds = DataSourcesRegistry.constructTridentDataSource(n.location(), n.inputFormatClass(), n.outputFormatClass(), n.properties(), fields);
    if (ds == null) {
        throw new RuntimeException("Failed to find data source for " + n.tableName() + " URI: " + n.location());
    } else if (dataSources.containsKey(n.tableName())) {
        throw new RuntimeException("Duplicated definition for table " + n.tableName());
    }
    dataSources.put(n.tableName(), ds);
}
Also used : ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) FieldInfo(org.apache.storm.sql.runtime.FieldInfo)

Example 2 with FieldInfo

use of org.apache.storm.sql.runtime.FieldInfo in project storm by apache.

the class StormSqlImpl method updateSchema.

private List<FieldInfo> updateSchema(SqlCreateTable n) {
    TableBuilderInfo builder = new TableBuilderInfo(typeFactory);
    List<FieldInfo> fields = new ArrayList<>();
    for (ColumnDefinition col : n.fieldList()) {
        builder.field(col.name(), col.type(), col.constraint());
        RelDataType dataType = col.type().deriveType(typeFactory);
        Class<?> javaType = (Class<?>) typeFactory.getJavaClass(dataType);
        ColumnConstraint constraint = col.constraint();
        boolean isPrimary = constraint != null && constraint instanceof ColumnConstraint.PrimaryKey;
        fields.add(new FieldInfo(col.name(), javaType, isPrimary));
    }
    if (n.parallelism() != null) {
        builder.parallelismHint(n.parallelism());
    }
    Table table = builder.build();
    schema.add(n.tableName(), table);
    return fields;
}
Also used : Table(org.apache.calcite.schema.Table) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) SqlCreateTable(org.apache.storm.sql.parser.SqlCreateTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) TableBuilderInfo(org.apache.storm.sql.compiler.CompilerUtil.TableBuilderInfo) ColumnConstraint(org.apache.storm.sql.parser.ColumnConstraint) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) FieldInfo(org.apache.storm.sql.runtime.FieldInfo) ColumnDefinition(org.apache.storm.sql.parser.ColumnDefinition)

Example 3 with FieldInfo

use of org.apache.storm.sql.runtime.FieldInfo in project storm by apache.

the class KafkaDataSourcesProvider method constructTrident.

@Override
public ISqlTridentDataSource constructTrident(URI uri, String inputFormatClass, String outputFormatClass, Properties properties, List<FieldInfo> fields) {
    int port = uri.getPort() != -1 ? uri.getPort() : DEFAULT_ZK_PORT;
    ZkHosts zk = new ZkHosts(uri.getHost() + ":" + port, uri.getPath());
    Map<String, String> values = parseURIParams(uri.getQuery());
    String topic = values.get("topic");
    Preconditions.checkNotNull(topic, "No topic of the spout is specified");
    TridentKafkaConfig conf = new TridentKafkaConfig(zk, topic);
    List<String> fieldNames = new ArrayList<>();
    int primaryIndex = -1;
    for (int i = 0; i < fields.size(); ++i) {
        FieldInfo f = fields.get(i);
        fieldNames.add(f.name());
        if (f.isPrimary()) {
            primaryIndex = i;
        }
    }
    Preconditions.checkState(primaryIndex != -1, "Kafka stream table must have a primary key");
    Scheme scheme = SerdeUtils.getScheme(inputFormatClass, properties, fieldNames);
    conf.scheme = new SchemeAsMultiScheme(scheme);
    IOutputSerializer serializer = SerdeUtils.getSerializer(outputFormatClass, properties, fieldNames);
    return new KafkaTridentDataSource(conf, topic, primaryIndex, properties, serializer);
}
Also used : IOutputSerializer(org.apache.storm.sql.runtime.IOutputSerializer) SchemeAsMultiScheme(org.apache.storm.spout.SchemeAsMultiScheme) Scheme(org.apache.storm.spout.Scheme) ZkHosts(org.apache.storm.kafka.ZkHosts) ArrayList(java.util.ArrayList) TridentKafkaConfig(org.apache.storm.kafka.trident.TridentKafkaConfig) SchemeAsMultiScheme(org.apache.storm.spout.SchemeAsMultiScheme) FieldInfo(org.apache.storm.sql.runtime.FieldInfo)

Example 4 with FieldInfo

use of org.apache.storm.sql.runtime.FieldInfo in project storm by apache.

the class StormSqlImpl method handleCreateTable.

private void handleCreateTable(SqlCreateTable n, Map<String, DataSource> dataSources) {
    List<FieldInfo> fields = updateSchema(n);
    DataSource ds = DataSourcesRegistry.construct(n.location(), n.inputFormatClass(), n.outputFormatClass(), fields);
    if (ds == null) {
        throw new RuntimeException("Cannot construct data source for " + n.tableName());
    } else if (dataSources.containsKey(n.tableName())) {
        throw new RuntimeException("Duplicated definition for table " + n.tableName());
    }
    dataSources.put(n.tableName(), ds);
}
Also used : FieldInfo(org.apache.storm.sql.runtime.FieldInfo) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) DataSource(org.apache.storm.sql.runtime.DataSource)

Example 5 with FieldInfo

use of org.apache.storm.sql.runtime.FieldInfo in project storm by apache.

the class KafkaDataSourcesProvider method constructStreams.

@Override
public ISqlStreamsDataSource constructStreams(URI uri, String inputFormatClass, String outputFormatClass, Properties properties, List<FieldInfo> fields) {
    List<String> fieldNames = new ArrayList<>();
    int primaryIndex = -1;
    for (int i = 0; i < fields.size(); ++i) {
        FieldInfo f = fields.get(i);
        fieldNames.add(f.name());
        if (f.isPrimary()) {
            primaryIndex = i;
        }
    }
    Preconditions.checkState(primaryIndex != -1, "Kafka stream table must have a primary key");
    Scheme scheme = SerdeUtils.getScheme(inputFormatClass, properties, fieldNames);
    Map<String, String> values = parseUriParams(uri.getQuery());
    String bootstrapServers = values.get(URI_PARAMS_BOOTSTRAP_SERVERS);
    Preconditions.checkNotNull(bootstrapServers, "bootstrap-servers must be specified");
    String topic = uri.getHost();
    KafkaSpoutConfig<ByteBuffer, ByteBuffer> kafkaSpoutConfig = new KafkaSpoutConfig.Builder<ByteBuffer, ByteBuffer>(bootstrapServers, topic).setProp(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteBufferDeserializer.class).setProp(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteBufferDeserializer.class).setProp(ConsumerConfig.GROUP_ID_CONFIG, "storm-sql-kafka-" + UUID.randomUUID().toString()).setRecordTranslator(new RecordTranslatorSchemeAdapter(scheme)).build();
    IOutputSerializer serializer = SerdeUtils.getSerializer(outputFormatClass, properties, fieldNames);
    return new KafkaStreamsDataSource(kafkaSpoutConfig, bootstrapServers, topic, properties, serializer);
}
Also used : Scheme(org.apache.storm.spout.Scheme) ByteBufferDeserializer(org.apache.kafka.common.serialization.ByteBufferDeserializer) ArrayList(java.util.ArrayList) KafkaSpoutConfig(org.apache.storm.kafka.spout.KafkaSpoutConfig) ByteBuffer(java.nio.ByteBuffer) IOutputSerializer(org.apache.storm.sql.runtime.IOutputSerializer) FieldInfo(org.apache.storm.sql.runtime.FieldInfo)

Aggregations

FieldInfo (org.apache.storm.sql.runtime.FieldInfo)6 ArrayList (java.util.ArrayList)4 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Table (org.apache.calcite.schema.Table)2 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)2 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)2 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)2 Scheme (org.apache.storm.spout.Scheme)2 ColumnConstraint (org.apache.storm.sql.parser.ColumnConstraint)2 ColumnDefinition (org.apache.storm.sql.parser.ColumnDefinition)2 SqlCreateTable (org.apache.storm.sql.parser.SqlCreateTable)2 IOutputSerializer (org.apache.storm.sql.runtime.IOutputSerializer)2 ISqlTridentDataSource (org.apache.storm.sql.runtime.ISqlTridentDataSource)2 ByteBuffer (java.nio.ByteBuffer)1 ByteBufferDeserializer (org.apache.kafka.common.serialization.ByteBufferDeserializer)1 ZkHosts (org.apache.storm.kafka.ZkHosts)1 KafkaSpoutConfig (org.apache.storm.kafka.spout.KafkaSpoutConfig)1 TridentKafkaConfig (org.apache.storm.kafka.trident.TridentKafkaConfig)1 SchemeAsMultiScheme (org.apache.storm.spout.SchemeAsMultiScheme)1 CompilerUtil (org.apache.storm.sql.compiler.CompilerUtil)1