Skip to content
Advertisement

Iterate inside BigQuery errors[] collection in Java

When I execute this Java code :

(note : string parameters are dummy examples)

package bigquery.test;

import java.io.FileInputStream;
import java.io.InputStream;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableId;

public class GoogleBigQueryErrors {

    static BigQuery __bigquery__;

    public static void main(String[] args) {
        try {
            prepare();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        TableId tableId = com.google.cloud.bigquery.TableId.of("DATA_SET", "TABLE");
        Table table = __bigquery__.getTable(tableId);

        Job job = table.load(com.google.cloud.bigquery.FormatOptions.json(),
                "gs://URI_PATH/HASH.bigquery.json");
        Job completedJob = null;
        try {
            completedJob = job.waitFor(); // HERE THE ERROR IS RAISED
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    }

    private static void prepare() throws Exception {
        String projectId = "PROJECT_ID";
        String credentialsFile = "PATH/FILE.json";
        InputStream credentialsStream = new FileInputStream(credentialsFile);
        GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsStream);
        BigQueryOptions options = BigQueryOptions.newBuilder().setProjectId(projectId).setCredentials(credentials)
                .build();
        BigQuery bigquery = options.getService();
        __bigquery__ = bigquery;
        ;
    }

}

I have this error : com.google.cloud.bigquery.BigQueryException: Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1. Please look into the errors[] collection for more details.

I want to iterate this errors collection, but I did not succeed to 🙁

Can someone please give me an hint ?

I found something in Python:

But I did not succeed to have a Java equivalent.

I looked into JavaDoc : https://javadoc.io/doc/com.google.cloud/google-cloud-bigquery/latest/index.html

  • “com.google.cloud.bigquery.Job” object has no “getErrors” method (or something equivalent).
  • I saw a “com.google.cloud.bigquery.BigQueryErrorMessages” object, but I did not succeed to get it from the above “Job” object.

Advertisement

Answer

The error was in my “catch” block header. I catched a generic Exception raiser than a specific BigQueryException. BigQueryException has a getErrors method wich contains all the errors.

try {
    completedJob = job.waitFor(); // HERE THE ERROR IS RAISED
} catch (BigQueryException e) {
    List<BigQueryError> errors = e.getErrors();
    Exception metaE = null;
    if (null != errors) {
        StringBuffer message = new StringBuffer(e.getMessage()).append("rn");
        int errorNumber = errors.size();
        int errorIndex = 0;
        for (BigQueryError error : errors) {
            message = message.append("error ").append(++errorIndex).append("/").append(errorNumber)
                    .append(" : ").append(error.getMessage()).append("rn");
                }
        metaE = new Exception(message.toString(), e.getCause());
        metaE.setStackTrace(e.getStackTrace());
    } else {
        metaE = e;
    }
    throw metaE;
} catch (Exception e) {
    throw e;
}

So console output is :

Exception in thread "main" java.lang.Exception: Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1. Please look into the errors[] collection for more details.
error 1/3 : Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1. Please look into the errors[] collection for more details.
error 2/3 : Error while reading data, error message: JSON processing encountered too many errors, giving up. Rows: 1; errors: 1; max bad: 0; error percent: 0
error 3/3 : Invalid NUMERIC value: 2010-08-31 00:00:00.000000 Field: num; Value: 2010-08-31 00:00:00.000000

    at com.google.cloud.bigquery.Job.reload(Job.java:419)
    at com.google.cloud.bigquery.Job.waitFor(Job.java:252)
    at test.Ticket37120GoogleBigQueryErrors.main(Ticket37120GoogleBigQueryErrors.java:38)

And the error root cause in now clear : Invalid NUMERIC value

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement