Read examples
Cloud Bigtable client libraries provide the ability to read datafrom a table. This page provides examples of each type of read request inmultiple languages. For an overview, seeReads.
The Python client library for Bigtable offers two APIs,asyncioand a synchronous API. If your application is asynchronous, useasyncio.
Required roles
To get the permissions that you need to read data from a table, ask your administrator to grant you theBigtable reader (roles/bigtable.reader) IAM role on the table. For more information about granting roles, seeManage access to projects, folders, and organizations.
You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
Read a single row
The following code samples show how to get asingle row of data using therow key.
Go
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
funcreadRow(wio.Writer,projectID,instanceIDstring,tableNamestring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"// tableName := "mobile-time-series"ctx:=context.Background()client,err:=bigtable.NewClient(ctx,projectID,instanceID)iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}deferclient.Close()tbl:=client.Open(tableName)rowKey:="phone#4c410523#20190501"row,err:=tbl.ReadRow(ctx,rowKey)iferr!=nil{returnfmt.Errorf("could not read row with key %s: %w",rowKey,err)}printRow(w,row)returnnil}HBase
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/** * Example of reading an individual row key. */publicstaticvoidreadRow(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRow(projectId,instanceId,tableId);}publicstaticvoidreadRow(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Tabletable=connection.getTable(TableName.valueOf(tableId));byte[]rowkey=Bytes.toBytes("phone#4c410523#20190501");Resultrow=table.get(newGet(rowkey));printRow(row);}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Java
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
publicstaticvoidreadRow(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRow(projectId,instanceId,tableId);}publicstaticvoidreadRow(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(BigtableDataClientdataClient=BigtableDataClient.create(projectId,instanceId)){Stringrowkey="phone#4c410523#20190501";Rowrow=dataClient.readRow(TableId.of(tableId),rowkey);printRow(row);}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Python asyncio
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
fromgoogle.cloud.bigtable.dataimportBigtableDataClientAsyncasyncdefread_row(project_id,instance_id,table_id):asyncwithBigtableDataClientAsync(project=project_id)asclient:asyncwithclient.get_table(instance_id,table_id)astable:row_key="phone#4c410523#20190501"row=awaittable.read_row(row_key)print(row)Python
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
C#
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/// <summary>/// /// Reads one row from an existing table.///</summary>/// <param name="projectId">Your Google Cloud Project ID.</param>/// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>/// <param name="tableId">Your Google Cloud Bigtable table ID.</param>publicstringReadRow(stringprojectId="YOUR-PROJECT-ID",stringinstanceId="YOUR-INSTANCE-ID",stringtableId="YOUR-TABLE-ID"){BigtableClientbigtableClient=BigtableClient.Create();TableNametableName=newTableName(projectId,instanceId,tableId);Rowrow=bigtableClient.ReadRow(tableName,rowKey:"phone#4c410523#20190501");returnPrintRow(row);}C++
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
namespacecbt=::google::cloud::bigtable;using::google::cloud::StatusOr;[](google::cloud::bigtable::Tabletable,std::stringconst&row_key){StatusOr<std::pair<bool,cbt::Row>>tuple=table.ReadRow(row_key,cbt::Filter::PassAllFilter());if(!tuple)throwstd::move(tuple).status();if(!tuple->first){std::cout <<"Row " <<row_key <<" not found\n";return;}PrintRow(tuple->second);}Node.js
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
constrowkey='phone#4c410523#20190501';const[row]=awaittable.row(rowkey).get();printRow(rowkey,row.data);PHP
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
use Google\Cloud\Bigtable\BigtableClient;/** * Read a row using the row key * * @param string $projectId The Google Cloud project ID * @param string $instanceId The ID of the Bigtable instance * @param string $tableId The ID of the table to read from */function read_row( string $projectId, string $instanceId, string $tableId): void { // Connect to an existing table with an existing instance. $dataClient = new BigtableClient([ 'projectId' => $projectId, ]); $table = $dataClient->table($instanceId, $tableId); $rowkey = 'phone#4c410523#20190501'; $row = $table->readRow($rowkey); print_row($rowkey, $row);}Ruby
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
Read a partial row
The following code samples show how to getspecific columns from a row.
Go
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
funcreadRowPartial(wio.Writer,projectID,instanceIDstring,tableNamestring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"// tableName := "mobile-time-series"ctx:=context.Background()client,err:=bigtable.NewClient(ctx,projectID,instanceID)iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}deferclient.Close()tbl:=client.Open(tableName)rowKey:="phone#4c410523#20190501"row,err:=tbl.ReadRow(ctx,rowKey,bigtable.RowFilter(bigtable.ColumnFilter("os_build")))iferr!=nil{returnfmt.Errorf("could not read row with key %s: %w",rowKey,err)}printRow(w,row)returnnil}HBase
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/** * Example of reading a subset of the columns for a single row. */publicstaticvoidreadRowPartial(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRowPartial(projectId,instanceId,tableId);}publicstaticvoidreadRowPartial(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Tabletable=connection.getTable(TableName.valueOf(tableId));byte[]rowkey=Bytes.toBytes("phone#4c410523#20190501");Resultrow=table.get(newGet(rowkey).addColumn(Bytes.toBytes("stats_summary"),Bytes.toBytes("os_build")));printRow(row);}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Java
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
publicstaticvoidreadRowPartial(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRowPartial(projectId,instanceId,tableId);}publicstaticvoidreadRowPartial(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(BigtableDataClientdataClient=BigtableDataClient.create(projectId,instanceId)){Stringrowkey="phone#4c410523#20190501";Filters.Filterfilter=FILTERS.chain().filter(FILTERS.family().exactMatch("stats_summary")).filter(FILTERS.qualifier().exactMatch("os_build"));Rowrow=dataClient.readRow(TableId.of(tableId),rowkey,filter);printRow(row);}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Python asyncio
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
fromgoogle.cloud.bigtable.dataimportBigtableDataClientAsyncfromgoogle.cloud.bigtable.dataimportrow_filtersasyncdefread_row_partial(project_id,instance_id,table_id):asyncwithBigtableDataClientAsync(project=project_id)asclient:asyncwithclient.get_table(instance_id,table_id)astable:row_key="phone#4c410523#20190501"col_filter=row_filters.ColumnQualifierRegexFilter(b"os_build")row=awaittable.read_row(row_key,row_filter=col_filter)print(row)Python
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
defread_row_partial(project_id,instance_id,table_id):fromgoogle.cloudimportbigtablefromgoogle.cloud.bigtableimportrow_filtersclient=bigtable.Client(project=project_id,admin=True)instance=client.instance(instance_id)table=instance.table(table_id)row_key="phone#4c410523#20190501"col_filter=row_filters.ColumnQualifierRegexFilter(b"os_build")row=table.read_row(row_key,filter_=col_filter)print_row(row)C#
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/// <summary>/// /// Reads part of one row from an existing table.///</summary>/// <param name="projectId">Your Google Cloud Project ID.</param>/// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>/// <param name="tableId">Your Google Cloud Bigtable table ID.</param>publicstringReadRowPartial(stringprojectId="YOUR-PROJECT-ID",stringinstanceId="YOUR-INSTANCE-ID",stringtableId="YOUR-TABLE-ID"){BigtableClientbigtableClient=BigtableClient.Create();TableNametableName=newTableName(projectId,instanceId,tableId);stringrowkey="phone#4c410523#20190501";RowFilterfilter=RowFilters.ColumnQualifierExact("os_build");Rowrow=bigtableClient.ReadRow(tableName,rowkey,filter);returnPrintRow(row);}C++
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
namespacecbt=::google::cloud::bigtable;using::google::cloud::StatusOr;[](google::cloud::bigtable::Tabletable,std::stringconst&row_key){StatusOr<std::pair<bool,cbt::Row>>tuple=table.ReadRow(row_key,cbt::Filter::ColumnName("stats_summary","os_build"));if(!tuple)throwstd::move(tuple).status();if(!tuple->first){std::cout <<"Row " <<row_key <<" not found\n";return;}PrintRow(tuple->second);}Node.js
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
constCOLUMN_FAMILY='stats_summary';constCOLUMN_QUALIFIER='os_build';constrowkey='phone#4c410523#20190501';const[row]=awaittable.row(rowkey).get([`${COLUMN_FAMILY}:${COLUMN_QUALIFIER}`]);printRow(rowkey,row);PHP
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
use Google\Cloud\Bigtable\BigtableClient;use Google\Cloud\Bigtable\Filter;/** * Read partial row data * * @param string $projectId The Google Cloud project ID * @param string $instanceId The ID of the Bigtable instance * @param string $tableId The ID of the table to read from */function read_row_partial( string $projectId, string $instanceId, string $tableId): void { // Connect to an existing table with an existing instance. $dataClient = new BigtableClient([ 'projectId' => $projectId, ]); $table = $dataClient->table($instanceId, $tableId); $rowkey = 'phone#4c410523#20190501'; $rowFilter = Filter::qualifier()->regex('os_build'); $row = $table->readRow($rowkey, ['filter' => $rowFilter]); print_row($rowkey, $row);}Ruby
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
Read multiple rows
The following code samples show how to getmultiple rows of data using a setof row keys.
Go
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
funcreadRows(wio.Writer,projectID,instanceIDstring,tableNamestring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"// tableName := "mobile-time-series"ctx:=context.Background()client,err:=bigtable.NewClient(ctx,projectID,instanceID)iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}deferclient.Close()tbl:=client.Open(tableName)err=tbl.ReadRows(ctx,bigtable.RowList{"phone#4c410523#20190501","phone#4c410523#20190502"},func(rowbigtable.Row)bool{printRow(w,row)returntrue},)iferr!=nil{returnfmt.Errorf("tbl.ReadRows: %w",err)}returnnil}HBase
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/** * Example of reading multiple row keys. */publicstaticvoidreadRows(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRows(projectId,instanceId,tableId);}publicstaticvoidreadRows(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Tabletable=connection.getTable(TableName.valueOf(tableId));List<Get>queryRowList=newArrayList<Get>();queryRowList.add(newGet(Bytes.toBytes("phone#4c410523#20190501")));queryRowList.add(newGet(Bytes.toBytes("phone#4c410523#20190502")));Result[]rows=table.get(queryRowList);for(Resultrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Java
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
publicstaticvoidreadRows(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRows(projectId,instanceId,tableId);}publicstaticvoidreadRows(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(BigtableDataClientdataClient=BigtableDataClient.create(projectId,instanceId)){Queryquery=Query.create(TableId.of(tableId)).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502");ServerStream<Row>rows=dataClient.readRows(query);for(Rowrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Python asyncio
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
fromgoogle.cloud.bigtable.dataimportBigtableDataClientAsyncfromgoogle.cloud.bigtable.dataimportReadRowsQueryasyncdefread_rows(project_id,instance_id,table_id):asyncwithBigtableDataClientAsync(project=project_id)asclient:asyncwithclient.get_table(instance_id,table_id)astable:query=ReadRowsQuery(row_keys=[b"phone#4c410523#20190501",b"phone#4c410523#20190502"])asyncforrowinawaittable.read_rows_stream(query):print(row)Python
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
defread_rows(project_id,instance_id,table_id):fromgoogle.cloudimportbigtablefromgoogle.cloud.bigtable.row_setimportRowSetclient=bigtable.Client(project=project_id,admin=True)instance=client.instance(instance_id)table=instance.table(table_id)row_set=RowSet()row_set.add_row_key(b"phone#4c410523#20190501")row_set.add_row_key(b"phone#4c410523#20190502")rows=table.read_rows(row_set=row_set)forrowinrows:print_row(row)C#
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/// <summary>/// /// Reads multiple rows from an existing table.///</summary>/// <param name="projectId">Your Google Cloud Project ID.</param>/// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>/// <param name="tableId">Your Google Cloud Bigtable table ID.</param>publicasyncTask<string>ReadRows(stringprojectId="YOUR-PROJECT-ID",stringinstanceId="YOUR-INSTANCE-ID",stringtableId="YOUR-TABLE-ID"){BigtableClientbigtableClient=BigtableClient.Create();TableNametableName=newTableName(projectId,instanceId,tableId);RowSetrowSet=RowSet.FromRowKeys("phone#4c410523#20190501","phone#4c410523#20190502");ReadRowsStreamreadRowsStream=bigtableClient.ReadRows(tableName,rowSet);stringresult="";awaitforeach(varrowinreadRowsStream){result+=PrintRow(row);}returnresult;}C++
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
namespacecbt=::google::cloud::bigtable;using::google::cloud::StatusOr;[](cbt::Tabletable){// Read and print the rows.for(StatusOr<cbt::Row>&row:table.ReadRows(cbt::RowSet("phone#4c410523#20190501","phone#4c410523#20190502"),cbt::Filter::PassAllFilter())){if(!row)throwstd::move(row).status();PrintRow(*row);}}Node.js
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
constrowKeys=['phone#4c410523#20190501','phone#4c410523#20190502'];const[rows]=awaittable.getRows({keys:rowKeys});rows.forEach(row=>printRow(row.id,row.data));PHP
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
use Google\Cloud\Bigtable\BigtableClient;/** * Read rows using an array of keys * * @param string $projectId The Google Cloud project ID * @param string $instanceId The ID of the Bigtable instance * @param string $tableId The ID of the table to read from */function read_rows( string $projectId, string $instanceId, string $tableId): void { // Connect to an existing table with an existing instance. $dataClient = new BigtableClient([ 'projectId' => $projectId, ]); $table = $dataClient->table($instanceId, $tableId); $rows = $table->readRows( ['rowKeys' => ['phone#4c410523#20190501', 'phone#4c410523#20190502']] ); foreach ($rows as $key => $row) { print_row($key, $row); }}Ruby
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
Read a range of rows
The following code samples show how to getmultiple rows of data using astart key and an end key.
Go
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
funcreadRowRange(wio.Writer,projectID,instanceIDstring,tableNamestring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"// tableName := "mobile-time-series"ctx:=context.Background()client,err:=bigtable.NewClient(ctx,projectID,instanceID)iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}deferclient.Close()tbl:=client.Open(tableName)err=tbl.ReadRows(ctx,bigtable.NewRange("phone#4c410523#20190501","phone#4c410523#201906201"),func(rowbigtable.Row)bool{printRow(w,row)returntrue},)iferr!=nil{returnfmt.Errorf("tbl.ReadRows: %w",err)}returnnil}HBase
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/** * Example of reading a range of rows using a key range. */publicstaticvoidreadRowRange(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRowRange(projectId,instanceId,tableId);}publicstaticvoidreadRowRange(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Tabletable=connection.getTable(TableName.valueOf(tableId));ScanrangeQuery=newScan().withStartRow(Bytes.toBytes("phone#4c410523#20190501")).withStopRow(Bytes.toBytes("phone#4c410523#201906201"));ResultScannerrows=table.getScanner(rangeQuery);for(Resultrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Java
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
publicstaticvoidreadRowRange(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRowRange(projectId,instanceId,tableId);}publicstaticvoidreadRowRange(StringprojectId,StringinstanceId,StringtableId){Stringstart="phone#4c410523#20190501";Stringend="phone#4c410523#201906201";// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(BigtableDataClientdataClient=BigtableDataClient.create(projectId,instanceId)){Queryquery=Query.create(TableId.of(tableId)).range(start,end);ServerStream<Row>rows=dataClient.readRows(query);for(Rowrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Python asyncio
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
fromgoogle.cloud.bigtable.dataimportBigtableDataClientAsyncfromgoogle.cloud.bigtable.dataimportReadRowsQueryfromgoogle.cloud.bigtable.dataimportRowRangeasyncdefread_row_range(project_id,instance_id,table_id):asyncwithBigtableDataClientAsync(project=project_id)asclient:asyncwithclient.get_table(instance_id,table_id)astable:row_range=RowRange(start_key=b"phone#4c410523#20190501",end_key=b"phone#4c410523#201906201",)query=ReadRowsQuery(row_ranges=[row_range])asyncforrowinawaittable.read_rows_stream(query):print(row)Python
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
defread_row_range(project_id,instance_id,table_id):fromgoogle.cloudimportbigtablefromgoogle.cloud.bigtable.row_setimportRowSetclient=bigtable.Client(project=project_id,admin=True)instance=client.instance(instance_id)table=instance.table(table_id)row_set=RowSet()row_set.add_row_range_from_keys(start_key=b"phone#4c410523#20190501",end_key=b"phone#4c410523#201906201")rows=table.read_rows(row_set=row_set)forrowinrows:print_row(row)C#
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/// <summary>/// /// Reads a range of rows from an existing table.///</summary>/// <param name="projectId">Your Google Cloud Project ID.</param>/// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>/// <param name="tableId">Your Google Cloud Bigtable table ID.</param>publicasyncTask<string>ReadRowRange(stringprojectId="YOUR-PROJECT-ID",stringinstanceId="YOUR-INSTANCE-ID",stringtableId="YOUR-TABLE-ID"){stringstart="phone#4c410523#20190501";stringend="phone#4c410523#201906201";BigtableClientbigtableClient=BigtableClient.Create();TableNametableName=newTableName(projectId,instanceId,tableId);RowSetrowSet=RowSet.FromRowRanges(RowRange.ClosedOpen(start,end));ReadRowsStreamreadRowsStream=bigtableClient.ReadRows(tableName,rowSet);stringresult="";awaitforeach(varrowinreadRowsStream){result+=PrintRow(row);}returnresult;}C++
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
namespacecbt=::google::cloud::bigtable;using::google::cloud::StatusOr;[](cbt::Tabletable){// Read and print the rows.for(StatusOr<cbt::Row>&row:table.ReadRows(cbt::RowRange::Range("phone#4c410523#20190501","phone#4c410523#201906201"),cbt::Filter::PassAllFilter())){if(!row)throwstd::move(row).status();PrintRow(*row);}}Node.js
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
conststart='phone#4c410523#20190501';constend='phone#4c410523#201906201';awaittable.createReadStream({start,end,}).on('error',err=>{// Handle the error.console.log(err);}).on('data',row=>printRow(row.id,row.data)).on('end',()=>{// All rows retrieved.});PHP
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
use Google\Cloud\Bigtable\BigtableClient;/** * Read using a range for row keys * * @param string $projectId The Google Cloud project ID * @param string $instanceId The ID of the Bigtable instance * @param string $tableId The ID of the table to read from */function read_row_range( string $projectId, string $instanceId, string $tableId): void { // Connect to an existing table with an existing instance. $dataClient = new BigtableClient([ 'projectId' => $projectId, ]); $table = $dataClient->table($instanceId, $tableId); $rows = $table->readRows([ 'rowRanges' => [ [ 'startKeyClosed' => 'phone#4c410523#20190501', 'endKeyOpen' => 'phone#4c410523#201906201' ] ] ]); foreach ($rows as $key => $row) { print_row($key, $row); }}Ruby
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
# instance_id = "my-instance"# table_id = "my-table"bigtable=Google::Cloud::Bigtable.newtable=bigtable.tableinstance_id,table_idrange=table.new_row_range.between"phone#4c410523#20190501","phone#4c410523#201906201"table.read_rows(ranges:range).eachdo|row|print_rowrowendRead multiple ranges of rows
The following code samples show how to getmultiple rows of data usingmultiple start key and end keys. Be aware that requesting a large number ofrow ranges in a single request can have an impact onread performance.
Go
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
funcreadRowRanges(wio.Writer,projectID,instanceIDstring,tableNamestring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"// tableName := "mobile-time-series"ctx:=context.Background()client,err:=bigtable.NewClient(ctx,projectID,instanceID)iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}deferclient.Close()tbl:=client.Open(tableName)err=tbl.ReadRows(ctx,bigtable.RowRangeList{bigtable.NewRange("phone#4c410523#20190501","phone#4c410523#201906201"),bigtable.NewRange("phone#5c10102#20190501","phone#5c10102#201906201"),},func(rowbigtable.Row)bool{printRow(w,row)returntrue},)iferr!=nil{returnfmt.Errorf("tbl.ReadRows: %w",err)}returnnil}HBase
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/** * Example of reading multiple disjoint row ranges. */publicstaticvoidreadRowRanges(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRowRanges(projectId,instanceId,tableId);}publicstaticvoidreadRowRanges(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Tabletable=connection.getTable(TableName.valueOf(tableId));List<RowRange>ranges=newArrayList<>();ranges.add(newRowRange(Bytes.toBytes("phone#4c410523#20190501"),true,Bytes.toBytes("phone#4c410523#20190601"),false));ranges.add(newRowRange(Bytes.toBytes("phone#5c10102#20190501"),true,Bytes.toBytes("phone#5c10102#20190601"),false));Filterfilter=newMultiRowRangeFilter(ranges);Scanscan=newScan().setFilter(filter);ResultScannerrows=table.getScanner(scan);for(Resultrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Java
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
publicstaticvoidreadRowRanges(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRowRanges(projectId,instanceId,tableId);}publicstaticvoidreadRowRanges(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(BigtableDataClientdataClient=BigtableDataClient.create(projectId,instanceId)){Queryquery=Query.create(TableId.of(tableId)).range("phone#4c410523#20190501","phone#4c410523#20190601").range("phone#5c10102#20190501","phone#5c10102#20190601");ServerStream<Row>rows=dataClient.readRows(query);for(Rowrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Python
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
defread_row_ranges(project_id,instance_id,table_id):fromgoogle.cloudimportbigtablefromgoogle.cloud.bigtable.row_setimportRowSetclient=bigtable.Client(project=project_id,admin=True)instance=client.instance(instance_id)table=instance.table(table_id)row_set=RowSet()row_set.add_row_range_from_keys(start_key=b"phone#4c410523#20190501",end_key=b"phone#4c410523#201906201")row_set.add_row_range_from_keys(start_key=b"phone#5c10102#20190501",end_key=b"phone#5c10102#201906201")rows=table.read_rows(row_set=row_set)forrowinrows:print_row(row)C#
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/// <summary>/// /// Reads multiple ranges of rows from an existing table.///</summary>/// <param name="projectId">Your Google Cloud Project ID.</param>/// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>/// <param name="tableId">Your Google Cloud Bigtable table ID.</param>publicasyncTask<string>ReadRowRanges(stringprojectId="YOUR-PROJECT-ID",stringinstanceId="YOUR-INSTANCE-ID",stringtableId="YOUR-TABLE-ID"){BigtableClientbigtableClient=BigtableClient.Create();TableNametableName=newTableName(projectId,instanceId,tableId);RowSetrowSet=RowSet.FromRowRanges(RowRange.ClosedOpen("phone#4c410523#20190501","phone#4c410523#20190601"),RowRange.ClosedOpen("phone#5c10102#20190501","phone#5c10102#20190601"));ReadRowsStreamreadRowsStream=bigtableClient.ReadRows(tableName,rowSet);stringresult="";awaitforeach(varrowinreadRowsStream){result+=PrintRow(row);}returnresult;}C++
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
namespacecbt=::google::cloud::bigtable;using::google::cloud::StatusOr;[](cbt::Tabletable){// Read and print the rows.for(StatusOr<cbt::Row>&row:table.ReadRows(cbt::RowSet({cbt::RowRange::Range("phone#4c410523#20190501","phone#4c410523#20190601"),cbt::RowRange::Range("phone#5c10102#20190501","phone#5c10102#20190601")}),cbt::Filter::PassAllFilter())){if(!row)throwstd::move(row).status();PrintRow(*row);}}Node.js
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
awaittable.createReadStream({ranges:[{start:'phone#4c410523#20190501',end:'phone#4c410523#20190601',},{start:'phone#5c10102#20190501',end:'phone#5c10102#20190601',},],}).on('error',err=>{// Handle the error.console.log(err);}).on('data',row=>printRow(row.id,row.data)).on('end',()=>{// All rows retrieved.});PHP
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
use Google\Cloud\Bigtable\BigtableClient;/** * Read using multiple ranges for row keys * * @param string $projectId The Google Cloud project ID * @param string $instanceId The ID of the Bigtable instance * @param string $tableId The ID of the table to read from */function read_row_ranges( string $projectId, string $instanceId, string $tableId): void { // Connect to an existing table with an existing instance. $dataClient = new BigtableClient([ 'projectId' => $projectId, ]); $table = $dataClient->table($instanceId, $tableId); $rows = $table->readRows([ 'rowRanges' => [ [ 'startKeyClosed' => 'phone#4c410523#20190501', 'endKeyOpen' => 'phone#4c410523#201906201' ], [ 'startKeyClosed' => 'phone#5c10102#20190501', 'endKeyOpen' => 'phone#5c10102#201906201' ] ] ]); foreach ($rows as $key => $row) { print_row($key, $row); }}Ruby
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
# instance_id = "my-instance"# table_id = "my-table"bigtable=Google::Cloud::Bigtable.newtable=bigtable.tableinstance_id,table_idranges=[]ranges<<table.new_row_range.between("phone#4c410523#20190501","phone#4c410523#201906201")<<table.new_row_range.between("phone#5c10102#20190501","phone#5c10102#201906201")table.read_rows(ranges:ranges).eachdo|row|print_rowrowendRead multiple rows using a row key prefix
The following code samples show how to getmultiple rows of data using arow key prefix.
Go
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
funcreadPrefix(wio.Writer,projectID,instanceIDstring,tableNamestring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"// tableName := "mobile-time-series"ctx:=context.Background()client,err:=bigtable.NewClient(ctx,projectID,instanceID)iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}deferclient.Close()tbl:=client.Open(tableName)err=tbl.ReadRows(ctx,bigtable.PrefixRange("phone#"),func(rowbigtable.Row)bool{printRow(w,row)returntrue},)iferr!=nil{returnfmt.Errorf("tbl.ReadRows: %w",err)}returnnil}HBase
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/** * Example of reading a range of rows using a row prefix. */publicstaticvoidreadPrefix(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readPrefix(projectId,instanceId,tableId);}publicstaticvoidreadPrefix(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Tabletable=connection.getTable(TableName.valueOf(tableId));ScanprefixScan=newScan().setRowPrefixFilter(Bytes.toBytes("phone"));ResultScannerrows=table.getScanner(prefixScan);for(Resultrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Java
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
publicstaticvoidreadPrefix(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readPrefix(projectId,instanceId,tableId);}publicstaticvoidreadPrefix(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(BigtableDataClientdataClient=BigtableDataClient.create(projectId,instanceId)){Queryquery=Query.create(TableId.of(tableId)).prefix("phone");ServerStream<Row>rows=dataClient.readRows(query);for(Rowrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Python asyncio
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
fromgoogle.cloud.bigtable.dataimportBigtableDataClientAsyncfromgoogle.cloud.bigtable.dataimportReadRowsQueryfromgoogle.cloud.bigtable.dataimportRowRangeasyncdefread_prefix(project_id,instance_id,table_id):asyncwithBigtableDataClientAsync(project=project_id)asclient:asyncwithclient.get_table(instance_id,table_id)astable:prefix="phone#"end_key=prefix[:-1]+chr(ord(prefix[-1])+1)prefix_range=RowRange(start_key=prefix,end_key=end_key)query=ReadRowsQuery(row_ranges=[prefix_range])asyncforrowinawaittable.read_rows_stream(query):print(row)Python
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
defread_prefix(project_id,instance_id,table_id):fromgoogle.cloudimportbigtablefromgoogle.cloud.bigtable.row_setimportRowSetclient=bigtable.Client(project=project_id,admin=True)instance=client.instance(instance_id)table=instance.table(table_id)prefix="phone#"end_key=prefix[:-1]+chr(ord(prefix[-1])+1)row_set=RowSet()row_set.add_row_range_from_keys(prefix.encode("utf-8"),end_key.encode("utf-8"))rows=table.read_rows(row_set=row_set)forrowinrows:print_row(row)C#
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/// <summary>/// /// Reads rows starting with a prefix from an existing table.///</summary>/// <param name="projectId">Your Google Cloud Project ID.</param>/// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>/// <param name="tableId">Your Google Cloud Bigtable table ID.</param>publicasyncTask<string>ReadPrefix(stringprojectId="YOUR-PROJECT-ID",stringinstanceId="YOUR-INSTANCE-ID",stringtableId="YOUR-TABLE-ID"){BigtableClientbigtableClient=BigtableClient.Create();TableNametableName=newTableName(projectId,instanceId,tableId);stringprefix="phone";charprefixEndChar=prefix[prefix.Length-1];prefixEndChar++;stringend=prefix.Substring(0,prefix.Length-1)+prefixEndChar;RowSetrowSet=RowSet.FromRowRanges(RowRange.Closed(prefix,end));ReadRowsStreamreadRowsStream=bigtableClient.ReadRows(tableName,rowSet);stringresult="";awaitforeach(varrowinreadRowsStream){result+=PrintRow(row);}returnresult;}C++
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
namespacecbt=::google::cloud::bigtable;using::google::cloud::StatusOr;[](cbt::Tabletable){// Read and print the rows.for(StatusOr<cbt::Row>&row:table.ReadRows(cbt::RowRange::Prefix("phone"),cbt::Filter::PassAllFilter())){if(!row)throwstd::move(row).status();PrintRow(*row);}}Node.js
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
constprefix='phone#';awaittable.createReadStream({prefix,}).on('error',err=>{// Handle the error.console.log(err);}).on('data',row=>printRow(row.id,row.data)).on('end',()=>{// All rows retrieved.});PHP
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
use Google\Cloud\Bigtable\BigtableClient;/** * Read using a row key prefix * * @param string $projectId The Google Cloud project ID * @param string $instanceId The ID of the Bigtable instance * @param string $tableId The ID of the table to read from */function read_prefix( string $projectId, string $instanceId, string $tableId): void { // Connect to an existing table with an existing instance. $dataClient = new BigtableClient([ 'projectId' => $projectId, ]); $table = $dataClient->table($instanceId, $tableId); $prefix = 'phone#'; $end = $prefix; // Increment the last character of the prefix so the filter matches everything in between $end[-1] = chr( ord($end[-1]) + 1 ); $rows = $table->readRows([ 'rowRanges' => [ [ 'startKeyClosed' => $prefix, 'endKeyClosed' => $end, ] ] ]); foreach ($rows as $key => $row) { print_row($key, $row); }}Ruby
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
# instance_id = "my-instance"# table_id = "my-table"bigtable=Google::Cloud::Bigtable.newtable=bigtable.tableinstance_id,table_idprefix="phone#"end_key=prefix[0...-1]+prefix[-1].nextrange=table.new_row_range.betweenprefix,end_keytable.read_rows(ranges:range).eachdo|row|print_rowrowendScan in reverse
The following code samples show how to scan in reverse. See the details atReverse scans. To avoid inefficiency,limit a reverse scan to about 50 rows.
Go
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
funcreadRowsReversed(wio.Writer,projectID,instanceIDstring,tableNamestring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"// tableName := "mobile-time-series"ctx:=context.Background()client,err:=bigtable.NewClient(ctx,projectID,instanceID)iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}deferclient.Close()tbl:=client.Open(tableName)err=tbl.ReadRows(ctx,bigtable.NewRange("phone#5c10102","phone#5c10103"),func(rowbigtable.Row)bool{printRow(w,row)returntrue},bigtable.ReverseScan())iferr!=nil{returnfmt.Errorf("tbl.ReadRows: %w",err)}returnnil}HBase
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/** * Example of reading a range of rows in reverse order. */publicstaticvoidreadRowsReversed(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRowsReversed(projectId,instanceId,tableId);}publicstaticvoidreadRowsReversed(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Tabletable=connection.getTable(TableName.valueOf(tableId));ScanrevScan=newScan().setReversed(true).setLimit(2).withStartRow(Bytes.toBytes("phone#4c410523#20190505"));ResultScannerrows=table.getScanner(revScan);for(Resultrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Java
Because this example uses a prefixand start and end row keys, it returns aresult from the union of those two filters.
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
publicstaticvoidreadRowsReversed(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readRowsReversed(projectId,instanceId,tableId);}publicstaticvoidreadRowsReversed(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(BigtableDataClientdataClient=BigtableDataClient.create(projectId,instanceId)){Queryquery=Query.create(TableId.of(tableId)).reversed(true).limit(3).prefix("phone#4c410523").range("phone#5c10102","phone#5c10103");ServerStream<Row>rows=dataClient.readRows(query);for(Rowrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}C++
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
namespacecbt=::google::cloud::bigtable;using::google::cloud::Options;using::google::cloud::StatusOr;[](cbt::Tabletable){// Read and print the rows.autoreader=table.ReadRows(cbt::RowRange::RightOpen("phone#5c10102","phone#5c10103"),3,cbt::Filter::PassAllFilter(),Options{}.set<cbt::ReverseScanOption>(true));for(StatusOr<cbt::Row>&row:reader){if(!row)throwstd::move(row).status();PrintRow(*row);}}Read with filters
The following code samples show how to getmultiple rows of data using arow filter. To learn more about the types of filters that you can use inread requests, see theoverview of filters. Additionalcode samples showing how to implement various types of filters in multiplelanguages are also available.
Note: The HBase client for Java uses HBase's filter APIs instead of the Bigtable Data API. To learn about HBase's filter APIs, see theHBase documentation.
Go
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
funcreadFilter(wio.Writer,projectID,instanceIDstring,tableNamestring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"// tableName := "mobile-time-series"ctx:=context.Background()client,err:=bigtable.NewClient(ctx,projectID,instanceID)iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}deferclient.Close()tbl:=client.Open(tableName)err=tbl.ReadRows(ctx,bigtable.RowRange{},func(rowbigtable.Row)bool{printRow(w,row)returntrue},bigtable.RowFilter(bigtable.ValueFilter("PQ2A.*$")),)iferr!=nil{returnfmt.Errorf("tbl.ReadRows: %w",err)}returnnil}HBase
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/** * Example of filtering row contents using filters. */publicstaticvoidreadFilter(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readFilter(projectId,instanceId,tableId);}publicstaticvoidreadFilter(StringprojectId,StringinstanceId,StringtableId){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Tabletable=connection.getTable(TableName.valueOf(tableId));ValueFiltervalueFilter=newValueFilter(CompareOp.EQUAL,newRegexStringComparator("PQ2A.*"));Scanscan=newScan().setFilter(valueFilter);ResultScannerrows=table.getScanner(scan);for(Resultrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Java
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
publicstaticvoidreadFilter(){// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringinstanceId="my-instance-id";StringtableId="mobile-time-series";readFilter(projectId,instanceId,tableId);}publicstaticvoidreadFilter(StringprojectId,StringinstanceId,StringtableId){Filters.Filterfilter=FILTERS.value().regex("PQ2A.*");// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(BigtableDataClientdataClient=BigtableDataClient.create(projectId,instanceId)){Queryquery=Query.create(TableId.of(tableId)).filter(filter);ServerStream<Row>rows=dataClient.readRows(query);for(Rowrow:rows){printRow(row);}}catch(IOExceptione){System.out.println("Unable to initialize service client, as a network error occurred: \n"+e.toString());}}Python asyncio
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
fromgoogle.cloud.bigtable.dataimportBigtableDataClientAsyncfromgoogle.cloud.bigtable.dataimportReadRowsQueryfromgoogle.cloud.bigtable.dataimportrow_filtersasyncdefread_with_filter(project_id,instance_id,table_id):asyncwithBigtableDataClientAsync(project=project_id)asclient:asyncwithclient.get_table(instance_id,table_id)astable:row_filter=row_filters.ValueRegexFilter(b"PQ2A.*$")query=ReadRowsQuery(row_filter=row_filter)asyncforrowinawaittable.read_rows_stream(query):print(row)Python
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
defread_filter(project_id,instance_id,table_id):fromgoogle.cloudimportbigtablefromgoogle.cloud.bigtableimportrow_filtersclient=bigtable.Client(project=project_id,admin=True)instance=client.instance(instance_id)table=instance.table(table_id)rows=table.read_rows(filter_=row_filters.ValueRegexFilter(b"PQ2A.*$"))forrowinrows:print_row(row)C#
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
/// <summary>/// /// Reads using a filter from an existing table.///</summary>/// <param name="projectId">Your Google Cloud Project ID.</param>/// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>/// <param name="tableId">Your Google Cloud Bigtable table ID.</param>publicasyncTask<string>ReadFilter(stringprojectId="YOUR-PROJECT-ID",stringinstanceId="YOUR-INSTANCE-ID",stringtableId="YOUR-TABLE-ID"){BigtableClientbigtableClient=BigtableClient.Create();TableNametableName=newTableName(projectId,instanceId,tableId);RowFilterfilter=RowFilters.ValueRegex("PQ2A.*");ReadRowsStreamreadRowsStream=bigtableClient.ReadRows(tableName,filter:filter);stringresult="";awaitforeach(varrowinreadRowsStream){result+=PrintRow(row);}returnresult;}C++
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
namespacecbt=::google::cloud::bigtable;using::google::cloud::StatusOr;[](cbt::Tabletable){// Read and print the rows.for(StatusOr<cbt::Row>&row:table.ReadRows(cbt::RowRange::InfiniteRange(),cbt::Filter::ValueRegex("PQ2A.*"))){if(!row)throwstd::move(row).status();PrintRow(*row);}}Node.js
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
constfilter={value:/PQ2A.*$/,};awaittable.createReadStream({filter,}).on('error',err=>{// Handle the error.console.log(err);}).on('data',row=>printRow(row.id,row.data)).on('end',()=>{// All rows retrieved.});PHP
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
use Google\Cloud\Bigtable\BigtableClient;use Google\Cloud\Bigtable\Filter;/** * Read using a filter * * @param string $projectId The Google Cloud project ID * @param string $instanceId The ID of the Bigtable instance * @param string $tableId The ID of the table to read from */function read_filter( string $projectId, string $instanceId, string $tableId): void { // Connect to an existing table with an existing instance. $dataClient = new BigtableClient([ 'projectId' => $projectId, ]); $table = $dataClient->table($instanceId, $tableId); $rowFilter = Filter::value()->regex('PQ2A.*$'); $rows = $table->readRows([ 'filter' => $rowFilter ]); foreach ($rows as $key => $row) { print_row($key, $row); }}Ruby
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
Read from an authorized view
The following examples show how to send a read request to an authorized view.The syntax is similar to reading from a table except you must also provide theauthorized view ID.
Java
This example shows how to read a single row from an authorized view.
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
try{System.out.println("\nReading a single row by row key from an authorized view");Rowrow=dataClient.readRow(AuthorizedViewId.of(tableId,authorizedViewId),ROW_KEY_PREFIX+0);System.out.println("Row: "+row.getKey().toStringUtf8());for(RowCellcell:row.getCells()){System.out.printf("Family: %s Qualifier: %s Value: %s%n",cell.getFamily(),cell.getQualifier().toStringUtf8(),cell.getValue().toStringUtf8());}returnrow;}catch(NotFoundExceptione){System.err.println("Failed to read from a non-existent authorized view: "+e.getMessage());returnnull;}This example shows how to read from an authorized view with a filter.
To learn how to install and use the client library for Bigtable, seeBigtable client libraries.
To authenticate to Bigtable, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
try{// A filter that matches only the most recent cell within each columnFilterfilter=FILTERS.limit().cellsPerColumn(1);System.out.println("\nScanning authorized view with filter");Queryquery=Query.create(AuthorizedViewId.of(tableId,authorizedViewId)).filter(filter);ServerStream<Row>rowStream=dataClient.readRows(query);List<Row>authorizedViewRows=newArrayList<>();for(Rowr:rowStream){System.out.println("Row Key: "+r.getKey().toStringUtf8());authorizedViewRows.add(r);for(RowCellcell:r.getCells()){System.out.printf("Family: %s Qualifier: %s Value: %s%n",cell.getFamily(),cell.getQualifier().toStringUtf8(),cell.getValue().toStringUtf8());}}returnauthorizedViewRows;}catch(NotFoundExceptione){System.err.println("Failed to read a non-existent authorized view: "+e.getMessage());returnnull;}What's next
- Learn more about reading data.
- Explore the types of filters that you can use.
- Review code samples that show how to implement filters.
- Read about Bigtable writes.
- Use the Bigtable emulator.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.