Apache Flink Table 查询结果作为字符串值
Apache Flink Table query result as string values
我正在从 flink table api 编写查询以检索记录。然后检查是否找到记录,如果找到,则获取每个记录列值的字符串值。
即
users:
|id | name | phone |
|---|------|-------|
| 01| sam | 23354 |
| 02| jake | 23352 |
| 03| kim | 23351 |
问题是 flink 仅 returns Table 来自查询,所以我无法 1:检查是否找到记录和 2:获取找到的记录值的各个值
sudo 代码:
foundRecord = find record by phone
if foundRecord {
create new instance of Visitor
Visitor.name = foundRecord.name
Visitor.id = foundRecord.id
} else {
throw exception
}
flink 文档推荐的下面的代码给了我一个 table 但不确定如何实现上面的 sudo 代码,因为它作为另一个 table 返回,我需要实际的记录值。
Table users = registeredUsers.select("id, name, phone").where("phone === '23354'"));
Flink 文档供参考:https://ci.apache.org/projects/flink/flink-docs-stable/dev/table/tableApi.html#expression-syntax
为了知道找不到匹配的记录,输入必须有界——所以我们将使用 BatchTableEnvironment
,而不是 StreamTableEnvironment
。 (使用流式输入,最终可能会匹配到记录并满足查询。只有使用批输入才能证明不匹配。)
import org.apache.flink.api.common.functions.FlatMapFunction
import org.apache.flink.api.scala.ExecutionEnvironment
import org.apache.flink.table.api.scala.BatchTableEnvironment
import org.apache.flink.types.Row
import org.apache.flink.api.scala._
import org.apache.flink.table.api.scala._
import org.apache.flink.util.Collector
class MissingResultException() extends Exception {}
object Phone {
case class Visitor(name: String, id: String)
@throws[Exception]
def main(args: Array[String]): Unit = {
val env = ExecutionEnvironment.getExecutionEnvironment
val tableEnv = BatchTableEnvironment.create(env)
val rawInput = env.fromElements(
("01", "sam", "23354"),
("02", "jake", "23352"),
("03", "kim", "23351"))
val events = tableEnv.fromDataSet(rawInput, 'id, 'name, 'phone)
tableEnv.registerTable("events", events)
val resultTable = tableEnv
.from("events")
.select('id, 'name, 'phone)
.where("phone === 'missing'")
val results = resultTable.toDataSet[Row]
results
.map(row => new Visitor(row.getField(1).toString, row.getField(0).toString))
.print
val count: DataSet[Long] = env.fromElements(results.count())
count
.flatMap(new FlatMapFunction[Long, Collector[Long]]{
override def flatMap(x: Long, collector: Collector[Collector[Long]]): Unit = {
if (x == 0L) {
throw new MissingResultException
}
}})
.print()
}
}
我用来检测结果集是否为空的方法感觉有点像 hack,但我想不出更好的方法。请注意,最后的 print()
是必需的,尽管没有什么可打印的,因为任何未最终馈送到接收器的计算都将被优化掉,并且不会执行。
我正在从 flink table api 编写查询以检索记录。然后检查是否找到记录,如果找到,则获取每个记录列值的字符串值。
即
users:
|id | name | phone |
|---|------|-------|
| 01| sam | 23354 |
| 02| jake | 23352 |
| 03| kim | 23351 |
问题是 flink 仅 returns Table 来自查询,所以我无法 1:检查是否找到记录和 2:获取找到的记录值的各个值
sudo 代码:
foundRecord = find record by phone
if foundRecord {
create new instance of Visitor
Visitor.name = foundRecord.name
Visitor.id = foundRecord.id
} else {
throw exception
}
flink 文档推荐的下面的代码给了我一个 table 但不确定如何实现上面的 sudo 代码,因为它作为另一个 table 返回,我需要实际的记录值。
Table users = registeredUsers.select("id, name, phone").where("phone === '23354'"));
Flink 文档供参考:https://ci.apache.org/projects/flink/flink-docs-stable/dev/table/tableApi.html#expression-syntax
为了知道找不到匹配的记录,输入必须有界——所以我们将使用 BatchTableEnvironment
,而不是 StreamTableEnvironment
。 (使用流式输入,最终可能会匹配到记录并满足查询。只有使用批输入才能证明不匹配。)
import org.apache.flink.api.common.functions.FlatMapFunction
import org.apache.flink.api.scala.ExecutionEnvironment
import org.apache.flink.table.api.scala.BatchTableEnvironment
import org.apache.flink.types.Row
import org.apache.flink.api.scala._
import org.apache.flink.table.api.scala._
import org.apache.flink.util.Collector
class MissingResultException() extends Exception {}
object Phone {
case class Visitor(name: String, id: String)
@throws[Exception]
def main(args: Array[String]): Unit = {
val env = ExecutionEnvironment.getExecutionEnvironment
val tableEnv = BatchTableEnvironment.create(env)
val rawInput = env.fromElements(
("01", "sam", "23354"),
("02", "jake", "23352"),
("03", "kim", "23351"))
val events = tableEnv.fromDataSet(rawInput, 'id, 'name, 'phone)
tableEnv.registerTable("events", events)
val resultTable = tableEnv
.from("events")
.select('id, 'name, 'phone)
.where("phone === 'missing'")
val results = resultTable.toDataSet[Row]
results
.map(row => new Visitor(row.getField(1).toString, row.getField(0).toString))
.print
val count: DataSet[Long] = env.fromElements(results.count())
count
.flatMap(new FlatMapFunction[Long, Collector[Long]]{
override def flatMap(x: Long, collector: Collector[Collector[Long]]): Unit = {
if (x == 0L) {
throw new MissingResultException
}
}})
.print()
}
}
我用来检测结果集是否为空的方法感觉有点像 hack,但我想不出更好的方法。请注意,最后的 print()
是必需的,尽管没有什么可打印的,因为任何未最终馈送到接收器的计算都将被优化掉,并且不会执行。