Expand description
§DynamoDB
In its low level API, DynamoDB works with JSON objects with extra levels to set the type of the values.
{
"Item": {
"Age": {"N": "8"},
"Colors": {
"L": [
{"S": "White"},
{"S": "Brown"},
{"S": "Black"}
]
},
"Name": {"S": "Fido"},
"Vaccinations": {
"M": {
"Rabies": {
"L": [
{"S": "2009-03-17"},
{"S": "2011-09-21"},
{"S": "2014-07-08"}
]
},
"Distemper": {"S": "2015-10-13"}
}
},
"Breed": {"S": "Beagle"},
"AnimalType": {"S": "Dog"}
}
}
The allowed type keys are described here.
§Rusoto DynamoDB
Rusoto DynamoDB map those values to AttributeValue
,
and functions to get/set/… from DynamoDB use HashMap<String, AttributeValue>
as a way to represent the data.
§Parsing HashMap as strongly typed data structures
Serde provides a powerful way of mapping HashMap data into Rust data structures largely automatically by using serde_dynamodb_streams::from_hashmap
extern crate serde;
extern crate serde_dynamodb;
extern crate rusoto_core;
extern crate rusoto_dynamodb;
#[macro_use]
extern crate serde_derive;
use std::collections::HashMap;
use rusoto_core::Region;
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, QueryInput, AttributeValue};
use serde_dynamodb_streams::Error;
#[derive(Serialize, Deserialize, Debug)]
struct Person {
surname: String,
age: u8,
phones: Vec<String>,
}
fn typed_example() -> Result<(), Error> {
let client = DynamoDbClient::simple(Region::UsEast1);
let mut query = HashMap::new();
query.insert(String::from(":surname"), AttributeValue {
s: Some(String::from("Smith")),
..Default::default()
});
// get data from DynamoDB
let persons: Vec<Person> = client
.query(&QueryInput {
table_name: String::from("person"),
key_condition_expression: Some(String::from("surname = :surname")),
expression_attribute_values: Some(query),
..Default::default()
})
.sync()
.unwrap()
.items
.unwrap_or_else(|| vec![])
.into_iter()
.map(|item| serde_dynamodb_streams::from_hashmap(item).unwrap())
.collect();
// Do things just like with any other Rust data structure.
for p in persons {
println!("Please call {} at the number {}", p.surname, p.phones[0]);
}
Ok(())
}
§Creating an HashMap by serializing data structures
A data structure can be converted to an HashMap by
serde_dynamodb_streams::to_hashmap
.
use serde::{Serialize, Deserialize};
use serde_dynamodb_streams::Error;
#[derive(Serialize, Deserialize, Debug)]
struct Address {
street: String,
city: String,
}
fn print_an_address() -> Result<(), Error> {
// Some data structure.
let address = Address {
street: "10 Downing Street".to_owned(),
city: "London".to_owned(),
};
// Serialize it to an HashMap.
let dynamodb_object = serde_dynamodb_streams::to_hashmap(&address)?;
// Print, write to a file, or send to an HTTP server.
println!("{:?}", dynamodb_object);
Ok(())
}
§Features
§rusoto_dynamodb
Feature rusoto_dynamodb
is enabled by default and add methods
serde_dynamodb_streams::from_hashmap
and
serde_dynamodb_streams::to_hashmap
. Those methods work with
AttributeValue
from DynamoDB.
§rusoto_dynamodbstreams
Feature rusoto_dynamodbstreams
is disabled by default and add module
streams
with methods
serde_dynamodb_streams::streams::from_hashmap
and
serde_dynamodb_streams::streams::to_hashmap
. Those methods work with
AttributeValue
from DynamoDB Streams.
Re-exports§
pub use error::Error;
Modules§
- error
- This type represents all possible errors that can occur when serializing or deserializing DynamoDB data.
- streams
- Methods in this module are generated to work with
AttributeValue
from DynamoDB Streams instead ofAttributeValue
from DynamoDB.