使用谷歌应用程序脚本在谷歌大查询上保存表格

时间:2015-04-01 05:38:24

标签: google-apps-script google-bigquery

我正在尝试使用app脚本保存此表。然后我想在同一个脚本中的另一个查询中使用此表。所以基本上我需要在创建第一个表之前等待,以便第二个查询可以在google big query项目中找到日期。

{
    { 
        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;
            }
        }

代码创建第一个表,然后进入无限循环并最终超时。查询的状态不会从运行变为完成

1 个答案:

答案 0 :(得分:3)

这是我用来检查插入作业状态的代码:

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);
}