TiKV offers two APIs that you can interact with:

APIDescriptionAtomicityUse when…
RawA lower-level key-value API for interacting directly with individual key-value pairs.Single keyYour application doesn't require distributed transactions or multi-version concurrency control (MVCC)
TransactionalA higher-level key-value API that provides ACID semanticsMultiple keysYour application requires distributed transactions and/or MVCC

There are several clients that connect to TiKV:

Below we use the Rust client for some examples, but you should find all clients work similarly.

Basic Types

Both clients use a few basic types for most of their API:

  • Key, a wrapper around a Vec<u8> symbolizing the ‘key’ in a key-value pair.
  • Value, a wrapper around a Vec<u8> symbolizing the ‘value’ in a key-value pair.
  • KvPair, a tuple of (Key, Value) representing a key-value pair.
  • KeyRange, a trait representing a range of Keys from one value to either another value, or the end of the entire dataset.

The Key and Value types implement Deref<Target=Vec<u8>> so they can generally be used just like their contained values. Where possible API calls accept impl Into<T> instead of the type T when it comes to Key, Value, and KvPair.

If you're using your own key or value types, we reccomend implementing Into<Key> and/or Into<Value> for them where appropriate. You can also impl KeyRange if you have any range types.

Add the dependency

This guide assumes you are using Rust 1.31 or above. You will also need an already deployed TiKV and PD cluster, since TiKV is not an embedded database.

To start, open the Cargo.toml of your project, and add the tikv-client and futures as dependencies.

  1. [dependencies]
  2. tikv-client = { git = "https://github.com/tikv/client-rust" }
  3. futures = "0.1"

Connect a client

In your src/main.rs, import the raw API as well as the functionality of the Future trait.

Note: In this example we used raw, but you can also use transaction. The process is the same.

  1. use tikv_client::{Config, raw::Client}
  2. use futures::Future;

Build an instance of Config, then use it to build an instance of a Client.

  1. let config = Config::new(vec![ // Always use more than one PD endpoint!
  2. "192.168.0.100:2379",
  3. "192.168.0.101:2379",
  4. "192.168.0.102:2379",
  5. ]).with_security( // Configure TLS if used.
  6. "root.ca",
  7. "internal.cert",
  8. "internal.key",
  9. );
  10. let unconnected_client = Client::new(config);
  11. let client = unconnected_client.wait()?; // Block and resolve the future.

The value returned by Client::new is a Future. Futures need to be resolved in order to obtain the output. During the resolution of the future the client must create a connection with the cluster.

With a connected client, you'll be able to send requests to TiKV. This client supports both singlular or batch operations.

Raw key-value API

Using a connected raw::Client, you can perform actions such as put, get, and delete:

  1. let client = Client::new(config).wait();
  2. // Data stored in TiKV does not need to be UTF-8.
  3. let key = "TiKV".to_bytes();
  4. let value = "Astronaut".to_bytes();
  5. // This creates a future that must be resolved.
  6. let req = client.put(
  7. key, // Vec<u8> impl Into<Key>
  8. value // Vec<u8> impl Into<Value>
  9. );
  10. req.wait()?;
  11. let req = client.get(key);
  12. let result = req.wait()?;
  13. // `Value` derefs to `Vec<u8>`.
  14. assert_eq!(result, Some(value));
  15. let req = client.delete(key);
  16. req.wait()?;
  17. let req = client.get(key).wait()?;
  18. assert_eq!(result, None);

You can also perform scans, giving you all the values for keys in a given range:

  1. // For stability and reliability, it's good to chose a reasonable limit.
  2. const REASONABLE_LIMIT = 1000;
  3. // If you are using UTF-8,`Key` and `Value` arguments can be provided as
  4. // `String` or `&'static str` as well.
  5. const (START, END) = ("C", "F");
  6. // Scanning can also work on an open end (Eg `START..`)
  7. let req = client.scan(START..END, REASONABLE_LIMIT);
  8. let result: Vec<KvPair> = req.wait()?;

These functions also have batch variants which accept sets and return Vec<_>s of data. These offer considerably reduced network overhead and can result in dramatic performance increases under certain workloads.

For documented, tested examples of all functionalities, check the documentation of raw::Client in the generated Rust documentation.

Transactional key-value API

The transactional API of the Rust client is incomplete. You can track the progress with issue #15. For a complete implementation, you can try the Go client.

Using a connected transaction::Client you can then begin a transaction:

  1. let client = Client::new(config).wait();
  2. let txn = client.begin();

Then it's possible to send commands like get, set, delete, or scan. Batch variants also exist.

  1. // `Key` and `Value` wrap around `Vec<u8>` values.
  2. // This means data need not be UTF-8.
  3. let key = "TiKV".to_bytes();
  4. let value = "Astronaut".to_bytes();
  5. // This creates a future that must be resolved.
  6. let req = txn.set(key, value);
  7. req.wait()?;
  8. let req = txn.get(key);
  9. let result = req.wait()?;
  10. // `Value` and `Key` deref to `Vec<u8>`.
  11. // This means you should find them easy to work with.
  12. assert_eq!(result, Some(value));
  13. let req = txn.delete(key);
  14. req.wait()?;
  15. let req = txn.get(key).wait()?;
  16. assert_eq!(result, None);
  17. // For more detail on scanning, see the raw section above or the documentation.
  18. let req = client.scan("A".."B", 1000);
  19. let result: Vec<KvPair> = req.wait()?;

Commit these changes when you're ready, or roll back if you prefer to abort the operation:

  1. if all_is_good {
  2. txn.commit()?;
  3. } else {
  4. txn.rollback()?
  5. }

Beyond the Basics

At this point you're familiar with the basic functionality of TiKV. To begin integrating with TiKV you should explore the documentation of your favorite client from those we listed above.

For the Rust client, you can find the full documentation for the client (and all your dependencies) by running:

  1. cargo doc --package tikv-client --open