简体   繁体   中英

How do I put my struct into a PutRecordInput for AWS Kinesis in Rust?

I am very new to Rust and trying to get a bit of code working that can push a record to a kinesis firehose stream.

struct AuditRecord{
    user_id : String,
    request : Value,
    request_id : String,
    timestamp_raw : i64,
}
...
let client = KinesisClient::new(Region::UsEast1);
let record = PutRecordInput{
    partition_key : requestId.to_string(),
    stream_name : streamName,
    data : auditRecord
};

When I tried this it wants the data in bytes::bytes::Bytes but I am unclear as to how to convert my struct to a bytes::bytes::Bytes representation. Any examples of how to go about this would be appreciated.

For clarification I am using

rusoto = "0.24.2"
rusoto_kinesis = "0.43.0"
rusoto_core = "0.43.0"

Also if anyone know of a good place to see real examples of how to use rusoto to talk with varios AWS entities that would be appreciated.

As other people have stated, It really depends on your choice of serialization. If you chose a string based encoding like xml or json, then you could use something like:

let msg: String = "some message".to_string();

let record = PutRecordInput{
    partition_key : requestId.to_string(),
    stream_name : streamName,
    data : msg.into()
};

// or it could be 
let record = PutRecordInput{
    partition_key : requestId.to_string(),
    stream_name : streamName,
    data : Bytes::from(msg)
};

Where you replace "some message" with your string sterilization of choice.

Though technically, using some binary encoding like protobuf or something are usually a lot more compact, but then whatever is on the other side must be able to interpret the format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM