![](/img/trans.png)
[英]How to skip header row while loading csv data in google BigQuery using client library (java)
[英]Any examples of loading to BigQuery using a POST request and Java client library?
有沒有人有任何使用以下兩種方法為BigQuery創建新插入作業的示例:
你需要調用bigquery.jobs()。insert(...)方法。
我不知道你做了什么,但你應該有一個經過身份驗證的API客戶端至少像:
bigquery = new Bigquery.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials)
.setApplicationName("...").build();
這是我使用google-http-client庫為java和bigquery-api編寫的insertRows方法的簡化版本(你應該檢查數據集是否存在,驗證id等):
public Long insertRows(String projectId,
String datasetId,
String tableId,
InputStream schema,
AbstractInputStreamContent data) {
try {
// Defining table fields
ObjectMapper mapper = new ObjectMapper();
List<TableFieldSchema> schemaFields = mapper.readValue(schema, new TypeReference<List<TableFieldSchema>>(){});
TableSchema tableSchema = new TableSchema().setFields(schemaFields);
// Table reference
TableReference tableReference = new TableReference()
.setProjectId(projectId)
.setDatasetId(datasetId)
.setTableId(tableId);
// Load job configuration
JobConfigurationLoad loadConfig = new JobConfigurationLoad()
.setDestinationTable(tableReference)
.setSchema(tableSchema)
// Data in Json format (could be CSV)
.setSourceFormat("NEWLINE_DELIMITED_JSON")
// Table is created if it does not exists
.setCreateDisposition("CREATE_IF_NEEDED")
// Append data (not override data)
.setWriteDisposition("WRITE_APPEND");
// If your data are coming from Google Cloud Storage
//.setSourceUris(...);
// Load job
Job loadJob = new Job()
.setJobReference(
new JobReference()
.setJobId(Joiner.on("-").join("INSERT", projectId, datasetId,
tableId, DateTime.now().toString("dd-MM-yyyy_HH-mm-ss-SSS")))
.setProjectId(projectId))
.setConfiguration(new JobConfiguration().setLoad(loadConfig));
// Job execution
Job createTableJob = bigquery.jobs().insert(projectId, loadJob, data).execute();
// If loading data from Google Cloud Storage
//createTableJob = bigquery.jobs().insert(projectId, loadJob).execute();
String jobId = createTableJob.getJobReference().getJobId();
// Wait for job completion
createTableJob = waitForJob(projectId, createTableJob);
Long rowCount = createTableJob != null ? createTableJob.getStatistics().getLoad().getOutputRows() : 0l;
log.info("{} rows inserted in table '{}' (dataset: '{}', project: '{}')", rowCount, tableId, datasetId, projectId);
return rowCount;
}
catch (IOException e) { throw Throwables.propagate(e); }
}
我不知道您的數據格式,但如果您使用的是文件,則可以添加如下函數:
public Long insertRows(String projectId, String datasetId, String tableId, File schema, File data) {
try {
return insertRows(projectId, datasetId, tableId, new FileInputStream(schema),
new FileContent(MediaType.OCTET_STREAM.toString(), data));
}
catch (FileNotFoundException e) { throw Throwables.propagate(e); }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.