I try to query data from Google BigQuery via JDBC driver, but some values seems to be incorrect.
Steps:
Download Simba JDBC driver for googlebigquery freely from https://storage.googleapis.com/simba-bq-release/jdbc/SimbaJDBCDriverforGoogleBigQuery42_1.2.2.1004.zip
Loading the driver to your Java App
Connect the google query via jdbc The sample codes are below:
public static void main(String[] args) {
Connection conn = null;
String url ="jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" +
"ProjectId=fedjdbc;OAuthType=0;DefaultDataset=jdbc;" +
"[email protected];" +
"OAuthPvtKeyPath=/Users/laptop/workspace/bigquery/accounts/fedjdbc-5cb29b8a02fb.json";
try {
Class.forName("com.simba.googlebigquery.jdbc42.Driver");
conn = DriverManager.getConnection(url);
query(conn);
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void query(Connection conn) {
String sql = "select * from numbers1";
sql = "select * from numbers1";
sql = "select timestamp(\"0001-01-01 00:00:00 UTC\") from jdbc.test_date";
try {
PreparedStatement stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
System.out.println("query begin");
java.util.Date date = new java.util.Date();
System.out.println(System.currentTimeMillis());
ResultSet rs = stmt.executeQuery();
System.out.println(System.currentTimeMillis());
System.out.println("query end");
while (rs.next()) {
System.out.println(rs.getString(1));
//System.out.println(rs.getTimestamp(1));
}
rs.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
You can find that the result is not "0001-01-01 00:00:00.000000" but "0001-01-03 08:00:00.000000".
Do you know why?
The timezone convert the data locally?
I test it in some hosts with different timezones, same issues.
getString(..)
does (getTimestamp(..).toString()
), which given the requirements and implementations ofjava.sql.Timestamp
can have additional problems. – Mark Rotteveel