简体   繁体   English

在 Scala 中使用 Flink 的 leftOuterJoinLateral 时出现 NullPointerException 异常

[英]NullPointerException exception when using Flink's leftOuterJoinLateral in Scala

I am trying to follow the documentation and create a Table Function to "flatten" some data.我正在尝试遵循文档并创建一个表 Function 来“展平”一些数据。 The Table Function seems to work fine when using the joinLateral to do the flattening.使用joinLateral进行展平时,表 Function 似乎工作正常。 When using leftOuterJoinLateral though, I get the following error.但是,当使用leftOuterJoinLateral时,我收到以下错误。 I'm using Scala and have tried both Table API and SQL with the same result:我正在使用 Scala 并尝试了表 API 和 SQL ,结果相同:

Caused by: java.lang.NullPointerException: Null result cannot be stored in a Case Class.原因:java.lang.NullPointerException:Null 结果不能存储在案例 Class 中。

Here is my job:这是我的工作:

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.scala.StreamTableEnvironment
import org.apache.flink.table.api.scala._
import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.functions.TableFunction

object example_job{
  // Split the List[Int] into multiple rows
  class Split() extends TableFunction[Int] {
    def eval(nums: List[Int]): Unit = {
      nums.foreach(x =>
        if(x != 3) {
          collect(x)
      })
    }
  }

  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.createLocalEnvironment()
    val tableEnv = StreamTableEnvironment.create(env)
    val splitMe = new Split()

    // Create some dummy data
    val events: DataStream[(String, List[Int])] = env.fromElements(("simon", List(1,2,3)), ("jessica", List(3)))
    
    val table = tableEnv.fromDataStream(events, 'name, 'numbers)
      .leftOuterJoinLateral(splitMe('numbers) as 'number)
      .select('name, 'number)
    table.toAppendStream[(String, Int)].print()
    env.execute("Flink jira ticket example")
  }
}

When I change .leftOuterJoinLateral to .joinLateral I get the expected result:当我将.leftOuterJoinLateral更改为.joinLateral时,我得到了预期的结果:

(simon,1)
(simon,2)

When using the .leftOuterJoinLateral I would expect something like:使用.leftOuterJoinLateral时,我会期望类似:

(simon,1)
(simon,2)
(simon,null) // or (simon, None)
(jessica,null) // or (jessica, None)

Seems like this might be a bug with the Scala API?似乎这可能是 Scala API 的错误? I wanted to check here first before raising a ticket just in case I'm doing something stupid!我想在提出罚单之前先检查这里,以防我做一些愚蠢的事情!

The problem is that Flink per default does expect that all fields of a row are non-null.问题是 Flink 默认情况下确实期望一行的所有字段都是非空的。 That's why the program fails when it sees the null result from the outer join operation.这就是程序在看到外连接操作的null结果时失败的原因。 In order to accept null values, you either need to disable the null check via为了接受null值,您需要通过以下方式禁用 null 检查

val tableConfig = tableEnv.getConfig
tableConfig.setNullCheck(false)

Or you must specify the result type to tolerate null values, eg specifying a custom POJO output type:或者您必须指定结果类型以容忍 null 值,例如指定自定义 POJO output 类型:

table.toAppendStream[MyOutput].print()

with

class MyOutput(var name: String, var number: Integer) {
  def this() {
    this(null, null)
  }

  override def toString: String = s"($name, $number)"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Scala (2.12.8) 中使用 case 类时的 Apache flink (1.9.1) 运行时异常 - Apache flink (1.9.1) runtime exception when using case classes in scala (2.12.8) 使用带有自定义对象数组的数据集时,Flink中的NoSuchMethod异常 - NoSuchMethod exception in Flink when using dataset with custom object array 如何在Scala中使用Flink的KafkaSource? - How to use Flink's KafkaSource in Scala? 无法使用 flink 在 Scala 中实例化用户函数 - Cannot instantiate user function in scala using flink 使用scala sbt对kafka + flink示例进行故障排除? - troubleshooting kafka + flink example using scala sbt? 使用 scala 案例类时,Flink 不允许 POJO object 的不可变样式设置器 - Flink does not allow Immutable style setters for POJO object when using scala case classes 当我使用SparkStreaming消耗Kafka的消息时,得到NullPointerException - Got NullPointerException, when I using SparkStreaming to consume the Kafka's messages 使用播放框架在Scala中的Actors中出现奇怪的NullPointerException - Strange NullPointerException in Actors in scala using play framework 使用kafka(或scala一般?)时使用sonarqube扫描的例外情况? - Exception from sonarqube scan when using using kafka (or scala in general?) Scala 中的 Flink:尝试将 map 应用于 DataStreamSource 时出现问题 - Flink in Scala: Problem with map when trying to apply it to a DataStreamSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM