2014-09-10 17 views
1

這應該是以CSV格式讀取,然後將其寫入bigquery。然而,當它運行時,沒有任何東西被寫入,並且沒有記錄錯誤。我讀到我需要寫一個csv,然後把它變成一個Octet Stream。我不確定這是否與google bigquery兼容。Apps腳本寫入Big Query未知錯誤

function test(){ 
try{ 
    var tableReference = BigQuery.newTableReference(); 
    tableReference.setProjectId(PROJECT_ID); 
    tableReference.setDatasetId(datasetId); 
    tableReference.setTableId(tableId); 
    var schema = "CUSTOMER:string, CLASSNUM:integer, CLASSDESC:string, CSR:string, CSR2:string, INSURANCE:string, REFERRALGENERAL:string, REFERRALSPECIFIC:string, NOTES:string, INMIN:integer, INHR:integer, OUTMIN:integer, OUTHR:integer, WAITMIN:integer, WAITHR:integer, DATETIMESTAMP:float, DATEYR:integer,DATEMONTH:integer, DATEDAY:integer"; 
    var load = BigQuery.newJobConfigurationLoad(); 
    load.setDestinationTable(tableReference); 
    load.setSourceUris(URIs); 
    load.setSourceFormat('NEWLINE_DELIMITED_JSON'); 
    load.setSchema(schema); 
    load.setMaxBadRecords(0); 
    load.setWriteDisposition('WRITE_TRUNCATE'); 

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

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


    var loadr = DriveApp.getFilesByName("test.csv"); 
    var x = loadr.next().getBlob(); 
    Logger.log(x.getDataAsString()); 

    var d = DriveApp.getFilesByName("test.csv"); 
    var id = d.next().getId(); 
    Logger.log(id); 
    var data = DocsList.getFileById(id).getBlob().getDataAsString(); 
    var mediaData = Utilities.newBlob(data, 'application/octet-stream'); 

    BigQuery.Jobs.insert(newJob, PROJECT_ID, mediaData) 
} 
catch(error){Logger.log("A" + error.message);} 
} 
+0

你有問題的答案嗎?謝謝 – 2015-05-13 09:29:22

回答

3

你sourceFormat是錯誤的CSV文件:

數據文件的格式。對於CSV文件,請指定「CSV」。對於 數據存儲庫備份,請指定「DATASTORE_BACKUP」。對於換行符分隔的 JSON,請指定「NEWLINE_DELIMITED_JSON」。默認值是CSV。

https://developers.google.com/bigquery/docs/reference/v2/jobs#configuration.load.sourceUris

在另一方面,我認爲你並不需要在所有的load.setSourceUris(URIs);因爲你嘗試從本地文件加載,而不是從谷歌雲存儲。檢查這個Python例子https://developers.google.com/bigquery/loading-data-into-bigquery

+0

我將sourceFormat改爲CSV並刪除了setSourceUris,但它仍然沒有寫入,仍然沒有顯示錯誤。我再次放入setSourceUris,並且沒有錯誤。我真的不確定要做什麼。我所需要做的就是讓這部分工作,我完成了這個項目的Bigquery方面。 – 2014-09-12 15:49:54