RAGHAV RASTOGI
RAGHAV RASTOGI

Reputation: 53

Save table on google big query using google app script

I am trying to save this table using app script. Then I want to use this table in another query in the same script. So basically I need to wait before the first table is created so that the second query can find the date in the google big query project.

{
    { 
        var projectId = 'P1';
        var datasetId = 'D1';
        var tableId = 'RR_Signup1_' + week;
        Logger.log(tableId);

        var job = {
            configuration: {
                query: {
                    query: 'query desciption',

                    destinationTable : {
                        projectId: projectId,
                        datasetId : datasetId,
                                tableId : tableId
                    }
                }
            }
        };
    }

    var queryResults = BigQuery.Jobs.insert(job, projectId);
    var jobId = queryResults.jobReference.jobId;
    Logger.log(queryResults.status);
    var sleepTimeMs = 500;
    while (true) {
        Utilities.sleep(sleepTimeMs);
        sleepTimeMs *= 2;
        queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId);
        if (!queryResults.jobComplete) {
            break;
        }
    }


    {
        {
            var projectId = 'P1';
            var datasetId = 'D1';
            var tableId = 'RR_Signup2_' + week;
            Logger.log(tableId);

            var job = {
                configuration: {
                    query: {
                        query: 'select  uid, Signup_time from [' + tableId1 + '] ;',
                        destinationTable : {
                            projectId: projectId,
                            datasetId : datasetId,
                            tableId : tableId
                        }
                    }
                }
            };
        }

        var queryResults = BigQuery.Jobs.insert(job, projectId);
        var jobId = queryResults.jobReference.jobId;
        Logger.log(queryResults.status);
        var sleepTimeMs = 500;
        while (true) {
            Utilities.sleep(sleepTimeMs);
            sleepTimeMs *= 2;
            queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId);
            if (!queryResults.jobComplete) {
                break;
            }
        }

the code creates the first table and then goes into an infinite loop and times out eventually. The state of the query does not change from running to done

Upvotes: 0

Views: 590

Answers (1)

Gil Adirim
Gil Adirim

Reputation: 1924

This is the code I use to check the state of an Insert job:

job = BigQuery.Jobs.insert(job, projectId, data);
var jobId = job.jobReference.jobId;
var status = null;

// Check on status of the Query Job.
var sleepTimeMs = 1000;
while (job.status.state != "DONE") {
if (job.status.errorResult != null)
  status = "fail"
Utilities.sleep(sleepTimeMs);
job = BigQuery.Jobs.get(projectId, jobId);
}

if (status != "fail") {
   Logger.log('Load job started. JobID: ' + jobId);
} else {
Logger.log('Load job error. JobID: ' + jobId + "\r\nErrors: " +  job.status.errors[0].message + " | " + job.status.errors[0].location);
}

Upvotes: 3

Related Questions