Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot write date in BigQuery using Java Bigquery Client API

I'm doing some ETL from a CSV file in GCS to BQ, everything works fine, except for dates. The field name in my table is TEST_TIME and the type is DATE, so in the TableRow I tried passing a java.util.Date, a com.google.api.client.util.DateTime, a String, a Long value with the number of seconds, but none worked.
I got error messages like these: Could not convert non-string JSON value to DATE type. Field: TEST_TIME; Value: ...
When using DateTime I got this error: JSON object specified for non-record field: TEST_TIME.

//tableRow.set("TEST_TIME", date);
//tableRow.set("TEST_TIME", new DateTime(date));
//tableRow.set("TEST_TIME", date.getTime()/1000);
//tableRow.set("TEST_TIME", dateFormatter.format(date)); //e.g. 05/06/2016
like image 978
CCC Avatar asked Apr 06 '17 15:04

CCC


People also ask

How do I add a date column in BigQuery?

Adding/Subtracting Dates To perform any kind of data transformations like adding a year to a date, or subtracting 2 weeks, then we can use the [date_part]_ADD and [date_part]_SUB functions. To add an interval to a date/time in BigQuery we can use any of: DATE_ADD(date_expression, INTERVAL int64_expression part)

What is the date format in BigQuery?

The date in the format %m/%d/%y. The day of the month as a decimal number (01-31). The day of month as a decimal number (1-31); single digits are preceded by a space. The date in the format %Y-%m-%d.


2 Answers

I think that you're expected to pass a String in the format YYYY-MM-DD, which is similar to if you were using the REST API directly with JSON. Try this:

tableRow.set("TEST_TIME", "2017-04-06");

If that works, then you can convert the actual date that you have to that format and it should also work.

like image 60
Elliott Brossard Avatar answered Sep 21 '22 12:09

Elliott Brossard


While working with google cloud dataflow, I used a wrapper from Google for timestamp - com.google.api.client.util.DateTime.

This worked for me while inserting rows into Big Query tables. So, instead of

tableRow.set("TEST_TIME" , "2017-04-07");

I would recommend

tableRow.set("TEST_TIME" , new DateTime(new Date()));

I find this to be a lot cleaner than passing timestamp as a string.

like image 29
Paritosh Avatar answered Sep 24 '22 12:09

Paritosh