Retrieve item from DynamoDB using apache camel

为君一笑 提交于 2020-08-10 20:15:11

问题


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();

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

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


回答1:


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

From AWS DynamoDB Component documentation:

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

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



来源:https://stackoverflow.com/questions/63101656/retrieve-item-from-dynamodb-using-apache-camel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!