Febian Shah
Febian Shah

Reputation: 1007

Create table out of a query result?

I'm using Bigquery's Java API. I'm running a select query and want the result saved to a destination table.

I've set the loadConfig.setDestinationTable() but I am getting "Load configuration must specify at least one source URI".

Could you please explain what am I doing wrong?

Upvotes: 0

Views: 907

Answers (3)

Yonihu
Yonihu

Reputation: 171

this is the code i am using to do this:

    public static String copyTable(String project, String dataSet, String table) {

        String newTableName = table + "_copy_"+System.currentTimeMillis();;
        try {
            Job copyJob = new Job();

            TableReference source = new TableReference();
            source.setProjectId(project);
            source.setDatasetId(dataSet);
            source.setTableId(table);
            TableReference destination = new TableReference();
            destination.setProjectId(project);
            destination.setDatasetId(dataSet);
            destination.setTableId(newTableName);

            JobConfiguration configuration = new JobConfiguration();
            JobConfigurationTableCopy copyConf = new JobConfigurationTableCopy();
            copyConf.setSourceTable(source);
            copyConf.setDestinationTable(destination);

            configuration.setCopy(copyConf);
            copyJob.setConfiguration(configuration);

            bigquery.jobs().insert(project, copyJob).execute();
            return newTableName;
        } catch (Exception e) {
            e.printStackTrace();
            logger.warn("unable to copy table :" + project + "."
                    + dataSet + "." + table, e);
            throw new RuntimeException(e);
        }
    }

please contact me if you have any more questions

Upvotes: 1

Jordan Tigani
Jordan Tigani

Reputation: 26637

You don't want to set the loadConfig destination table, but the queryConfig.setDestinationTable() instead (since this isn't a load job -- it is a query job). As Fh said, if you share the code you're using we can give more detailed help.

Upvotes: 2

Shayan Masood
Shayan Masood

Reputation: 1057

Assuming you are running an interactive asynchronous query, you essentially want to pass the query, destination projectId, destination dataSetId and destination tableId in one request body. Refer to the Java API example here: https://developers.google.com/bigquery/querying-data#asyncqueries

Upvotes: 0

Related Questions