Lire à l'aide d'un filtre conditionnel

Crée un filtre composite à l'aide d'une condition.

En savoir plus

Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :

Exemple de code

C++

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la page Bibliothèques clientes Bigtable.

Pour vous authentifier auprès de Bigtable, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
[](cbt::Table table) {
  cbt::Filter filter = cbt::Filter::Condition(
      cbt::Filter::Chain(cbt::Filter::ValueRegex("true"),
                         cbt::Filter::ColumnRegex("data_plan_10gb")),
      cbt::Filter::ApplyLabelTransformer("passed-filter"),
      cbt::Filter::ApplyLabelTransformer("filtered-out"));
  // Read and print the rows.
  for (StatusOr<cbt::Row>& row :
       table.ReadRows(cbt::RowSet(cbt::RowRange::InfiniteRange()), filter)) {
    if (!row) throw std::move(row).status();
    std::cout << row->row_key() << " = ";
    for (auto const& cell : row->cells()) {
      std::cout << "[" << cell.family_name() << ", "
                << cell.column_qualifier() << ", " << cell.value()
                << ", label(";
      for (auto const& label : cell.labels()) {
        std::cout << label << ",";
      }
      std::cout << ")],";
    }
    std::cout << "\n";
  }
}

C#

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la page Bibliothèques clientes Bigtable.

Pour vous authentifier auprès de Bigtable, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

/// <summary>
/// /// Read using a conditional 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>

public string filterComposingCondition(string projectId = "YOUR-PROJECT-ID", string instanceId = "YOUR-INSTANCE-ID", string tableId = "YOUR-TABLE-ID")
{
    // A filter that applies the label passed-filter IF the cell has the column qualifier
    // data_plan_10gb AND the value true, OTHERWISE applies the label filtered-out
    RowFilter filter = RowFilters.Condition(
        RowFilters.Chain(RowFilters.ColumnQualifierExact("data_plan_10gb"), RowFilters.ValueExact("true")),
        new RowFilter { ApplyLabelTransformer = "passed-filter" },
        new RowFilter { ApplyLabelTransformer = "filtered-out" }
        );
    return readFilter(projectId, instanceId, tableId, filter);
}

Go

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la page Bibliothèques clientes Bigtable.

Pour vous authentifier auprès de Bigtable, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

func filterComposingCondition(w io.Writer, projectID, instanceID string, tableName string) error {
	filter := bigtable.ConditionFilter(
		bigtable.ChainFilters(bigtable.ColumnFilter("data_plan_10gb"), bigtable.ValueFilter("true")),
		bigtable.StripValueFilter(),
		bigtable.PassAllFilter())
	return readWithFilter(w, projectID, instanceID, tableName, filter)
}

Java

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la page Bibliothèques clientes Bigtable.

Pour vous authentifier auprès de Bigtable, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

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

public static void filterComposingCondition(String projectId, String instanceId, String tableId) {
  // A filter that applies the label passed-filter IF the cell has the column qualifier
  // data_plan_10gb AND the value true, OTHERWISE applies the label filtered-out
  Filter filter =
      FILTERS
          .condition(
              FILTERS
                  .chain()
                  .filter(FILTERS.qualifier().exactMatch("data_plan_10gb"))
                  .filter(FILTERS.value().exactMatch("true")))
          .then(FILTERS.label("passed-filter"))
          .otherwise(FILTERS.label("filtered-out"));
  readFilter(projectId, instanceId, tableId, filter);
}

Node.js

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la page Bibliothèques clientes Bigtable.

Pour vous authentifier auprès de Bigtable, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

const filter = {
  condition: {
    test: [
      {column: 'data_plan_10gb'},
      {
        value: 'true',
      },
    ],
    pass: {
      label: 'passed-filter',
    },
    fail: {
      label: 'filtered-out',
    },
  },
};
readWithFilter(filter);

PHP

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la page Bibliothèques clientes Bigtable.

Pour vous authentifier auprès de Bigtable, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

use Google\Cloud\Bigtable\BigtableClient;
use Google\Cloud\Bigtable\Filter;

/**
 * Create a composite filter using a conditional
 *
 * @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 filter_composing_condition(
    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);

    $filter = Filter::condition(
        Filter::chain()
            ->addFilter(Filter::value()->exactMatch('1'))
            ->addFilter(Filter::qualifier()->exactMatch('data_plan_10gb'))
    )
        ->then(Filter::label('passed-filter'))
        ->otherwise(Filter::label('filtered-out'));

    $rows = $table->readRows([
        'filter' => $filter
    ]);

    foreach ($rows as $key => $row) {
        // The "print_row" helper function is defined in https://cloud.google.com/bigtable/docs/samples/bigtable-reads-print
        print_row($key, $row);
    }
}

Python

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la page Bibliothèques clientes Bigtable.

Pour vous authentifier auprès de Bigtable, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

def filter_composing_condition(project_id, instance_id, table_id):
    client = bigtable.Client(project=project_id, admin=True)
    instance = client.instance(instance_id)
    table = instance.table(table_id)

    rows = table.read_rows(
        filter_=row_filters.ConditionalRowFilter(
            base_filter=row_filters.RowFilterChain(
                filters=[
                    row_filters.ColumnQualifierRegexFilter("data_plan_10gb"),
                    row_filters.ValueRegexFilter("true"),
                ]
            ),
            true_filter=row_filters.ApplyLabelFilter(label="passed-filter"),
            false_filter=row_filters.ApplyLabelFilter(label="filtered-out"),
        )
    )
    for row in rows:
        print_row(row)

Ruby

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la page Bibliothèques clientes Bigtable.

Pour vous authentifier auprès de Bigtable, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

# instance_id = "my-instance"
# table_id    = "my-table"
filter = Google::Cloud::Bigtable::RowFilter.condition(
  Google::Cloud::Bigtable::RowFilter.chain.qualifier("data_plan_10gb").value("true")
)
                                           .on_match(Google::Cloud::Bigtable::RowFilter.label("passed-filter"))
                                           .otherwise(Google::Cloud::Bigtable::RowFilter.label("filtered-out"))
read_with_filter instance_id, table_id, filter

Étapes suivantes

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'exemple de navigateur Google Cloud.