如何在Flink 1.4.0中查询可查询状态?

时间:2018-01-30 10:08:24

标签: apache-flink flink-streaming

我觉得很难理解文档中的代码。

QueryableStateClient client = new QueryableStateClient(tmHostname, proxyPort);

// the state descriptor of the state to be fetched.
ValueStateDescriptor<Tuple2<Long, Long>> descriptor =
        new ValueStateDescriptor<>(
          "average",
          TypeInformation.of(new TypeHint<Tuple2<Long, Long>>() {}),
          Tuple2.of(0L, 0L));

CompletableFuture<ValueState<Tuple2<Long, Long>>> resultFuture =
        client.getKvState(jobId, "query-name", key, BasicTypeInfo.LONG_TYPE_INFO, descriptor);

// now handle the returned value
resultFuture.thenAccept(response -> {
        try {
            Tuple2<Long, Long> res = response.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
});

如何定义keyjobIdflink-queryable-state模块中也没有用于查看的测试用例。

2 个答案:

答案 0 :(得分:3)

jobId是可查询状态所属的作业的ID。 JobId可以从JobSubmissionResult或Flink的REST API返回的ExecutionEnvironment.execute()中获取。

可查询状态始终是键控状态,其作用类似于分布式哈希映射。 key是应该获取键控状态值的键。

答案 1 :(得分:1)

JobId可以从flink UI获得。 Running Jobs - &gt; Job ID - &gt; JobGraph jobGraph = env.getStreamGraph().getJobGraph(); System.out.println("[info] Job ID: " + jobGraph.getJobID());

flink UI overview

或者你可以通过API获得它。

Key

Integer是您为键控流指定的内容。如果您键入了如下所示的流,则密钥应为source.keyBy(new KeySelector<Tuple2<Integer, Long>, Integer>() { private static final long serialVersionUID = 8470749712274833552L; @Override public Integer getKey(Tuple2<Integer, Long> value) { return value.f0; } }).transform( "TestAggregatingOperator", BasicTypeInfo.STRING_TYPE_INFO, new AggregatingTestOperator(aggrStateDescriptor) ); 类型:

JobID jobID = JobID.fromHexString("ece5660c0e32a7d9780b8f24cd4fffc6");
    CompletableFuture<AggregatingState<Tuple2<Integer, Long>, String>> kvState = client.getKvState(jobID,
            "aggr-queryable",
            1, BasicTypeInfo.INT_TYPE_INFO,
            aggrStateDescriptor);

查询代码:

testJob

我在github上写了一个查询演示。 testQuery方法启动可查询状态服务器。和SELECT SUM (TRAN_ID) FROM Transactions_Table WHERE YEAR (TRAN_CREATE_DATE) = 2017 AND HOUR (TRAN_CREATE_DATE) = 15 方法启动可查询的状态客户端。

https://github.com/liujiawinds/queryable-state-demo/blob/master/src/test/java/com/hansight/hanstreaming/Test.java