Crear una instancia y escribir datos con la CLI de cbt

Si estás aprendiendo sobre Bigtable, puedes completar una guía de inicio rápido para familiarizarte con los conceptos básicos que usarías a gran escala en un entorno de producción.

En esta guía de inicio rápido, realizarás las siguientes acciones:

  • Conéctate a una instancia de Bigtable.
  • Realizar tareas administrativas básicas.
  • Escribe datos en una tabla.
  • Leer datos de una tabla.

Antes de empezar

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. Si utilizas un proveedor de identidades (IdP) externo, primero debes iniciar sesión en la CLI de gcloud con tu identidad federada.

  4. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  5. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Bigtable and Cloud Bigtable Admin APIs:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable bigtable.googleapis.com bigtableadmin.googleapis.com
  8. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/bigtable.admin

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  9. Install the Google Cloud CLI.

  10. Si utilizas un proveedor de identidades (IdP) externo, primero debes iniciar sesión en la CLI de gcloud con tu identidad federada.

  11. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  12. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. Verify that billing is enabled for your Google Cloud project.

  14. Enable the Cloud Bigtable and Cloud Bigtable Admin APIs:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable bigtable.googleapis.com bigtableadmin.googleapis.com
  15. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/bigtable.admin

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  16. Ejecuta el siguiente comando para instalar la CLI de cbt:
    gcloud components install cbt
  17. Crear una instancia de Bigtable

    1. Abre la página Crear instancia en la Google Cloud consola.

      Crear una instancia

    2. En Nombre de instancia, introduce Quickstart instance.

    3. En Instance ID (ID de instancia), introduce quickstart-instance.

    4. Haz clic en Continuar.

    5. En Tipo de almacenamiento, selecciona SSD.

    6. Haz clic en Continuar.

    7. En ID de clúster, introduce quickstart-instance-c1.

    8. En Región, selecciona una región cercana.

    9. En Zona, selecciona Cualquiera.

    10. En Modo de escalado de nodos, elige Asignación manual.

    11. En Cantidad, selecciona 1.

    12. Haz clic en Crear para que se genere la instancia.

    Conectarse a la instancia

    1. Configura la CLI de cbt para que use tu proyecto y tu instancia. Para ello, crea un archivo .cbtrc y sustituye PROJECT_ID por el ID del proyecto en el que has creado tu instancia de Bigtable:

      echo project = PROJECT_ID >> ~/.cbtrc && echo instance = quickstart-instance >> ~/.cbtrc
      
    2. Comprueba que has configurado el archivo .cbtrc correctamente:

      cat ~/.cbtrc

      En el terminal se muestra el contenido del archivo .cbtrc, que es similar al siguiente:

      project = PROJECT_ID
      instance = quickstart-instance

      Ahora puedes usar la CLI cbt con tu instancia.

    Leer y escribir datos

    Bigtable almacena los datos en tablas, que contienen filas. Cada fila se identifica mediante una clave de fila.

    Los datos de una fila se organizan en familias de columnas, que son grupos de columnas. Un calificador de columna identifica una sola columna dentro de una familia de columnas.

    Puede haber varias celdas con marca de tiempo en la intersección de una fila y una columna.

    1. Crea una tabla llamada my-table.

      cbt createtable my-table
    2. Lista de tus tablas:

      cbt ls

      El comando muestra un resultado similar al siguiente:

          my-table

    3. Añade una familia de columnas llamada cf1:

      cbt createfamily my-table cf1
    4. Muestra tus familias de columnas:

      cbt ls my-table

      El comando muestra un resultado similar al siguiente:

          Family Name     GC Policy
          -----------     ---------
          cf1             <never>

    5. Escribe los valores test-value1 y test-value2 en la fila r1, usando la familia de columnas cf1 y el calificador de columna c1:

      cbt set my-table r1 cf1:c1=test-value1
        cbt set my-table r1 cf1:c1=test-value2
      
    6. Usa el comando cbt read para leer los datos que has añadido a la tabla:

      cbt read my-table

      El shell muestra un resultado similar al siguiente:

          ----------------------------------------
          r1
            cf1:c1                                   @ 2023/03/22-06:56:11.323000
              "test-value1"
            cf1:c1                                   @ 2023/03/22-06:56:04.361000
              "test-value2"

      Se almacenan dos valores con marca de tiempo en la misma columna de la fila r1.

    Limpieza

    Para evitar que se apliquen cargos en tu Google Cloud cuenta por los recursos utilizados en esta guía de inicio rápido, elimina la instancia. Si eliminas el archivo .cbtrc, podrás trabajar en otro proyecto.

    1. Elimina la tabla my-table:

      cbt deletetable my-table
    2. Elimina la instancia:

      cbt deleteinstance quickstart-instance
    3. Elimina el archivo .cbtrc:

      rm ~/.cbtrc

    4. Opcional: Revoca las credenciales de gcloud CLI:

      gcloud auth revoke

    Siguientes pasos