Key-Value

❗️

This is a legacy Apache Ignite documentation

The new documentation is hosted here: https://ignite.apache.org/docs/latest/

Supported JCache API

Presently, thin client implements only a subset of JCache and for that reason does not implement javax.cache.Cache (and ClientCacheConfiguration does not implement javax.cache.configuration).

The following JCache API is supported by ClientCache<K, V>:

  • V get(K key)
  • void put(K key, V val)
  • boolean containsKey(K key)
  • String getName()
  • CacheClientConfiguration getConfiguration()
  • Map<K, V> getAll(Set<? extends K> keys)
  • void putAll(Map<? extends K, ? extends V> map)
  • boolean replace(K key, V oldVal, V newVal)
  • boolean replace(K key, V val)
  • boolean remove(K key)
  • boolean remove(K key, V oldVal)
  • void removeAll(Set<? extends K> keys)
  • void removeAll()
  • V getAndPut(K key, V val)
  • V getAndRemove(K key)
  • V getAndReplace(K key, V val)
  • boolean putIfAbsent(K key, V val)
  • void clear()

ClientCache<K, V> exposes advanced cache API not included in JCache:

  • int size(CachePeekMode... peekModes)
Map<Integer, String> data = IntStream.rangeClosed(1, 100).boxed()
    .collect(Collectors.toMap(i -> i, Object::toString));

cache.putAll(data);

assertFalse(cache.replace(1, "2", "3"));
assertEquals("1", cache.get(1));
assertTrue(cache.replace(1, "1", "3"));
assertEquals("3", cache.get(1));

cache.put(101, "101");

cache.removeAll(data.keySet());
assertEquals(1, cache.size());
assertEquals("101", cache.get(101));

cache.removeAll();
assertEquals(0, cache.size());

Scan Queries

Use a ScanQuery<K, V> to filter cache entries on the server with a Java predicate and iterate the filtered result on the client side.
Filtered entries are transferred to the client in pages so that only one page is loaded in the client's memory. The page size is set by ScanQuery#setPageSize(int).

Query<Cache.Entry<Integer, Person>> qry = new ScanQuery<Integer, Person>((i, p) -> p.getName().contains("Smith")).setPageSize(1000);

try (QueryCursor<Cache.Entry<Integer, Person>> cur = cache.query(qry)) {
  for (Cache.Entry<Integer, Person> entry : cur) {
    // Handle the entry ...
  }
}

Client Transactions

Java Thin Client supports key-value transactions. The Transactions API is similar to the transactions API in the Ignite core functionality.

Transactions can only be executed on caches that have the AtomicityMode.TRANSACTIONAL atomicity mode.

Executing Transactions

Obtain the ClientTransactions object from the IgniteClient instance. ClientTransactions allows you to start transactions using one of the txStart(...) methods, which returns a ClientTransaction object.

Use ClientTransaction to commit or rollback the transaction.

ClientCache<Integer, String> cache = client.cache("my_transactional_cache");

ClientTransactions tx = client.transactions();

try (ClientTransaction t = tx.txStart()) {
    cache.put(1, "new value");

    t.commit();
}

Transaction Configuration

Client transactions can have different concurrency modes, isolation levels, and execution timeout, which can be set for all transactions or on a per transaction basis.

The client configuration object supports setting the default concurrency mode, isolation level, and timeout for all transactions started with this client interface.

ClientConfiguration cfg = new ClientConfiguration();
 cfg.setAddresses("localhost:10800");

cfg.setTransactionConfiguration(new ClientTransactionConfiguration()
       .setDefaultTxTimeout(10000)
       .setDefaultTxConcurrency(TransactionConcurrency.OPTIMISTIC)
       .setDefaultTxIsolation(TransactionIsolation.REPEATABLE_READ));

IgniteClient client = Ignition.startClient(cfg);

You can also specify the concurrency mode, isolation level, and timeout when starting an individual transaction. In this case, the provided values will override the default settings.

ClientTransactions tx = client.transactions();
        
try (ClientTransaction t = tx.txStart(TransactionConcurrency.OPTIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
    cache.put(1, "new value");
    t.commit();
}