2013-07-03 121 views
0

我相信这会正确设置作业配置,但我无法确定如何让作业运行。有没有人使用Apps脚本将表格加载到BigQuery?使用Google应用程序脚本将表格从Google存储装载到BigQuery

function testLoad(){ 

    var fields = [ 
    {'name': 'FirstName', 'type':'STRING'}, 
    {'name': 'LastName', 'type':'STRING'} 
    ]; 

    var schema = BigQuery.newTableSchema() 
    schema.setFields(fields) 

    var tableReference = BigQuery.newTableReference() 
    tableReference.setProjectId(PROJECT_ID); 
    tableReference.setDatasetId('Test_Dataset'); 
    tableReference.setTableId('TestTable1'); 

    var load = BigQuery.newJobConfigurationLoad(); 
    load.setDestinationTable(tableReference); 
    load.setSkipLeadingRows(1); 
    load.setSourceUris([SOURCE]); 
    load.setSourceFormat('CSV'); 
    load.setSchema(schema) 

    var configuration = BigQuery.newJobConfiguration(); 
    configuration.setLoad(load); 

    var newJob = BigQuery.newJob(); 
    newJob.setConfiguration(configuration); 

    var insert = BigQuery.Jobs.insert(newJob) 

    Logger.log(insert.getId()); 

} 
+0

看起来不错,你会得到什么错误? –

+0

我得到“缺少必要的参数(第58行,文件”加载“)”它指的是Logger.log之前的行 – JKWA

回答

1

终于回到了这个问题。缺少的部分需要将项目ID包含到插入作业以及表格引用中。

我还包括了一些轮询工作状态并在工作完成时返回。

function testLoad(){ 

    Logger.log(exampleLoad_()); 
} 


function exampleLoad_(){ 

    try{ 

    var fields = [ 
     {'name': 'FirstName', 'type':'STRING'}, 
     {'name': 'LastName', 'type':'STRING'} 
    ]; 

    var schema = BigQuery.newTableSchema(); 
    schema.setFields(fields); 

    var tableReference = BigQuery.newTableReference(); 
    tableReference.setProjectId(PROJECT_ID); 
    tableReference.setDatasetId('Test_Dataset'); 
    tableReference.setTableId('TestTable1'); 

    var load = BigQuery.newJobConfigurationLoad(); 
    load.setDestinationTable(tableReference); 
    load.setSkipLeadingRows('1'); 
    load.setSourceUris([SOURCE]); 
    load.setSourceFormat('CSV'); 
    load.setSchema(schema); 
    load.setAllowJaggedRows(false); 

    var configuration = BigQuery.newJobConfiguration(); 
    configuration.setLoad(load); 

    var newJob = BigQuery.newJob(); 
    newJob.setConfiguration(configuration); 

    var job = BigQuery.Jobs.insert(newJob, {projectId:PROJECT_ID}); 

    var jobId = job.getJobReference().getJobId(); 

    var status = job.getStatus(); 


    while (status.getState() != 'DONE'){ 

     if(status.getState() == 'PENDING'){ 
     Utilities.sleep(100); 

     } 

     if (status.getErrorResult() == true){  
     return status.getErrors(); 

     } 
     status = BigQuery.Jobs.get(PROJECT_ID, jobId).getStatus(); 
    } 

    }catch(err){ 
    return err; 

    } 

    return status.getState(); 
} 
+0

你能解释这一点吗?我试图使用它,我不能。 –

相关问题