Lettura di un intervallo di righe

Le righe con un intervallo specificato.

Per saperne di più

Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:

Esempio di codice

C++

Per scoprire come installare e utilizzare la libreria client per Bigtable, consulta Librerie client di Bigtable.

Per eseguire l'autenticazione in Bigtable, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
[](cbt::Table table) {
  // 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) throw std::move(row).status();
    PrintRow(*row);
  }
}

C#

Per scoprire come installare e utilizzare la libreria client per Bigtable, consulta Librerie client di Bigtable.

Per eseguire l'autenticazione in Bigtable, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.


/// <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>

public string readRowRange(string projectId = "YOUR-PROJECT-ID", string instanceId = "YOUR-INSTANCE-ID", string tableId = "YOUR-TABLE-ID")
{
    String start = "phone#4c410523#20190501";
    String end = "phone#4c410523#201906201";

    BigtableClient bigtableClient = BigtableClient.Create();
    TableName tableName = new TableName(projectId, instanceId, tableId);
    RowSet rowSet = RowSet.FromRowRanges(RowRange.ClosedOpen(start, end));
    ReadRowsStream readRowsStream = bigtableClient.ReadRows(tableName, rowSet);

    string result = "";
    readRowsStream.ForEach(row => result += printRow(row));

    return result;
}

Go

Per scoprire come installare e utilizzare la libreria client per Bigtable, consulta Librerie client di Bigtable.

Per eseguire l'autenticazione in Bigtable, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

func readRowRange(w io.Writer, projectID, instanceID string, tableName string) error {
	// projectID := "my-project-id"
	// instanceID := "my-instance-id"
	// tableName := "mobile-time-series"

	ctx := context.Background()
	client, err := bigtable.NewClient(ctx, projectID, instanceID)
	if err != nil {
		return fmt.Errorf("bigtable.NewClient: %w", err)
	}
	defer client.Close()

	tbl := client.Open(tableName)
	err = tbl.ReadRows(ctx, bigtable.NewRange("phone#4c410523#20190501", "phone#4c410523#201906201"),
		func(row bigtable.Row) bool {
			printRow(w, row)
			return true
		},
	)

	if err != nil {
		return fmt.Errorf("tbl.ReadRows: %w", err)
	}

	return nil
}

Java

Per scoprire come installare e utilizzare la libreria client per Bigtable, consulta Librerie client di Bigtable.

Per eseguire l'autenticazione in Bigtable, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

public static void readRowRange() {
  // TODO(developer): Replace these variables before running the sample.
  String projectId = "my-project-id";
  String instanceId = "my-instance-id";
  String tableId = "mobile-time-series";
  readRowRange(projectId, instanceId, tableId);
}

public static void readRowRange(String projectId, String instanceId, String tableId) {
  String start = "phone#4c410523#20190501";
  String end = "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 (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
    Query query = Query.create(TableId.of(tableId)).range(start, end);
    ServerStream<Row> rows = dataClient.readRows(query);
    for (Row row : rows) {
      printRow(row);
    }
  } catch (IOException e) {
    System.out.println(
        "Unable to initialize service client, as a network error occurred: \n" + e.toString());
  }
}

Node.js

Per scoprire come installare e utilizzare la libreria client per Bigtable, consulta Librerie client di Bigtable.

Per eseguire l'autenticazione in Bigtable, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

const start = 'phone#4c410523#20190501';
const end = 'phone#4c410523#201906201';

await table
  .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

Per scoprire come installare e utilizzare la libreria client per Bigtable, consulta Librerie client di Bigtable.

Per eseguire l'autenticazione in Bigtable, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

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);
    }
}

Python

Per scoprire come installare e utilizzare la libreria client per Bigtable, consulta Librerie client di Bigtable.

Per eseguire l'autenticazione in Bigtable, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

def read_row_range(project_id, instance_id, table_id):
    client = 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)
    for row in rows:
        print_row(row)

Ruby

Per scoprire come installare e utilizzare la libreria client per Bigtable, consulta Librerie client di Bigtable.

Per eseguire l'autenticazione in Bigtable, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

# instance_id = "my-instance"
# table_id    = "my-table"
bigtable = Google::Cloud::Bigtable.new
table = bigtable.table instance_id, table_id

range = table.new_row_range.between "phone#4c410523#20190501", "phone#4c410523#201906201"
table.read_rows(ranges: range).each do |row|
  print_row row
end

Passaggi successivi

Per cercare e filtrare esempi di codice per altri prodotti Google Cloud, consulta il browser di esempio Google Cloud.