1
votes

I want to upload data from Google Cloud Storage to table in Big Query. There is my code to create job:

    Job job = new Job();
    JobConfiguration config = new JobConfiguration();
    JobConfigurationLoad loadConfig = new JobConfigurationLoad();

    List<String> sources = new ArrayList<String>();
    sources.add("gs://bucket/file.cvs");
    loadConfig.setSourceUris(sources);

    TableReference tableRef = new TableReference();
    tableRef.setDatasetId("DATASET_ID");
    tableRef.setTableId(TABLE_ID);
    tableRef.setProjectId(PROJECT_ID);
    loadConfig.setDestinationTable(tableRef);

    List<TableFieldSchema> fields = FieldsBigQuery.schema();

    TableSchema schema = new TableSchema();
    schema.setFields(fields);

    loadConfig.setSchema(schema);
    config.setLoad(loadConfig);
    job.setConfiguration(config);

    Insert insert = _bigquery.jobs().insert(PROJECT_ID, job);
    insert.setProjectId(PROJECT_ID);
    JobReference jobRef =  insert.execute().getJobReference();

I don't have any errors or exception but it's not upload any data to my table (Table size 0B). I tried to create table without any data and than to upload data to this table but it isn't.

I would appreciate any help,

Thanks very much!

1

1 Answers

1
votes

Remember to check the job status with:

JobStatus status =
    _bigquery.jobs().get(PROJECT_ID, jobRef.getJobId()).execute().getStatus();
System.out.println(status.toPrettyString());

The job state might be PENDING, or RUNNING, if everything is working alright, and you'll have to wait until it's DONE. Or you'll be able to read the reason and message on why it couldn't be ran.