将Kafka与Apache Calcite集成

时间:2017-02-23 18:33:00

标签: java sql stream apache-kafka apache-calcite

我试图将方解石与Kafka整合,我反对CsvStreamableTable。

每个ConsumerRecord都使用fowlloing代码转换为Object []:

static class ArrayRowConverter extends RowConverter<Object[]> {
    private List<Schema.Field> fields;

    public ArrayRowConverter(List<Schema.Field> fields) {
        this.fields = fields;
    }

    @Override
    Object[] convertRow(ConsumerRecord<String, GenericRecord> consumerRecord) {
        Object[] objects = new Object[fields.size()+1];
        int i = 0 ;
        objects[i++] = consumerRecord.timestamp();
        for(Schema.Field field : this.fields) {
            Object obj = consumerRecord.value().get(field.name());
            if( obj instanceof Utf8 ){
                objects[i ++] = obj.toString();
            }else {
                objects[i ++] = obj;
            }
        }
        return objects;
    }
}

Enumerator实现如下,一个线程不断地从kafka轮询记录并将它们放入队列,从该队列中调用getRecord()方法:

public E current() {
    return current;
}

public boolean moveNext() {
for(;;) {
    if(cancelFlag.get()) {
        return false;
    }
    ConsumerRecord<String, GenericRecord> record = getRecord();
    if(record ==  null) {
        try {
            Thread.sleep(200L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        continue;
    }
    current = rowConvert.convertRow(record);
    return true;
    }
}

我测试SELECT STREAM * FROM Kafka.clicks,它运行正常。 rowtime是显式添加的第一列,值是Kafka的记录时间戳。

但是当我尝试

SELECT STREAM FLOOR(rowtime TO HOUR) 
AS rowtime,ip,COUNT(*) AS c FROM KAFKA.clicks  GROUP BY FLOOR(rowtime TO HOUR), ip

它引发了异常

java.sql.SQLException: Error while executing SQL "SELECT STREAM FLOOR(rowtime TO HOUR) AS rowtime,ip,COUNT(*) AS c FROM KAFKA.clicks  GROUP BY FLOOR(rowtime TO HOUR), ip": From line 1, column 85 to line 1, column 119: Streaming aggregation requires at least one monotonic expression in GROUP BY clause
    at org.apache.calcite.avatica.Helper.createException(Helper.java:56)
    at org.apache.calcite.avatica.Helper.createException(Helper.java:41)

1 个答案:

答案 0 :(得分:1)

您需要声明“ROWTIME”列是单调的。在MockCatalogReader中,请注意“ROWTIME”如何在“ORDERS”和“SHIPMENTS”流中声明为单调。这就是为什么SqlValidatorTest.testStreamGroupBy()中的某些查询有效而其他查询无效的原因。验证器所依赖的关键方法是SqlValidatorTable.getMonotonicity(String columnName)