Best Practices for Developing Applications

Scanning Over Tables with Byte Array Keys

In the CDAP Java API, Bytes.stopKeyForPrefix() is a very handy tool when performing scans over tables with byte array (byte[]) keys.

The method returns a given prefix, incremented by one, in a form that is suitable for prefix matching. Here is an example, showing stopKeyForPrefix being used to generate, from a provided index key, an incremented stop key:

// Returns first that matches @Nullable public <T> T get(Key id, Class<T> classOfT) { try { Scanner scan = table.scan(id.getKey(), Bytes.stopKeyForPrefix(id.getKey())); Row row = scan.next(); if (row == null || row.isEmpty()) { return null; } byte[] value = row.get(COLUMN); if (value == null) { return null; } return deserialize(value, classOfT); } catch (Exception e) { throw Throwables.propagate(e); } }

 

Created in 2020 by Google Inc.