Rust: Redis のデータを読む (Read)
フォルダー構造
$ tree -L 2
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
└── debug
Cargo.toml
[package]
name = "redis_read"
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"
src/main.rs
// --------------------------------------------------------------------
/*
redis_read/src/main.rs
Jul/22/2020
*/
// --------------------------------------------------------------------
extern crate redis;
use redis::{Commands};
fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln! ("*** 開始 ***");
let client = redis::Client::open("redis://localhost/").expect("url error");
let mut conn = client.get_connection().expect("connect error");
let mut keys : Vec<String> = conn.keys("t*")?;
println!("{}",keys.len());
keys.sort();
for it in &keys{
let res = conn.get(it.to_string());
let answer: String = res.unwrap();
let json: serde_json::Value = serde_json::from_str(&answer)?;
let obj = json.as_object().unwrap();
let str_out = it.to_string() + "\t"
+ &obj["name"].to_string() + "\t"
+ &obj["population"].to_string() + "\t"
+ &obj["date_mod"].to_string();
println!("{}",str_out);
}
eprintln! ("*** 終了 ***");
Ok(())
}
// --------------------------------------------------------------------
コンパイル
cargo build
実行
cargo run
Author And Source
この問題について(Rust: Redis のデータを読む (Read)), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/685ce4c64e103cb41d67著者帰属:元の著者の情報は、元の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 .