|
| 1 | +# Getting started |
| 2 | + |
| 3 | +The TiKV client is a Rust library (crate). To use this crate in your project, add the following dependencies to your `Cargo.toml`: |
| 4 | + |
| 5 | +```toml |
| 6 | +[dependencies] |
| 7 | +tikv-client = "0.1" |
| 8 | +tokio = { version = "1.5", features = ["full"] } |
| 9 | +``` |
| 10 | + |
| 11 | +Note that you need to use Tokio. The TiKV client has an async API and therefore you need an async runtime in your program to use it. At the moment, Tokio is used internally in the client and so you must use Tokio in your code too. We plan to become more flexible in future versions. |
| 12 | + |
| 13 | +The minimum supported version of Rust is 1.40. The minimum supported version of TiKV is 5.0. |
| 14 | + |
| 15 | +The general flow of using the client crate is to create either a raw or transaction client object (which can be configured) then send commands using the client object, or use it to create transactions objects. In the latter case, the transaction is built up using various commands and then committed (or rolled back). |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | +To use the client in your program, use code like the following. |
| 20 | + |
| 21 | +Raw mode: |
| 22 | + |
| 23 | +```rust |
| 24 | +use tikv_client::RawClient; |
| 25 | + |
| 26 | +let client = RawClient::new(vec!["127.0.0.1:2379"]).await?; |
| 27 | +client.put("key".to_owned(), "value".to_owned()).await?; |
| 28 | +let value = client.get("key".to_owned()).await?; |
| 29 | +``` |
| 30 | + |
| 31 | +Transactional mode: |
| 32 | + |
| 33 | +```rust |
| 34 | +use tikv_client::TransactionClient; |
| 35 | + |
| 36 | +let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?; |
| 37 | +let mut txn = txn_client.begin_optimistic().await?; |
| 38 | +txn.put("key".to_owned(), "value".to_owned()).await?; |
| 39 | +let value = txn.get("key".to_owned()).await?; |
| 40 | +txn.commit().await?; |
| 41 | +``` |
| 42 | + |
| 43 | +To make an example which builds and runs, |
| 44 | + |
| 45 | +```rust |
| 46 | +use tikv_client::{TransactionClient, Error}; |
| 47 | + |
| 48 | +async fn run() -> Result<(), Error> { |
| 49 | + let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?; |
| 50 | + let mut txn = txn_client.begin_optimistic().await?; |
| 51 | + txn.put("key".to_owned(), "value".to_owned()).await?; |
| 52 | + let value = txn.get("key".to_owned()).await?; |
| 53 | + println!("value: {:?}", value); |
| 54 | + txn.commit().await?; |
| 55 | + Ok(()) |
| 56 | +} |
| 57 | + |
| 58 | +#[tokio::main] |
| 59 | +async fn main() { |
| 60 | + run().await.unwrap(); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +For more examples, see the [examples](examples) directory. |
0 commit comments