You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@beam.apache.org by "Sergei Lilichenko (Jira)" <ji...@apache.org> on 2021/12/02 22:42:00 UTC

[jira] [Created] (BEAM-13374) ToJson() of a Row with a DateTime field fails with an exception

Sergei Lilichenko created BEAM-13374:
----------------------------------------

             Summary: ToJson() of a Row with a DateTime field fails with an exception
                 Key: BEAM-13374
                 URL: https://issues.apache.org/jira/browse/BEAM-13374
             Project: Beam
          Issue Type: Bug
          Components: beam-model
    Affects Versions: 2.33.0
            Reporter: Sergei Lilichenko


The following code fails with "class org.joda.time.Instant cannot be cast to class org.joda.time.DateTime" exception at ToJson.of() transform. 

The root cause of it appears to be [this conversion to Instant|https://github.com/apache/beam/blob/85a122735f84c0ee46ba0fb583d9ff9e05dcf2fc/sdks/java/core/src/main/java/org/apache/beam/sdk/values/RowUtils.java#L554].

This bug appeared in a JDBC processing pipeline where a TIMESTAMP column is part of the result set retrieved using JdbcIO.readRows(). 

 
{code:java}
Pipeline p = Pipeline.create();
Schema schema = Schema.of(Field.of("timestamp", FieldType.DATETIME));
p
    .apply("DateTime values", Create.of(new DateTime()))
    .apply("To Row", ParDo.of(new DoFn<DateTime, Row>() {
      @ProcessElement
      public void toRow(@Element DateTime dateTime, OutputReceiver<Row> rowOutputReceiver) {
        rowOutputReceiver.output(
            Row.withSchema(schema)
                .withFieldValue("timestamp", dateTime)
                .build());
      }
    }))
    .setCoder(RowCoder.of(schema))
    .apply("To Json", ToJson.of())
    .apply("Print to Console", ParDo.of(new DoFn<String, Void>() {
      @ProcessElement
      public void print(@Element String expectedJson) {
        System.out.println("JSON: " + expectedJson);
      }
    }));
p.run().waitUntilFinish(); {code}
 



--
This message was sent by Atlassian Jira
(v8.20.1#820001)