0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rust: Redis のデータを作成 (Create)

Posted at

フォルダー構造

$ tree -L 2
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    └── debug
Cargo.toml
[package]
name = "redis_create"
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_create/src/main.rs

					Jul/22/2020
*/
// --------------------------------------------------------------------
extern crate redis;
use redis::{Commands};
use serde_json::json;
use std::collections::HashMap;

// --------------------------------------------------------------------
fn unit_prepare_proc (name:&'static str,population: i32,date_mod:&'static str) -> String {
	let obj_new = json!({
		"name": name,
		"population": population,
		"date_mod": date_mod
		});

	let json_str = obj_new.to_string();

	return json_str.to_string();
}

// --------------------------------------------------------------------
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 dict_aa:HashMap<String,String> = HashMap::new();

	let mut json_str = unit_prepare_proc ("福井",53921,"1956-6-9");
	dict_aa.insert("t1851".to_string(),json_str);

	json_str = unit_prepare_proc ("敦賀",82154,"1956-3-4");
	dict_aa.insert("t1852".to_string(),json_str);

	json_str = unit_prepare_proc ("小浜",82157,"1956-10-17");
	dict_aa.insert("t1853".to_string(),json_str);

	json_str = unit_prepare_proc ("大野",13594,"1956-2-21");
	dict_aa.insert("t1854".to_string(),json_str);

	json_str = unit_prepare_proc ("勝山",52836,"1956-12-7");
	dict_aa.insert("t1855".to_string(),json_str);

	json_str = unit_prepare_proc ("鯖江",89132,"1956-10-17");
	dict_aa.insert("t1856".to_string(),json_str);

	json_str = unit_prepare_proc ("あわら",35126,"1956-8-25");
	dict_aa.insert("t1857".to_string(),json_str);

	json_str = unit_prepare_proc ("越前",56923,"1956-4-8");
	dict_aa.insert("t1858".to_string(),json_str);

	json_str = unit_prepare_proc ("坂井",18274,"1956-7-10");
	dict_aa.insert("t1859".to_string(),json_str);

	for (key,value) in &dict_aa {
		let _: ()  = conn.set(key.to_string(), value).unwrap();
		}

	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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?