Uploaded image for project: 'Beam'
  1. Beam
  2. BEAM-13374

ToJson() of a Row with a DateTime field fails with an exception

Details

    Description

      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.

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

       

      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(); 

       

      Attachments

        Activity

          People

            andreigurau Andrei Gurau
            slilichenko Sergei Lilichenko
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: