GCP

JAVA 프로젝트로 GCP BigQuery의 데이터 조회하기

whistory 2022. 10. 31. 14:48
반응형

 

솔직히 지금 로직에 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 ###############
반응형