Apache方解石:将整数转换为日期时间

时间:2019-07-19 18:34:22

标签: apache-beam apache-calcite beam-sql

我正在使用Beam SQL并尝试将整数转换为日期时间字段。

  Schema resultSchema =
    Schema.builder()
          .addInt64Field("detectedCount")
          .addStringField("sensor")
          .addInt64Field("timestamp")
          .build();

  PCollection<Row> sensorRawUnboundedTimestampedSubset = 
    sensorRowUnbounded.apply(
        SqlTransform.query(
          "select PCOLLECTION.payload.`value`.`count` detectedCount, \n"
          + "PCOLLECTION.payload.`value`.`id` sensor, \n"
          + "PCOLLECTION.`timestamp` `timestamp` \n"
          + "from PCOLLECTION "))
    .setRowSchema(resultSchema);

对于某些计算和窗口处理,我想将timestamp转换/广播为Datetime字段吗?请提供一些指针,以将timestamp中的resultSchema转换为DateTime。数据类型。

1 个答案:

答案 0 :(得分:1)

Beam(or in Calcite)中没有开箱即用的方法。简短版本-方解石或Beam无法知道如何实际存储日期或时间戳的整数形式。但是,假设您有纪元,这应该可以:

@Test
public void testBlah() throws Exception {
  // input schema, has timestamps as epoch millis
  Schema schema = Schema.builder().addInt64Field("ts").addStringField("st").build();

  DateTime ts1 = new DateTime(2019, 8, 9, 10, 11, 12);
  DateTime ts2 = new DateTime(2019, 8, 9, 10, 11, 12);

  PCollection<Row> input =
    pipeline
      .apply(
          "createRows",
          Create.of(
              Row.withSchema(schema).addValues(ts1.getMillis(), "two").build(),
              Row.withSchema(schema).addValues(ts2.getMillis(), "twelve").build()))
      .setRowSchema(schema);

  PCollection<Row> result =
    input.apply(
      SqlTransform.query(
          "SELECT \n"
          + "(TIMESTAMP '1970-01-01 00:00:00' + ts * INTERVAL '0.001' SECOND) as ts, \n"
          + "st \n"
          + "FROM \n"
          + "PCOLLECTION"));

  // output schema, has timestamps as DateTime
  Schema outSchema = Schema.builder().addDateTimeField("ts").addStringField("st").build();
  PAssert.that(result)
    .containsInAnyOrder(
        Row.withSchema(outSchema).addValues(ts1, "two").build(),
        Row.withSchema(outSchema).addValues(ts2, "twelve").build());
  pipeline.run();
}

或者,您始终可以在Java中而不是在SQL中进行操作,只需将自定义ParDo应用于SqlTransform的输出即可。在该ParDo中,从Row对象中提取整数时间戳,将其转换为DateTime,然后发出,例如作为具有不同架构的另一行的一部分。