ES sink 没有数据写入 ES

时间:2021-04-30 03:20:10

标签: apache-flink

我使用的是 Flink 1.12 和 ES 7.10.2。

我有以下简单的代码片段想要将一些数据写入 ES。但是在应用程序运行之后 (成功,没有报错),没有数据写入ES。

更新 如果我在 env.execute 后几秒钟睡觉,那么我可以成功写入 ES。我会问为什么会发生这种情况。我认为如果源和接收器未完成运行,env.execute 将不会返回。

test("elastic search 7 update or replace") {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)
    val tenv = StreamTableEnvironment.create(env)

    val ddl =
      """
      CREATE TABLE es_stocks (
          id STRING,
          a STRING,
          b STRING,
          PRIMARY KEY (id) NOT ENFORCED
      ) WITH (
        'connector' = 'elasticsearch-7',
        'hosts' = 'http://localhost:9200',
        'index' = 'stocks5'
      )

      """.stripMargin(' ')

    tenv.executeSql(ddl)

    val ds = env.fromElements[(String, String, String)](("id-1", "a1", null), ("id-1", null, "b1"))

    ds.print()

    tenv.createTemporaryView("sourceView", ds, $"id", $"a", $"b")



    val sql =
      """
      insert into es_stocks
      select id,a,b
      from sourceView
      """.stripMargin(' ')


    tenv.executeSql(sql)


    env.execute()

    //UPDATE
    //If I add the following code that waits for a while, then it can write to ES 
    //successfully

    Thread.sleep(10*1000)

  }

0 个答案:

没有答案