0

我最近参与了一个项目,我必须利用 QuickSight API 并以编程方式更新仪表板。我可以执行所有其他操作,但无法从模板更新仪表板。我尝试了几个不同的想法,但都是徒劳的。是否有人已经使用过UpdateDashboard API,或者向我指出一些详细的文档,我可以在其中了解我是否真的遗漏了任何东西?谢谢。

4

1 回答 1

0

我使用AWS QuickSight Java V2 API 让它工作。要完成这项工作,您需要按照此处的快速入门说明进行操作:

https://docs.aws.amazon.com/quicksight/latest/user/getting-started.html

您需要获取这些值:

  • account - 您的帐号
  • dashboardId - 仪表板 ID 值
  • dataSetArn -- 数据集 ID 值
  • analysisArn - 分析 Arn 值

一旦您完成了上述主题 - 您将拥有所有这些资源并准备好调用UpdateDashboard。这是更新仪表板的 Java 示例。

package com.example.quicksight;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.quicksight.QuickSightClient;
import software.amazon.awssdk.services.quicksight.model.*;

/*
    Before running this code example, follow the Getting Started with Data Analysis in Amazon QuickSight located here:

    https://docs.aws.amazon.com/quicksight/latest/user/getting-started.html

    This code example uses resources that you created by following that topic such as the DataSet Arn value.

 */

public class UpdateDashboard {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage: UpdateDashboard <account> <dashboardId> <>\n\n" +
                "Where:\n" +
                "  account - the account to use.\n\n" +
                "  dashboardId - the dashboard id value to use.\n\n" +
                "  dataSetArn - the ARN of the dataset.\n\n" +
                "  analysisArn - the ARN of an existing analysis";


        String account = "<account id>";
        String dashboardId = "<dashboardId>";
        String dataSetArn = "<dataSetArn>";
        String analysisArn = "<Analysis Arn>";
        QuickSightClient qsClient = QuickSightClient.builder()
                .region(Region.US_EAST_1)
                .build();

        try {

            DataSetReference dataSetReference = DataSetReference.builder()
                    .dataSetArn(dataSetArn)
                    .dataSetPlaceholder("Dataset placeholder2")
                    .build();

            // Get a template ARN to use.
            String arn = getTemplateARN(qsClient, account, dataSetArn, analysisArn);
            DashboardSourceTemplate sourceTemplate = DashboardSourceTemplate.builder()
                    .dataSetReferences(dataSetReference)
                   .arn(arn)
                    .build();

            DashboardSourceEntity sourceEntity = DashboardSourceEntity.builder()
                    .sourceTemplate(sourceTemplate)
                    .build();


            UpdateDashboardRequest dashboardRequest = UpdateDashboardRequest.builder()
                    .awsAccountId(account)
                    .dashboardId(dashboardId)
                    .name("UpdateTest")
                     .sourceEntity(sourceEntity)
                    .themeArn("arn:aws:quicksight::aws:theme/SEASIDE")
                    .build();

            UpdateDashboardResponse response = qsClient.updateDashboard(dashboardRequest);
            System.out.println("Dashboard " + response.dashboardId() + " has been updated");

        } catch (QuickSightException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }

    private static String getTemplateARN(QuickSightClient qsClient, String account, String dataset, String analysisArn) {

        String arn = "";
        try {
            DataSetReference setReference = DataSetReference.builder()
                    .dataSetArn(dataset)
                    .dataSetPlaceholder("Dataset placeholder2")
                    .build();

            TemplateSourceAnalysis templateSourceAnalysis = TemplateSourceAnalysis.builder()
                    .dataSetReferences(setReference)
                    .arn(analysisArn)
                    .build();

            TemplateSourceEntity sourceEntity = TemplateSourceEntity.builder()
                    .sourceAnalysis(templateSourceAnalysis)
                    .build();

            CreateTemplateRequest createTemplateRequest = CreateTemplateRequest.builder()
                    .awsAccountId(account)
                    .name("NewTemplate")
                    .sourceEntity(sourceEntity)
                    .templateId("a9a277fb-7239-4890-bc7a-8a3e82d67a37") // Specify a GUID value
                    .build();

            CreateTemplateResponse response = qsClient.createTemplate(createTemplateRequest);
            arn = response.arn();

        } catch (QuickSightException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return arn;
    }
}
于 2021-01-13T22:13:52.717 回答