Hello World Java

Cet exemple de code présente une application "Hello World" écrite en Java, qui utilise la bibliothèque cliente Bigtable pour Java. Il montre comment effectuer les tâches suivantes :

  • Configurer l'authentification
  • Se connecter à une instance Bigtable
  • créer une table ;
  • Écrire des données dans une table
  • Relire les données
  • Supprimer la table

Configurer l'authentification

Pour utiliser les exemples Java de cette page dans un environnement de développement local, installez et initialisez gcloud CLI, puis configurez le service Identifiants par défaut de l'application à l'aide de vos identifiants utilisateur.

  1. Installez Google Cloud CLI.
  2. Pour initialiser gcloudCLI, exécutez la commande suivante :

    gcloud init
  3. Créez des identifiants d'authentification locaux pour votre compte Google :

    gcloud auth application-default login

Pour en savoir plus, consultez les sections sur Configurer l'authentification pour un environnement de développement local.

Exécuter l'exemple

Ce code communique avec Bigtable à l'aide de la bibliothèque Google Cloud Bigtable qui se trouve dans les bibliothèques clientes Google Cloud pour Java.

Avant de commencer, suivez les instructions relatives aux exemples Google Cloud Platform sur GitHub.

Utiliser la bibliothèque cliente Cloud avec Bigtable

L'exemple d'application permet de se connecter à Bigtable et décrit quelques opérations simples.

Se connecter à Bigtable

Pour commencer, vous avez besoin d'un client de données que vous utiliserez pour communiquer avec la bibliothèque cliente de l'API Data et d'un client d'administration de table que vous utiliserez pour communiquer avec la bibliothèque cliente de l'API Admin.

Commencez par instancier un objet BigtableDataSettings incluant l'ID de projet et l'ID d'instance que l'application hello world utilisera. Ensuite, transmettez les paramètres à la méthode BigtableDataClient.create() pour créer le client de données.

De même, pour le client d'administration, établissez d'abord les paramètres en créant un objet BigtableTableAdminSettings, puis utilisez-les pour créer un objet BigtableTableAdminClient.

Lorsque vous utilisez Bigtable, il est recommandé de systématiquement créer un client une fois, puis de le réutiliser dans l'ensemble de l'application.

// Creates the settings to configure a bigtable data client.
BigtableDataSettings settings =
    BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();

// Creates a bigtable data client.
dataClient = BigtableDataClient.create(settings);

// Creates the settings to configure a bigtable table admin client.
BigtableTableAdminSettings adminSettings =
    BigtableTableAdminSettings.newBuilder()
        .setProjectId(projectId)
        .setInstanceId(instanceId)
        .build();

// Creates a bigtable table admin client.
adminClient = BigtableTableAdminClient.create(adminSettings);

Créer une table

Pour créer une table, créez un objet CreateTableRequest, puis transmettez-le à la méthode createTable() du client d'administration.

// Checks if table exists, creates table if does not exist.
if (!adminClient.exists(tableId)) {
  System.out.println("Creating table: " + tableId);
  CreateTableRequest createTableRequest =
      CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY);
  adminClient.createTable(createTableRequest);
  System.out.printf("Table %s created successfully%n", tableId);
}

Écrire des lignes dans une table

Créez un tableau de chaînes greetings[] contenant trois messages d'accueil simples, à utiliser comme source de données à écrire dans la table. Parcourez le tableau. À chaque itération de la boucle, créez un objet RowMutation et utilisez la méthode setCell() pour ajouter une entrée à la mutation.

try {
  System.out.println("\nWriting some greetings to the table");
  String[] names = {"World", "Bigtable", "Java"};
  for (int i = 0; i < names.length; i++) {
    String greeting = "Hello " + names[i] + "!";
    RowMutation rowMutation =
        RowMutation.create(TableId.of(tableId), ROW_KEY_PREFIX + i)
            .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, names[i])
            .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting);
    dataClient.mutateRow(rowMutation);
    System.out.println(greeting);
  }
} catch (NotFoundException e) {
  System.err.println("Failed to write to non-existent table: " + e.getMessage());
}

Lire une ligne à l'aide de sa clé

Utilisez la méthode readRow() du client de données pour lire la première ligne que vous avez écrite.

