Skip to content

komamitsu/td-client-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

td-client-rust

Rust Client Library for Treasure Data

Install

Add the following dependency in Cargo.toml

[dependencies]
td-client = "0.9"

Usage

First, add this line to your crate root

extern crate td_client;

Next, set up a td-client with your API key for Treasure Data

use td_client::client::*;
use td_client::model::*;

let client = Client::new("your API key");

Manipulate Database metadata

client.create_database("my_database").unwrap();
println!("{:?}", client.databases().unwrap());
client.delete_database("unused_database").unwrap();

Manipulate Table metadata

client.create_table("my_database", "my_table").unwrap();
println!("{:?}", client.tables("my_database").unwrap());
client.swap_table("my_database", "my_table", "my_temp_table").unwrap();
client.rename_table("my_database", "my_temp_table", "unused_table").unwrap();
client.delete_table("my_database", "unused_table").unwrap();

Import data to table

// Import msgpack gzipped file
client.import_msgpack_gz_file_to_table("my_database", "my_table",
                        "/tmp/2016-08-01.msgpack.gz", None).unwrap();

// Import records
let mut chunk = TableImportWritableChunk::new().unwrap();
chunk.next_row(4).unwrap();
chunk.write_key_and_i64("time", time::get_time().sec).unwrap();
chunk.write_key_and_str("name", "foo").unwrap();
chunk.write_key_and_u8("age", 42).unwrap();
chunk.write_key_and_f32("pi", 3.14).unwrap();

chunk.next_row(3).unwrap();
chunk.write_key_and_i64("time", time::get_time().sec).unwrap();
chunk.write_key_and_str("name", "bar").unwrap();
chunk.write_key_and_u8("age", 56).unwrap();

let readable_chunk = chunk.close().unwrap();

client.import_msgpack_gz_file_to_table("my_database", "my_table", 
			readable_chunk.file_path.as_str(), None).unwrap();

Information of jobs

// List up jobs
println!("{:?}", client.jobs(Some(JobStatusOption::Success), None, None).unwrap());

// Look at the job
println!("{:?}", client.job(1234567).unwrap());

// Check the job's status
println!("{:?}", client.job_status(1234567).unwrap());

Issue a query

// Issue a query
let job_id = client.issue_job(
	QueryType::Presto, "sample_datasets",
	"select code, method, count(1) as cnt from www_access group by code, method",
	None, None, None, None, None).unwrap();

println!("job_id={}, status={:?}", job_id, client.wait_job(job_id, None).unwrap());

// Download the result to a file
let result_file = File::create("/tmp/result.msgpack.gz").unwrap();
client.download_job_result(job_id, &result_file).unwrap();

// Do something for each record
client.each_row_in_job_result(job_id, &|xs| println!(">>>> {:?}", xs));

About

Rust Client Library for Treasure Data

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages