Difference between AWS SDK DynamoDB client and DocumentClient?

前端 未结 2 973
心在旅途
心在旅途 2021-02-18 21:33

I want to know the difference between the AWS SDK DynamoDB client and the DynamoDB DocumentClient? In which use case should we use the DynamoDB client over the DocumentClient? <

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-18 22:00

    I think this can be best answered by comparing two code samples which do the same thing.

    Here's how you put an item using the dynamoDB client:

    var params = {
        Item: {
            "AlbumTitle": {
                S: "Somewhat Famous"
            },
            "Artist": {
                S: "No One You Know"
            },
            "SongTitle": {
                S: "Call Me Today"
            }
        },
        TableName: "Music"
    };
    dynamodb.putItem(params, function (err, data) {
        if (err) console.log(err)
        else console.log(data);           
    });
    

    Here's how you put the same item using the DocumentClient API:

    var params = {
        Item: {
            "AlbumTitle": "Somewhat Famous",
            "Artist": "No One You Know",
            "SongTitle": "Call Me Today"
        },
        TableName: "Music"
    };
    
    var documentClient = new AWS.DynamoDB.DocumentClient();
    
    documentClient.put(params, function (err, data) {
        if (err) console.log(err);
        else console.log(data);
    });
    

    As you can see in the DocumentClient the Item is specified in a more natural way. Similar differences exist in all other operations that update DDB (update(), delete()) and in the items returned from read operations (get(), query(), scan()).

提交回复
热议问题