try {
  System.out.println("\nReading a single row by row key");
  Row row = dataClient.readRow(TableId.of(tableId), ROW_KEY_PREFIX + 0);
  System.out.println("Row: " + row.getKey().toStringUtf8());
  for (RowCell cell : row.getCells()) {
    System.out.printf(
        "Family: %s    Qualifier: %s    Value: %s%n",
        cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
  }
  return row;
} catch (NotFoundException e) {
  System.err.println("Failed to read from a non-existent table: " + e.getMessage());
  return null;
}
try {
  System.out.println("\nReading specific cells by family and qualifier");
  Row row = dataClient.readRow(TableId.of(tableId), ROW_KEY_PREFIX + 0);
  System.out.println("Row: " + row.getKey().toStringUtf8());
  List<RowCell> cells = row.getCells(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME);
  for (RowCell cell : cells) {
    System.out.printf(
        "Family: %s    Qualifier: %s    Value: %s%n",
        cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
  }
  return cells;
} catch (NotFoundException e) {
  System.err.println("Failed to read from a non-existent table: " + e.getMessage());
  return null;
}

Analyser toutes les lignes de la table

Ensuite, analysez l'intégralité de la table. Créez un objet Query, transmettez-le à la méthode readRows(), puis attribuez les résultats à un flux de lignes.

try {
  System.out.println("\nReading the entire table");
  Query query = Query.create(TableId.of(tableId));
  ServerStream<Row> rowStream = dataClient.readRows(query);
  List<Row> tableRows = new ArrayList<>();
  for (Row r : rowStream) {
    System.out.println("Row Key: " + r.getKey().toStringUtf8());
    tableRows.add(r);
    for (RowCell cell : r.getCells()) {
      System.out.printf(
          "Family: %s    Qualifier: %s    Value: %s%n",
          cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
    }
  }
  return tableRows;
} catch (NotFoundException e) {
  System.err.println("Failed to read a non-existent table: " + e.getMessage());
  return null;
}

Supprimer une table

Enfin, supprimez la table à l'aide de la méthode deleteTable().

System.out.println("\nDeleting table: " + tableId);
try {
  adminClient.deleteTable(tableId);
  System.out.printf("Table %s deleted successfully%n", tableId);
} catch (NotFoundException e) {
  System.err.println("Failed to delete a non-existent table: " + e.getMessage());
}

Synthèse

Voici l'exemple de code complet sans commentaires.


package com.example.bigtable;

import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;

import com.google.api.gax.rpc.NotFoundException;
import com.google.api.gax.rpc.ServerStream;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowCell;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class HelloWorld {

  private static final String COLUMN_FAMILY = "cf1";
  private static final String COLUMN_QUALIFIER_GREETING = "greeting";
  private static final String COLUMN_QUALIFIER_NAME = "name";
  private static final String ROW_KEY_PREFIX = "rowKey";
  private final String tableId;
  private final BigtableDataClient dataClient;
  private final BigtableTableAdminClient adminClient;

  public static void main(String[] args) throws Exception {

    if (args.length != 2) {
      System.out.println("Missing required project id or instance id");
      return;
    }
    String projectId = args[0];
    String instanceId = args[1];

    HelloWorld helloWorld = new HelloWorld(projectId, instanceId, "test-table");
    helloWorld.run();
  }

  public HelloWorld(String projectId, String instanceId, String tableId) throws IOException {
    this.tableId = tableId;

    BigtableDataSettings settings =
        BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();

    dataClient = BigtableDataClient.create(settings);

    BigtableTableAdminSettings adminSettings =
        BigtableTableAdminSettings.newBuilder()
            .setProjectId(projectId)
            .setInstanceId(instanceId)
            .build();

    adminClient = BigtableTableAdminClient.create(adminSettings);
  }

  public void run() throws Exception {
    createTable();
    writeToTable();
    readSingleRow();
    readSpecificCells();
    readTable();
    filterLimitCellsPerCol(tableId);
    deleteTable();
    close();
  }

  public void close() {
    dataClient.close();
    adminClient.close();
  }

  public void createTable() {
    if (!adminClient.exists(tableId)) {
      System.out.println("Creating table: " + tableId);
      CreateTableRequest createTableRequest =
          CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY);
      adminClient.createTable(createTableRequest);
      System.out.printf("Table %s created successfully%n", tableId);
    }
  }

  public void writeToTable() {
    try {
      System.out.println("\nWriting some greetings to the table");
      String[] names = {"World", "Bigtable", "Java"};
      for (int i = 0; i < names.length; i++) {
        String greeting = "Hello " + names[i] + "!";
        RowMutation rowMutation =
            RowMutation.create(TableId.of(tableId), ROW_KEY_PREFIX + i)
                .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, names[i])
                .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting);
        dataClient.mutateRow(rowMutation);
        System.out.println(greeting);
      }
    } catch (NotFoundException e) {
      System.err.println("Failed to write to non-existent table: " + e.getMessage());
    }
  }

  public Row readSingleRow() {
    try {
      System.out.println("\nReading a single row by row key");
      Row row = dataClient.readRow(TableId.of(tableId), ROW_KEY_PREFIX + 0);
      System.out.println("Row: " + row.getKey().toStringUtf8());
      for (RowCell cell : row.getCells()) {
        System.out.printf(
            "Family: %s    Qualifier: %s    Value: %s%n",
            cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
      }
      return row;
    } catch (NotFoundException e) {
      System.err.println("Failed to read from a non-existent table: " + e.getMessage());
      return null;
    }
  }

  public List<RowCell> readSpecificCells() {
    try {
      System.out.println("\nReading specific cells by family and qualifier");
      Row row = dataClient.readRow(TableId.of(tableId), ROW_KEY_PREFIX + 0);
      System.out.println("Row: " + row.getKey().toStringUtf8());
      List<RowCell> cells = row.getCells(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME);
      for (RowCell cell : cells) {
        System.out.printf(
            "Family: %s    Qualifier: %s    Value: %s%n",
            cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
      }
      return cells;
    } catch (NotFoundException e) {
      System.err.println("Failed to read from a non-existent table: " + e.getMessage());
      return null;
    }
  }

  public List<Row> readTable() {
    try {
      System.out.println("\nReading the entire table");
      Query query = Query.create(TableId.of(tableId));
      ServerStream<Row> rowStream = dataClient.readRows(query);
      List<Row> tableRows = new ArrayList<>();
      for (Row r : rowStream) {
        System.out.println("Row Key: " + r.getKey().toStringUtf8());
        tableRows.add(r);
        for (RowCell cell : r.getCells()) {
          System.out.printf(
              "Family: %s    Qualifier: %s    Value: %s%n",
              cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
        }
      }
      return tableRows;
    } catch (NotFoundException e) {
      System.err.println("Failed to read a non-existent table: " + e.getMessage());
      return null;
    }
  }

  public void filterLimitCellsPerCol(String tableId) {
    Filter filter = FILTERS.limit().cellsPerColumn(1);
    readRowFilter(tableId, filter);
    readFilter(tableId, filter);
  }

  private void readRowFilter(String tableId, Filter filter) {
    String rowKey =
        Base64.getEncoder().encodeToString("greeting0".getBytes(StandardCharsets.UTF_8));
    Row row = dataClient.readRow(TableId.of(tableId), rowKey, filter);
    printRow(row);
    System.out.println("Row filter completed.");
  }

  private void readFilter(String tableId, Filter filter) {
    Query query = Query.create(TableId.of(tableId)).filter(filter);
    ServerStream<Row> rows = dataClient.readRows(query);
    for (Row row : rows) {
      printRow(row);
    }
    System.out.println("Table filter completed.");
  }

  public void deleteTable() {
    System.out.println("\nDeleting table: " + tableId);
    try {
      adminClient.deleteTable(tableId);
      System.out.printf("Table %s deleted successfully%n", tableId);
    } catch (NotFoundException e) {
      System.err.println("Failed to delete a non-existent table: " + e.getMessage());
    }
  }

  private static void printRow(Row row) {
    if (row == null) {
      return;
    }
    System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8());
    String colFamily = "";
    for (RowCell cell : row.getCells()) {
      if (!cell.getFamily().equals(colFamily)) {
        colFamily = cell.getFamily();
        System.out.printf("Column Family %s%n", colFamily);
      }
      String labels =
          cell.getLabels().size() == 0 ? "" : " [" + String.join(",", cell.getLabels()) + "]";
      System.out.printf(
          "\t%s: %s @%s%s%n",
          cell.getQualifier().toStringUtf8(),
          cell.getValue().toStringUtf8(),
          cell.getTimestamp(),
          labels);
    }
    System.out.println();
  }
}