Created
March 15, 2021 09:51
-
-
Save bboure/96e7d1f0a311fcd21d6e307fc0552dca to your computer and use it in GitHub Desktop.
DynamoDB gzip Large Attribute Values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const { gunzipSync } = require('zlib'); | |
AWS.config.update({ region: 'eu-west-1' }); | |
const dynamoDbClient = new AWS.DynamoDB(); | |
dynamoDbClient.getItem({ | |
"TableName": "blog", | |
"ReturnConsumedCapacity": "TOTAL", | |
"Key": { | |
"author": { | |
"S": "bboure" | |
}, | |
"slug": { | |
"S": "raw-blog-post" | |
}, | |
} | |
}).promise().then(result => { | |
console.log('Read capacity for raw post', result.ConsumedCapacity ); | |
}); | |
dynamoDbClient.getItem({ | |
"TableName": "blog", | |
"ReturnConsumedCapacity": "TOTAL", | |
"Key": { | |
"author": { | |
"S": "bboure" | |
}, | |
"slug": { | |
"S": "compressed-blog-post" | |
}, | |
} | |
}).promise().then(result => { | |
console.log('Read capacity for compressed post', result.ConsumedCapacity ); | |
// uncompress | |
const content = gunzipSync(result.Item.content.B).toString(); | |
console.log(`Original text with ${content.length} characters and ${content.split(' ').length} words`); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const { loremIpsum } = require('lorem-ipsum'); | |
const { gzipSync } = require('zlib'); | |
const content = loremIpsum({ | |
count: 20, | |
units: "paragraph", | |
format: "plain", | |
paragraphLowerBound: 5, | |
paragraphUpperBound: 15, | |
sentenceLowerBound: 5, | |
sentenceUpperBound: 15, | |
suffix: "\n\n\n", | |
}); | |
console.log(`Generated a text with ${content.length} characters and ${content.split(' ').length} words`); | |
const compressed = gzipSync(content); | |
console.log(`total size (uncompressed): ~${Math.round(content.length/1024)} KB`); | |
console.log(`total size (compressed): ~${Math.round(compressed.length/1024)} KB`); | |
AWS.config.update({ region: 'eu-west-1' }); | |
const dynamoDbClient = new AWS.DynamoDB(); | |
dynamoDbClient.putItem({ | |
"TableName": "blog", | |
"ReturnConsumedCapacity": "INDEXES", | |
"Item": { | |
"author": { | |
"S": "bboure" | |
}, | |
"slug": { | |
"S": "indexed-raw-blog-post" | |
}, | |
"timestamp": { | |
"S": `${Date.now()}`, | |
}, | |
"title": { | |
"S": "My blog post" | |
}, | |
"content": { | |
"S": content, | |
} | |
} | |
}).promise().then(result => { | |
console.log('Write capacity for raw post', result.ConsumedCapacity ); | |
}); | |
dynamoDbClient.putItem({ | |
"TableName": "blog", | |
"ReturnConsumedCapacity": "INDEXES", | |
"Item": { | |
"author": { | |
"S": "bboure" | |
}, | |
"slug": { | |
"S": "indexed-compressed-blog-post" | |
}, | |
"timestamp": { | |
"S": `${Date.now()}`, | |
}, | |
"title": { | |
"S": "My blog post" | |
}, | |
"content": { | |
"B": compressed, | |
} | |
} | |
}).promise().then(result => { | |
console.log('Write capacity for compressed post', result.ConsumedCapacity ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As see on https://benoitboure.com/how-to-store-large-attribute-values-in-dynamodb