spotify/simple-bigtable

Name: simple-bigtable

Owner: Spotify

Description: null

Created: 2016-02-26 19:05:11.0

Updated: 2018-04-21 18:03:15.0

Pushed: 2018-03-30 16:43:09.0

Homepage: null

Size: 167

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Simple Bigtable

Overview

Cloud Bigtable is a datastore supported by Google for storing huge amounts of data and maintaining very low read latency. The main drawback to using Bigtable is that Google does not currently have an official asynchronous client. Within Spotify we have been using the RPC client which is a pain to use. This library aims to fix that by making the most common interactions with Bigtable clean and easy to use while not preventing you from doing anything you could do with the RPC client.

To import with maven, add this to your pom:

endency>
<groupId>com.spotify</groupId>
<artifactId>simple-bigtable</artifactId>
<version>LATEST_RELEASE</version>
pendency>
Raw RPC Client vs Bigtable Client Comparison
Using The RPC Client

To give an example of using the base RPC client (which gives the BigtableSession object), this is how you would request a single cell from Bigtable.

ng projectId;
ng zone;
ng cluster;
ableSession session;

ng fullTableName = String.format("projects/%s/zones/%s/clusters/%s/tables/%s",
    projectId,
    zone,
    cluster,
    "table");

ould also use a filter chain, but you can't actually set all the filters within the same RowFilter object
ithout a merge or chain of some sort
l RowFilter.Builder filter = RowFilter.newBuilder().setFamilyNameRegexFilter("column-family");
er.mergeFrom(RowFilter.newBuilder().setColumnQualifierRegexFilter(ByteString.copyFromUtf8("column-1")).build());
er.mergeFrom(RowFilter.newBuilder().setCellsPerColumnLimitFilter(1).build()); // By default it is 1

l ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder()
    .setTableName(fullTableName)
    .setRowKey(ByteString.copyFromUtf8("row"))
    .setNumRowsLimit(1)
    .setFilter(filter.build())
    .build();

