메뉴
Amazon Simple Storage Service
개발자 안내서 (API 버전 2006-03-01)

Java용 SDK를 사용하여 객체에서 콘텐츠 선택

Amazon S3 Select를 사용하여 selectObjectContent 메서드를 사용하여 Java가 적용된 객체의 콘텐츠를 선택합니다. 성공할 경우 SQL 표현식의 결과가 반환됩니다. 지정한 버킷 및 객체 키가 존재해야 합니다. 그렇지 않으면 오류가 발생합니다.

아래의 Java 코드는 CSV 형식으로 저장된 데이터를 포함하는 객체에 저장되어 있는 모든 레코드에 대해 첫 번째 열의 값을 반환합니다. 또한 ProgressStats 메시지를 반환하도록 요청합니다. 유효한 버킷 이름과 CSV 형식의 데이터를 포함하는 객체를 제공해야 합니다.

실제 예제를 작성 및 테스트하는 방법에 대한 자세한 내용은 Amazon S3 Java 코드 예제 테스트 단원을 참조하십시오.

package com.amazonaws; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.CSVInput; import com.amazonaws.services.s3.model.CSVOutput; import com.amazonaws.services.s3.model.CompressionType; import com.amazonaws.services.s3.model.ExpressionType; import com.amazonaws.services.s3.model.InputSerialization; import com.amazonaws.services.s3.model.OutputSerialization; import com.amazonaws.services.s3.model.SelectObjectContentEvent; import com.amazonaws.services.s3.model.SelectObjectContentEventVisitor; import com.amazonaws.services.s3.model.SelectObjectContentRequest; import com.amazonaws.services.s3.model.SelectObjectContentResult; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.concurrent.atomic.AtomicBoolean; import static com.amazonaws.util.IOUtils.copy; /** * This example shows how to query data from S3Select and consume the response in the form of an * InputStream of records and write it to a file. */ public class RecordInputStreamExample { private static final String BUCKET_NAME = "${my-s3-bucket}"; private static final String CSV_OBJECT_KEY = "${my-csv-object-key}"; private static final String S3_SELECT_RESULTS_PATH = "${my-s3-select-results-path}"; private static final String QUERY = "select s._1 from S3Object s"; public static void main(String[] args) throws Exception { final AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); SelectObjectContentRequest request = generateBaseCSVRequest(BUCKET_NAME, CSV_OBJECT_KEY, QUERY); final AtomicBoolean isResultComplete = new AtomicBoolean(false); try (OutputStream fileOutputStream = new FileOutputStream(new File (S3_SELECT_RESULTS_PATH)); SelectObjectContentResult result = s3Client.selectObjectContent(request)) { InputStream resultInputStream = result.getPayload().getRecordsInputStream( new SelectObjectContentEventVisitor() { @Override public void visit(SelectObjectContentEvent.StatsEvent event) { System.out.println( "Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned() + " Bytes Processed: " + event.getDetails().getBytesProcessed()); } /* * An End Event informs that the request has finished successfully. */ @Override public void visit(SelectObjectContentEvent.EndEvent event) { isResultComplete.set(true); System.out.println("Received End Event. Result is complete."); } } ); copy(resultInputStream, fileOutputStream); } /* * The End Event indicates all matching records have been transmitted. * If the End Event is not received, the results may be incomplete. */ if (!isResultComplete.get()) { throw new Exception("S3 Select request was incomplete as End Event was not received."); } } private static SelectObjectContentRequest generateBaseCSVRequest(String bucket, String key, String query) { SelectObjectContentRequest request = new SelectObjectContentRequest(); request.setBucketName(bucket); request.setKey(key); request.setExpression(query); request.setExpressionType(ExpressionType.SQL); InputSerialization inputSerialization = new InputSerialization(); inputSerialization.setCsv(new CSVInput()); inputSerialization.setCompressionType(CompressionType.NONE); request.setInputSerialization(inputSerialization); OutputSerialization outputSerialization = new OutputSerialization(); outputSerialization.setCsv(new CSVOutput()); request.setOutputSerialization(outputSerialization); return request; } }