Rust: Redis のデータを更新 (Update)
フォルダー構造
$ tree -L 2
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
└── debug
Cargo.toml
[package]
name = "redis_update"
version = "0.1.0"
edition = "2018"
#
[dependencies.redis]
version = "*"
[dependencies]
tokio = { version = "0.2", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = "0.4"
src/main.rs
// --------------------------------------------------------------------
/*
redis_update/src/main.rs
Jul/22/2020
*/
// --------------------------------------------------------------------
use std::env;
extern crate redis;
use redis::{Commands};
use chrono::{Local, Date};
use serde_json::json;
// --------------------------------------------------------------------
fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln! ("*** 開始 ***");
let args: Vec<_> = env::args().collect();
let key_in = &args[1];
let population_in = &args[2];
println! ("{}\t{}",key_in,population_in);
let client = redis::Client::open("redis://localhost/").expect("url error");
let mut conn = client.get_connection().expect("connect error");
let res = conn.get(key_in.to_string());
let answer: String = res.unwrap();
let json: serde_json::Value = serde_json::from_str(&answer)?;
let obj = json.as_object().unwrap();
println! ("{}",obj["name"].to_string());
println! ("{}",obj["population"].to_string());
println! ("{}",obj["date_mod"].to_string());
let date_mod: Date<Local> = Local::today();
println!("{}", date_mod);
let pp_in : i32 = population_in.parse().unwrap();
let obj_new = json!({
"name": obj["name"],
"population": pp_in,
"date_mod": date_mod.to_string()
});
let json_str = obj_new.to_string();
println!("{}",json_str);
let _: () = conn.set(key_in.to_string(), json_str).unwrap();
eprintln! ("*** 終了 ***");
Ok(())
}
// --------------------------------------------------------------------
コンパイル
cargo build
実行
cargo run t1857 5128900
Author And Source
この問題について(Rust: Redis のデータを更新 (Update)), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/d477ccb1a410e7730385著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .