使用 apache camel 从 DynamoDB 检索项目 [英] Retrieve item from DynamoDB using apache camel

查看:24
本文介绍了使用 apache camel 从 DynamoDB 检索项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 apache camel aws2 DynamoDB 组件来检索项目.在文档中,我们可以使用操作 getItem 来实现这一点.我写了一个这样的代码 ->

I am trying to use the apache camel aws2 DynamoDB component to retrieve an item. In the docs, it is given that we can use an operation getItem in order to achieve this. I wrote a code like so ->

.post("dynamodb-get-item")
    .route()
    .process(new Processor(){
        @Override
        public void process(Exchange exchange) throws Exception {
            Map<String, AttributeValue> key = new HashMap();
            key.put("LastName", AttributeValue.builder().s("Smith").build());
            exchange.getIn().setHeader(Ddb2Constants.KEY, key);
            exchange.getIn().setHeader(Ddb2Constants.ATTRIBUTE_NAMES, key.keySet());
        }        
    })
    .toD("aws2-ddb://user?accessKey=insert&secretKey=insert&region=us-east-1&operation=getItem")
    .process(new Processor(){
        @Override
        public void process(Exchange exchange) throws Exception {
            Map<String, AttributeValue> response =  (Map<String, AttributeValue>) exchange.getIn().getHeader(Ddb2Constants.ATTRIBUTES);
            exchange.getIn().setBody(response);
        }
    })
    .convertBodyTo(String.class)
    .endRest();

DynamoDb 表的名称是 user,键是 LastName.此代码执行成功并返回此响应 {LastName=AttributeValue(S=Smith, SS=[], NS=[], BS=[], M={}, L=[])}.此响应只有属性 LastName.这不会返回属性 FirstNameAge 这也是该项目的一部分.如果我将 aws cli 与此命令一起使用 aws dynamodb get-item --table-name user --key '{"LastName": {"S": "Smith"}}'我得到了正确的回应 ->

The name of the DynamoDb table is user and the key is LastName. This code executes successfully and returns this response {LastName=AttributeValue(S=Smith, SS=[], NS=[], BS=[], M={}, L=[])}. This response has only the attribute LastName. This doesn't return the attribute FirstName and Age which is also part of this item. If I use the aws cli with this command aws dynamodb get-item --table-name user --key '{"LastName": {"S": "Smith"}}' I get the correct response like so ->

{
    "Item": {
        "LastName": {
            "S": "Smith"
        }, 
        "Age": {
            "N": "22"
        }, 
        "FirstName": {
            "S": "Will"
        }
    }
}

如何使用apachecamel aws2 DynamoDb组件的getItem生产者操作获得上述的完整响应?

How to get the full response like above using the getItem producer operation of apache camel aws2 DynamoDb component?

推荐答案

您使用了导致响应属性过滤的标题 Ddb2Constants.ATTRIBUTE_NAMES.

You have used header Ddb2Constants.ATTRIBUTE_NAMES which causes filtering of response attributes.

来自 AWS DynamoDB 组件文档:

CamelAwsDdbAttributeNames - 如果未指定属性名称,则将返回所有属性.

CamelAwsDdbAttributeNames - If attribute names are not specified then all attributes will be returned.

因此,只需删除 exchange.getIn().setHeader(Ddb2Constants.ATTRIBUTE_NAMES, key.keySet()); 行,您就会得到完整的响应.

So just remove the line exchange.getIn().setHeader(Ddb2Constants.ATTRIBUTE_NAMES, key.keySet()); and you will get full response.

这篇关于使用 apache camel 从 DynamoDB 检索项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