반응형
솔직히 지금 로직에 Select 는 필요없는데,
차근차근 테스트를 하기위해 Select를 만들어본다.
아마 Insert 후 rowcount 조회용도로 사용할듯.
호출부에서 projecid, datasetname, tablename 과
select query 문을 지정하고
void contextLoads() throws Exception {
System.out.println("############### start ###############");
String projectId = "projectId ";
String datasetName = "datasetName ";
String tableName = "tableName ";
// Select
String selectQueryString = "SELECT col01, col02, col03 "
+ " FROM `" + projectId + "." + datasetName + "." + tableName + "`"
+ " ORDER BY col01 ASC ";
executeSelectQueryString(projectId, selectQueryString);
System.out.println("############### end ###############");
}
Select 문을 실행하다.
/**
* BigQuery의 Select 문 수행
* @param projectId
* @param query
* @throws InterruptedException
* @throws IOException
*/
public static void executeSelectQueryString(String projectId, String query) throws InterruptedException, Exception {
try {
BigQuery bigQuery = getBigQuery(projectId);
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();
// Create a job ID so that we can safely retry.
JobId jobId = JobId.of(UUID.randomUUID().toString());
Job queryJob = bigQuery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
// Wait for the query to complete.
queryJob = queryJob.waitFor();
// Check for errors
if (queryJob == null) {
throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
throw new RuntimeException(queryJob.getStatus().getError().toString());
}
// Get the results.
TableResult result = queryJob.getQueryResults();
// Print all pages of the results.
for (FieldValueList row : result.iterateAll()) {
String col01 = row.get("col01").getStringValue();
String col02 = row.get("col02").getStringValue();
String col03 = row.get("col03").getStringValue();
System.out.printf("col01: %s \\tcol02 : %s \\tcol03: %s \\n", col01, col02, col03);
}
} catch ( BigQueryException e ) {
System.out.println("Insert operation not performed \\n" + e.toString());
}
}
############### start ###############
col01: test1 col02 : 10 col03: 2022-10-31T14:29:58
col01: test1 col02 : 20 col03: 2022-10-31T14:29:58
col01: test1 col02 : 30 col03: 2022-10-31T14:29:58
col01: test1 col02 : 40 col03: 2022-10-31T14:29:58
col01: test1 col02 : 50 col03: 2022-10-31T14:29:58
.....
col01: test2 col02 : 99950 col03: 2022-10-31T14:29:58
col01: test2 col02 : 99960 col03: 2022-10-31T14:29:58
col01: test2 col02 : 99970 col03: 2022-10-31T14:29:58
col01: test2 col02 : 99980 col03: 2022-10-31T14:29:58
col01: test2 col02 : 99990 col03: 2022-10-31T14:29:58
############### end ###############
반응형
'GCP' 카테고리의 다른 글
JAVA 프로젝트로 GCP BigQuery의 Bulk INSERT(Streaming buffer) 테이블 truncate 이슈 (0) | 2022.11.14 |
---|---|
JAVA 프로젝트로 GCP BigQuery의 Bulk INSERT(Streaming buffer) (0) | 2022.11.07 |
JAVA 프로젝트로 GCP BigQuery의 데이터 조작하기(Insert / Update) (0) | 2022.11.04 |
Cloud Function으로 SAP OData 를 BigQuery에 적재하기 (0) | 2022.10.17 |
Cloud Data Fusion(CDF)으로 SAP OData 를 BigQuery에 저장할 때 벌어진 이슈 (0) | 2022.10.14 |