Skip to content

Commit

Permalink
Bigtable: readRow methods added with Query as argument (#4092)
Browse files Browse the repository at this point in the history
  • Loading branch information
elisheva-qlogic authored and igorbernstein2 committed Dec 10, 2018
1 parent a9a7044 commit bf26f86
Show file tree
Hide file tree
Showing 2 changed files with 304 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
import com.google.cloud.bigtable.data.v2.models.BulkMutationBatcher;
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
import com.google.cloud.bigtable.data.v2.models.InstanceName;
import com.google.cloud.bigtable.data.v2.models.KeyOffset;
import com.google.cloud.bigtable.data.v2.models.Query;
Expand All @@ -37,6 +38,7 @@
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.util.List;
import javax.annotation.Nullable;

/**
* Client for reading from and writing to existing Bigtable tables.
Expand Down Expand Up @@ -143,7 +145,7 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO
*
* <p>Sample code:
*
* <pre>{code
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE]";
Expand All @@ -153,7 +155,8 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO
* if(row != null) {
* System.out.println(row.getKey().toStringUtf8());
* for(RowCell cell : row.getCells()) {
* System.out.println("Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() + " Value: " + cell.getValue().toStringUtf8());
* System.out.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(),
* cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
* }
* }
* } catch(ApiException e) {
Expand All @@ -164,7 +167,7 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public Row readRow(String tableId, ByteString rowKey) {
return ApiExceptions.callAndTranslateApiException(readRowAsync(tableId, rowKey));
return ApiExceptions.callAndTranslateApiException(readRowAsync(tableId, rowKey, null));
}

/**
Expand All @@ -173,7 +176,7 @@ public Row readRow(String tableId, ByteString rowKey) {
*
* <p>Sample code:
*
* <pre>{code
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE]";
Expand All @@ -183,7 +186,8 @@ public Row readRow(String tableId, ByteString rowKey) {
* if(row != null) {
* System.out.println(row.getKey().toStringUtf8());
* for(RowCell cell : row.getCells()) {
* System.out.println("Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() + " Value: " + cell.getValue().toStringUtf8());
* System.out.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(),
* cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
* }
* }
* } catch(ApiException e) {
Expand All @@ -194,7 +198,81 @@ public Row readRow(String tableId, ByteString rowKey) {
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public Row readRow(String tableId, String rowKey) {
return ApiExceptions.callAndTranslateApiException(readRowAsync(tableId, rowKey));
return ApiExceptions.callAndTranslateApiException(
readRowAsync(tableId, ByteString.copyFromUtf8(rowKey), null));
}

/**
* Convenience method for synchronously reading a single row. If the row does not exist, the value
* will be null.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE]";
*
* // Build the filter expression
* Filter filter = FILTERS.chain()
* .filter(FILTERS.qualifier().regex("prefix.*"))
* .filter(FILTERS.limit().cellsPerRow(10));
*
* Row row = bigtableDataClient.readRow(tableId, "key", filter);
* // Do something with row, for example, display all cells
* if(row != null) {
* System.out.println(row.getKey().toStringUtf8());
* for(RowCell cell : row.getCells()) {
* System.out.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(),
* cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
* }
* }
* } catch(ApiException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public Row readRow(String tableId, String rowKey, @Nullable Filter filter) {
return ApiExceptions.callAndTranslateApiException(
readRowAsync(tableId, ByteString.copyFromUtf8(rowKey), filter));
}

/**
* Convenience method for synchronously reading a single row. If the row does not exist, the value
* will be null.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE]";
*
* // Build the filter expression
* Filter filter = FILTERS.chain()
* .filter(FILTERS.qualifier().regex("prefix.*"))
* .filter(FILTERS.limit().cellsPerRow(10));
*
* Row row = bigtableDataClient.readRow(tableId, ByteString.copyFromUtf8("key"), filter);
* // Do something with row, for example, display all cells
* if(row != null) {
* System.out.println(row.getKey().toStringUtf8());
* for(RowCell cell : row.getCells()) {
* System.out.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(),
* cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
* }
* }
* } catch(ApiException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public Row readRow(String tableId, ByteString rowKey, @Nullable Filter filter) {
return ApiExceptions.callAndTranslateApiException(readRowAsync(tableId, rowKey, filter));
}

/**
Expand Down Expand Up @@ -228,7 +306,7 @@ public Row readRow(String tableId, String rowKey) {
* }</pre>
*/
public ApiFuture<Row> readRowAsync(String tableId, String rowKey) {
return readRowAsync(tableId, ByteString.copyFromUtf8(rowKey));
return readRowAsync(tableId, ByteString.copyFromUtf8(rowKey), null);
}

/**
Expand Down Expand Up @@ -262,11 +340,93 @@ public ApiFuture<Row> readRowAsync(String tableId, String rowKey) {
* }</pre>
*/
public ApiFuture<Row> readRowAsync(String tableId, ByteString rowKey) {
return readRowsCallable().first().futureCall(Query.create(tableId).rowKey(rowKey));
return readRowAsync(tableId, rowKey, null);
}

/**
* Convenience method for asynchronously reading a single row. If the row does not exist, the
* future's value will be null.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE]";
*
* // Build the filter expression
* Filters.Filter filter = FILTERS.chain()
* .filter(FILTERS.qualifier().regex("prefix.*"))
* .filter(FILTERS.limit().cellsPerRow(10));
*
* ApiFuture<Row> futureResult = bigtableDataClient.readRowAsync(tableId, "key", filter);
*
* ApiFutures.addCallback(futureResult, new ApiFutureCallback<Row>() {
* public void onFailure(Throwable t) {
* if (t instanceof NotFoundException) {
* System.out.println("Tried to read a non-existent table");
* } else {
* t.printStackTrace();
* }
* }
* public void onSuccess(Row row) {
* if (result != null) {
* System.out.println("Got row: " + result);
* }
* }
* }, MoreExecutors.directExecutor());
* }
* }</pre>
*/
public ApiFuture<Row> readRowAsync(String tableId, String rowKey, @Nullable Filter filter) {
return readRowAsync(tableId, ByteString.copyFromUtf8(rowKey), filter);
}

/**
* Convenience method for asynchronously reading a single row. If the row does not exist, the
* future's value will be null.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE]";
*
* // Build the filter expression
* Filters.Filter filter = FILTERS.chain()
* .filter(FILTERS.qualifier().regex("prefix.*"))
* .filter(FILTERS.limit().cellsPerRow(10));
*
* ApiFuture<Row> futureResult = bigtableDataClient.readRowAsync(tableId, ByteString.copyFromUtf8("key"), filter);
*
* ApiFutures.addCallback(futureResult, new ApiFutureCallback<Row>() {
* public void onFailure(Throwable t) {
* if (t instanceof NotFoundException) {
* System.out.println("Tried to read a non-existent table");
* } else {
* t.printStackTrace();
* }
* }
* public void onSuccess(Row row) {
* if (result != null) {
* System.out.println("Got row: " + result);
* }
* }
* }, MoreExecutors.directExecutor());
* }
* }</pre>
*/
public ApiFuture<Row> readRowAsync(String tableId, ByteString rowKey, @Nullable Filter filter) {
Query query = Query.create(tableId).rowKey(rowKey);
if (filter != null) {
query = query.filter(filter);
}
return readRowsCallable().first().futureCall(query);
}

/**
* Convenience method for synchronous streaming the results of a {@link Query}.
* Convenience method for synchronously streaming the results of a {@link Query}.
*
* <p>Sample code:
*
Expand Down Expand Up @@ -304,7 +464,7 @@ public ServerStream<Row> readRows(Query query) {
}

/**
* Convenience method for asynchronous streaming the results of a {@link Query}.
* Convenience method for asynchronously streaming the results of a {@link Query}.
*
* <p>Sample code:
*
Expand Down
Loading

0 comments on commit bf26f86

Please sign in to comment.