集成kafka和apache方解石

xzlaal3s  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(450)

我试图把方解石和Kafka结合起来,我引用了csvstreamable。
每个consumerrecord都使用fowlloing代码转换为对象[]:

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;
    }
}

枚举器的实现如下,一个线程不断地轮询来自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)
ryhaxcpt

ryhaxcpt1#

您需要声明“rowtime”列是单调的。在 MockCatalogReader ,注意“orders”和“shippings”流中“rowtime”是如何声明为单调的。这就是为什么有些查询 SqlValidatorTest.testStreamGroupBy() 是有效的,其他的不是。验证器依赖的关键方法是 SqlValidatorTable.getMonotonicity(String columnName) .

相关问题