是否有使用app-engine服务帐户访问BigQuery服务的Java示例?我不想使用OAuth流程。
答案 0 :(得分:2)
获得服务帐户凭据的一个班轮:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import java.io.File;
import java.util.Arrays;
credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT)
.setServiceAccountScopes(Arrays.asList(new String[] {SCOPE}))
.setServiceAccountPrivateKeyFromP12File(new File(P12_KEY))
.build();
其中:
SERVICE_ACCOUNT = "15-uin9i5@developer.gserviceaccount.com"
P12_KEY = "path/to/private_key.p12"
TRANSPORT = new NetHttpTransport();
SCOPE = "https://www.googleapis.com/auth/bigquery"
JSON_FACTORY = new JacksonFactory()
获得凭据后,您可以构建BigQuery服务并将其与任何其他身份验证方法一起使用:
import com.google.api.services.bigquery.Bigquery;
bigquery = new Bigquery.Builder(
TRANSPORT, JSON_FACTORY, credential).setApplicationName(
"BQ-ServAcc/so/0.1").setHttpRequestInitializer(credential).build();
例如,检查工作:
import com.google.api.services.bigquery.model.Job;
while (true) {
Job job = bigquery.jobs().get(projectId, jobId).execute();
System.out.println(job.getStatus().toPrettyString());
if (job.getStatus().getState().equals("DONE")) {
if (job.getStatus().getErrorResult() != null)
System.out.println(job.getStatus().getErrorResult().toPrettyString());
System.out.println(job.getStatistics().toPrettyString());
break;
}
Thread.sleep(1000);
}