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

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

问题描述

我正在尝试使用apache骆驼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 dynamodb get-item --table-name user --key '{"LastName": {"S": "Smith"}}'中使用aws cli,我将得到正确的响应,例如->

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"
        }
    }
}

如何使用apache camel 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骆驼从DynamoDB检索项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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