l ListenableFuture<List<Row>> future = session.getDataClient().readRowsAsync(readRowsRequest);
l ListenableFuture<Cell> cell = FuturesExtra.syncTransform(future, rows -> {
 This doesnt actually check if the row, column family, and qualifier exist
 IndexOutOfBoundsException might be thrown
turn rows.get(0).getFamilies(0).getColumns(0).getCells(0);

Bigtable Client

The goal of this client is to let you query what you want with minimal overhead (there should be no need to create all these filter objects) as well as give you the object you want without needing to constantly convert a list of rows down to a single cell. Note that these examples use a String as a row key. Bigtable keys are really byte arrays. Strings in this api is just a convenience. Under the cover the string “row” is converted to a ByteString. In reality you should use byte arrays as keys as that will be more efficient.

Here is the same query as above using this client wrapper.

ng projectId;
ng zone;
ng cluster;
ableSession session;

able bigtable = new Bigtable(session, projectId, zone, cluster);
l ListenableFuture<Optional<Cell>> cell = bigtable.read("table")
.row("row")
.column("family:qualifier") // specify both column family and column qualifier separated by colon
.latestCell()
.executeAsync();
Performing Reads

The goal of this client is to make the most tedious and common interactions with Bigtable as painless as possible. Therefore reading data is an extremely large focus. Here are some examples of reading data.

Get full column family within row

l ListenableFuture<Optional<Family>> family = bigtable.read("table")
.row("row")
.family("family")
.executeAsync();

Get multiple columns within a row (Currently all need to be in the same column family but hopefully that gets fixed)

et the entire column
l ListenableFuture<List<Column>> family = bigtable.read("table")
.row("row")
.family("family")
.columnQualifiers(Lists.newArrayList("qualifier-1", "qualifier-2"))
.executeAsync();

et the latest cell in each column
l ListenableFuture<List<Column>> family = bigtable.read("table")
.row("row")
.family("family")
.columnQualifiers(Lists.newArrayList("qualifier1", "qualifier2"))
.latestCell()
.executeAsync();

Get columns within a single family and within column qualifier range

l ListenableFuture<List<Column>> columns = bigtable.read("table")
.row("row")
.family("family")
.columns()
.startQualifierInclusive(startBytestring)
.endQualifierExclusive(endBytestring)
.executeAsync();

Get cells between certain timestamps within a column

l ListenableFuture<List<Cell>> cells = bigtable.read("table")
.row("row")
.column("family:qualifier")
.cells()
.startTimestampMicros(someTimestamp)
.endTimestampMicros(someLatertimestamp)
.executeAsync();

Get the latest cell of a certain value within a column

l ListenableFuture<Optional<Cell>> cells = bigtable.read("table")
.row("row")
.column("family:qualifier")
.cells()
.startValueInclusive(myValueByteString)
.endValueInclusive(myValueByteString)
.latest()
.executeAsync();

Get the latest cell of a between 2 timestamps within a column for multiple rows

l ListenableFuture<List<Row>> cells = bigtable.read("table")
.rows(ImmutableSet.of("row1", "row2"))
.column("family:qualifier")
.cells()
.startTimestampMicros(someTimestamp)
.endTimestampMicros(someLatertimestamp)
.latest()
.executeAsync();

Get the multiple column families and column qualifiers (will match all combinations)

l ListenableFuture<List<Row>> cells = bigtable.read("table")
.row("row")
.families(ImmutableSet.of("family1, family2"))
.columnQualifiers(ImmutableSet.of("qualifier1", "qualifier2")
.cells()
.startTimestampMicros(someTimestamp)
.endTimestampMicros(someLatertimestamp)
.latest()
.executeAsync();

Get all rows between different ranges or with certain specific keys (these functions add rows to the row set, instead of filtering)

l ListenableFuture<List<Row>> rows = bigtable.read("table")
.rows()
.addRowRangeOpen(myStartKeyOpen, myEndKeyOpen) // add an exclusive range
.addRowRangeClosed(myStartKeyClosed, myEndKeyClosed) // add an inclusive range
.addKeys(extraKeys) // add some keys you always want
.executeAsync();

Note that currently there is no half open, half closed range.

Other Operations

The client supports other Bigtable operations as well, with hopefully the rest of all possible operations coming soon.

Mutations (Writes, Deletions)

Mutations are performed on the row level with many mutations possible within a single call. Mutations include writing new values as well as deleting a column, column family, or an entire row and all data help in each.

Write a new cell within a column

l ListenableFuture<Empty> mutation = bigtable.mutateRow("table", "row")
.write("family:qualifier", ByteString.copyFromUtf8("value"))
.executeAync()

Perform multiple writes in different columns setting an explicit timestamp on some

l ListenableFuture<Empty> mutation = bigtable.mutateRow("table", "row")
.write("family:qualifier", ByteString.copyFromUtf8("value-1"), timestampMicros)
.write("family", "qualifier", ByteString.copyFromUtf8("value-2"))
.executeAync()

Delete a column and then write to the same column

l Empty mutation = bigtable.mutateRow("table", "row")
.deleteColumn("family:qualifier")
.write("family:qualifier", ByteString.copyFromUtf8("brand-new-value"))
.execute()
ReadModifyWrite (Atomically Update or Append To A Column)

ReadModifyWrite is useful for either incrementing the latest cell within a column by a long or appenging bytes to the value. If the column is empty, is will write a new value. Once again this operation is on the row level with multiple ReadModifyWrites possible in a single request.

Increment a couple counter columns and append a value to another

able.readModifyWrite("table", "row")
.read("request-numbers:number-1")
.thenIncrementAmount(1L)
.read("request-numbers:number-2")
.thenIncrementAmount(5L)
.read("family:values")
.thenAppendValue(ByteString.copyFromUtf8("new-value"))
.executeAsync();
SampleRowKeys

Sample some row keys from a table.

l List<SampleRowKeysResponse> sampleRowKeys = bigtable.sampleRowKeys("table").execute();
CheckAndMutateRow - NOT YET IMPLEMENTED

Perform a read and a set of mutations depending on whether the read returns data. This is not yet implemented but here are some ideas on how this operation might be implemented in the future.

Have the check specified like a read, then allow mutations to be added.

able.checkAndMutateRow("table", "row")
.column("family:qualifier")
.cells()
.endTimestampMicros(timestamp)
.ifExists()
.deleteColumn("family:qualifier")
.write("family:qualifier", "had-data")
.ifDoesNotExist()
.write("family:qualifier", "did-not-have-data")
.executeAsync();

Pass in Bigtable protobuf objects, kind of against the purpose of the library but keeps things simple.

able.checkAndMutateRow("table", "row")
.rowFilter(someRowFilter)
.ifExists(someMutation)
.ifExists(someOtherMutation)
.ifDoesNotExist(someOtherMutation)
.executeAsync();

Pull requests with other ideas are encouraged.

Table and Cluster Admin Operations - NOT YET IMPLEMENTED

It is unclear whether there is a need this wrapper to provide the admin operations, though it would be pretty easy to include.

How to Release
<server>
  <!-- sonatype repository -->
  <id>ossrh</id>
  <username>access-token-name</username> <!-- access token tied to an account sonatype.org -->
  <password>access-token-password</password>
</server>
Open Problems and Questions
Code of conduct

This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.