LoginSignup
0
0

More than 3 years have passed since last update.

Rust: Redis のデータを読む (Read)

Posted at

フォルダー構造

$ 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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0