AWSAthena:如何处理GetQueryResultsCommand命令返回的数据?
创始人
2024-09-23 21:30:59
0

AWS SDK for Java 2.0提供了适用于Athena的客户端库。以下是使用AthenaClient执行查询并处理返回结果的示例:

import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.athena.AthenaClient;
import software.amazon.awssdk.services.athena.model.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class AthenaQueryExample {

    private static final String ATHENA_DB = "your-database-name";
    private static final String ATHENA_OUTPUT_BUCKET = "s3://your-bucket-name/your-folder-name/";
    private static final String ATHENA_QUERY_STRING = "SELECT count(*) FROM your-table-name WHERE your-column-name = 'your-value'";

    public static void main(String[] args) {
        // create Athena client
        AthenaClient athenaClient = AthenaClient.builder()
                .credentialsProvider(DefaultCredentialsProvider.create())
                .region(Region.US_WEST_2)
                .build();

        // setup query execution request
        StartQueryExecutionRequest startQueryExecutionRequest = StartQueryExecutionRequest.builder()
                .queryString(ATHENA_QUERY_STRING)
                .queryExecutionContext(QueryExecutionContext.builder().database(ATHENA_DB).build())
                .resultConfiguration(ResultConfiguration.builder().outputLocation(ATHENA_OUTPUT_BUCKET).build())
                .build();

        // execute query and get query execution ID
        String queryExecutionId = athenaClient.startQueryExecution(startQueryExecutionRequest).queryExecutionId();

        // setup query results request
        GetQueryResultsRequest getQueryResultsRequest = GetQueryResultsRequest.builder()
                .queryExecutionId(queryExecutionId)
                .build();

        // poll query status until query completes
        boolean isQueryStillRunning = true;
        while (isQueryStillRunning) {
            GetQueryExecutionRequest getQueryExecutionRequest = GetQueryExecutionRequest.builder().queryExecutionId(queryExecutionId).build();
            QueryExecution queryExecution = athenaClient.getQueryExecution(getQueryExecutionRequest).queryExecution();
            String queryState = queryExecution.status().state();

            if (queryState.equals(QueryExecutionState.FAILED.toString())) {
                throw new RuntimeException("Athena

相关内容

热门资讯

今日科普!微信卡五星辅助器,微... 今日科普!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)攻略;1.微信卡五星辅助器 选牌创建...
重大通报!微乐自建房辅助工具挂... 重大通报!微乐自建房辅助工具挂,微乐小程序免费黑科技(开挂自建房)app1、打开软件启动之后找到中间...
让我来分享经验!微乐自建房辅助... 让我来分享经验!微乐自建房辅助工具免费app,微乐自建房脚本免费入口(开挂自建房)脚本1、微乐自建房...
记者揭秘!微乐小程序自建房辅助... 记者揭秘!微乐小程序自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)软件微乐小程序自建房辅助...
必看攻略!微乐自建房辅助工具免... 必看攻略!微乐自建房辅助工具免费app,微乐家乡麻将自建房辅助app(开挂自建房)攻略1、每一步都需...
必备攻略!微乐家乡麻将自建房辅... 必备攻略!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)技巧1、必备攻略!...
查到实测辅助!微信卡五星辅助器... 查到实测辅助!微信卡五星辅助器,微乐家乡麻将自建房辅助app(开挂自建房)教程1、超多福利:超高返利...
传递经验!微乐自建房辅助工具挂... 传递经验!微乐自建房辅助工具挂,微乐小程序免费黑科技(开挂自建房)技巧1、操作简单,无需微乐自建房辅...
一分钟教你!微信卡五星辅助器,... 一分钟教你!微信卡五星辅助器,微乐家乡麻将自建房辅助app(开挂自建房)插件1、这是跨平台的微乐家乡...
传递经验!微乐小程序自建房辅助... 传递经验!微乐小程序自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)脚本传递经验!微乐小程序...