简体   繁体   English

AWS Kendra 和 Spring 启动

[英]Aws Kendra and Spring boot

I want to integrate spring boot with the aws kendra query index.I want to leverage kendra as elastic search where i make api for search query and then get results for the same via that api.我想将 spring 引导与 aws kendra 查询索引集成。我想利用 kendra 作为弹性搜索,我将 api 用于搜索查询,然后通过该 api 获得相同的结果。

Documentations dont clearly mention the connection procedure/steps.文档没有明确提及连接过程/步骤。 Not sure if thats possible or not.不确定那是否可能。

Ps created index and dataSource as webcrawler. ps作为网络爬虫创建了index和dataSource。 Now whats the step forward.现在有什么进展。

We can follow the official AWS documentation to query an index.我们可以按照AWS官方文档查询一个索引。 The tricky part is to provide the credentials while building the KendraClient object. The code is taken mostly from the link mentioned with the addition of creating Kendra Client.棘手的部分是在构建KendraClient object 时提供凭据。代码主要取自添加创建 Kendra 客户端时提到的链接。

A sample code would look like as below -示例代码如下所示 -

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kendra.KendraClient;
import software.amazon.awssdk.services.kendra.model.QueryRequest;
import software.amazon.awssdk.services.kendra.model.QueryResponse;
import software.amazon.awssdk.services.kendra.model.QueryResultItem;

public class SearchKendraIndex {

    public static void main(String[] args) {

        String accessKey = "your-access-key";
        String secretKey = "your-secret-key";

        AwsCredentials awsCredentials = AwsBasicCredentials.create(accessKey, secretKey);

        KendraClient kendra = KendraClient
                .builder()
                .region(Region.AP_SOUTHEAST_1) // Change it with your region
                .credentialsProvider(() -> awsCredentials)
                .build();

        String query = "your-search-term";
        String indexId = "your-index-id";

        QueryRequest queryRequest = QueryRequest
                .builder()
                .queryText(query)
                .indexId(indexId)
                .build();

        QueryResponse queryResponse = kendra.query(queryRequest);

        System.out.printf("\nSearch results for query: %s%n", query);
        for (QueryResultItem item : queryResponse.resultItems()) {
            System.out.println("----------------------");
            System.out.printf("Type: %s%n", item.type());
            switch (item.type()) {
                case QUESTION_ANSWER, ANSWER -> {
                    String answerText = item.documentExcerpt().text();
                    System.out.println(answerText);
                }
                case DOCUMENT -> {
                    String documentTitle = item.documentTitle().text();
                    System.out.printf("Title: %s%n", documentTitle);
                    String documentExcerpt = item.documentExcerpt().text();
                    System.out.printf("Excerpt: %s%n", documentExcerpt);
                }
                default -> System.out.printf("Unknown query result type: %s%n", item.type());
            }
            System.out.println("-----------------------\n");
        }
    }
}

There are examples of how to INVOKE AWS Services from a Spring BOOT app.有一些示例说明如何从 Spring BOOT 应用程序调用 AWS 服务。 Here are a few that show use of the AWS SDK for Java V2 :以下是一些显示将AWS SDK 用于 Java V2 的示例

  1. Creating your first AWS Java web application 创建您的第一个 AWS Java web 应用程序
  2. Creating a dynamic web application that analyzes photos using the AWS SDK for Java . 创建动态 web 应用程序,使用 AWS SDK 为 Java 分析照片
  3. Creating the Amazon DynamoDB web application item tracker 创建 Amazon DynamoDB web 应用程序项目跟踪器

There are more Spring BOOT/AWS Service examples in Github . Spring BOOT/AWS 服务示例更多见Github

While there are no Spring BOOT examples for Kendra client (yet), the pattern is the same.虽然 Kendra 客户端(目前)还没有 Spring BOOT 示例,但模式是相同的。 You can build a MVC app and build a service that uses https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/kendra/KendraClient.html .您可以构建 MVC 应用程序并构建使用https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/kendra/KendraClient.html的服务。

You can use the AWS SDK for Java to make API calls to AWS services.您可以将AWS SDK 用于 Java以对 AWS 服务进行 API 调用。 You can find code examples in the link above.您可以在上面的链接中找到代码示例。

Setting up your Kendra client will look something like this:设置您的 Kendra 客户端将如下所示:

String accessKey = "YOUR_ACCESS_KEY";
String secretKey = "YOUR_SECRET_KEY";
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
AWSkendra kendraClient = AWSkendraClientBuilder
                .standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .build();

After you set up your Kendra client, you can then use the query method to query your Kendra index.设置 Kendra 客户端后,您可以使用查询方法查询 Kendra 索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM