我想在REST API中向BigQuery表插入一些行,但是它失败了,但有一般例外。我不知道我的代码中有什么问题。当我尝试从Chrome浏览器插入带有REST客户端的行时,它已通过。
final HttpTransport TRANSPORT = new NetHttpTransport();
final JsonFactory JSON_FACTORY = new JacksonFactory();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Build service account credential.
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("foo")
.setServiceAccountScopes(Collections.singleton(BigqueryScopes.BIGQUERY))
.setServiceAccountPrivateKeyFromP12File(new File(Main.class.getClassLoader().getResource("foo").getFile()))
.build();
Bigquery service = new Bigquery.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("BigQuery Samples").build();
Bigquery.Projects.List projectListRequest = service.projects().list();
TableDataInsertAllRequest.Rows data = new TableDataInsertAllRequest.Rows();
// data.setInsertId("");
Map<String, Object> json = new HashMap<>();
json.put("from", "A");
json.put("to", "B");
TableDataInsertAllRequest.Rows row = new TableDataInsertAllRequest.Rows();
row.setJson(json);
List<TableDataInsertAllRequest.Rows> rows = new ArrayList<>();
rows.add(row);
TableDataInsertAllRequest requestData = new TableDataInsertAllRequest();
requestData.setSkipInvalidRows(true);
requestData.setIgnoreUnknownValues(true);
requestData.setKind("bigquery#tableDataInsertAllRequest");
requestData.setRows(rows);
try {
TableDataInsertAllResponse response = service.tabledata().insertAll("myDatastore "myProject", "test", requestData).execute();
System.out.println(response);
} catch (GoogleJsonResponseException e) {
e.printStackTrace();
}
使用REST客户端在URL https://www.googleapis.com/bigquery/v2/projects/myProject/datasets/myDatastore/tables/test/insertAll
{
"kind": "bigquery#tableDataInsertAllRequest",
"skipInvalidRows": true,
"ignoreUnknownValues": true,
"rows": [
{
"json": {
"from": "VIE",
"to": "FRA"
}
}
]
}
从Java调用REST API的例外。
com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
{
"code" : 503,
"errors" : [ {
"domain" : "global",
"message" : "Error encountered during execution. Retrying may solve the problem.",
"reason" : "backendError"
} ],
"message" : "Error encountered during execution. Retrying may solve the problem."
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.example.Main.main(Main.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)