Membuat instance dan menulis data dengan cbt CLI

Jika sedang mempelajari Bigtable, Anda dapat mempelajari panduan memulai cepat untuk memahami dasar-dasar yang akan Anda gunakan dalam skala besar di lingkungan produksi.

Dalam panduan memulai ini, Anda akan melakukan tindakan berikut:

  • Hubungkan ke instance Bigtable.
  • Lakukan tugas administratif dasar.
  • Menulis data ke tabel.
  • Membaca data dari tabel.

Sebelum memulai

  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. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

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

    • 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. Make sure that billing is enabled for your Google Cloud project.

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

    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 PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.
  9. Install the Google Cloud CLI.

  10. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  11. To initialize the gcloud CLI, run the following command:

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

    • 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. Make sure that billing is enabled for your Google Cloud project.

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

    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 PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.
  16. Jalankan perintah berikut untuk menginstal cbt CLI :
    gcloud components install cbt
  17. Membuat instance Bigtable

    1. Buka halaman Create instance di konsol Google Cloud .

      Membuat instance

    2. Untuk Instance name, masukkan Quickstart instance.

    3. Untuk Instance ID, masukkan quickstart-instance.

    4. Klik Lanjutkan.

    5. Untuk Storage type, pilih SSD.

    6. Klik Lanjutkan.

    7. Untuk Cluster ID, masukkan quickstart-instance-c1.

    8. Untuk Region, pilih wilayah di dekat Anda.

    9. Untuk Zone, pilih Any.

    10. Untuk Node scaling mode, pilih Manual allocation.

    11. Untuk Jumlah, pilih 1.

    12. Klik Buat untuk membuat instance.

    Menghubungkan ke instance

    1. Konfigurasi CLI cbt untuk menggunakan project dan instance Anda dengan membuat file .cbtrc, mengganti PROJECT_ID dengan ID untuk project tempat Anda membuat instance Bigtable:

      echo project = PROJECT_ID >> ~/.cbtrc && echo instance = quickstart-instance >> ~/.cbtrc
      
    2. Pastikan Anda menyiapkan file .cbtrc dengan benar:

      cat ~/.cbtrc

      Terminal akan menampilkan isi file .cbtrc, yang terlihat mirip dengan berikut:

      project = PROJECT_ID
      instance = quickstart-instance

      Sekarang Anda dapat menggunakan cbt CLI dengan instance Anda.

    Membaca dan menulis data

    Bigtable menyimpan data dalam tabel, yang berisi baris. Setiap baris diidentifikasi oleh kunci baris.

    Data dalam baris disusun ke dalam grup kolom, yang merupakan grup kolom. Penentu kolom mengidentifikasi satu kolom dalam grup kolom.

    Dapat ada beberapa sel dengan stempel waktu di persimpangan baris dan kolom.

    1. Buat tabel bernama my-table.

      cbt createtable my-table
    2. Menampilkan daftar tabel Anda:

      cbt ls

      Perintah ini menampilkan output yang mirip dengan berikut ini:

          my-table

    3. Tambahkan satu family kolom bernama cf1:

      cbt createfamily my-table cf1
    4. Mencantumkan grup kolom Anda:

      cbt ls my-table

      Perintah ini menampilkan output yang mirip dengan berikut ini:

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

    5. Tulis nilai test-value1 dan test-value2 ke baris r1, menggunakan grup kolom cf1 dan penentu kolom c1:

      cbt set my-table r1 cf1:c1=test-value1
        cbt set my-table r1 cf1:c1=test-value2
      
    6. Gunakan perintah cbt read untuk membaca data yang Anda tambahkan ke tabel:

      cbt read my-table

      Shell akan menampilkan output yang mirip dengan berikut ini:

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

      Dua nilai yang diberi stempel waktu disimpan untuk kolom yang sama di baris r1.

    Pembersihan

    Agar tidak menimbulkan biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam panduan memulai ini, hapus instance. Menghapus file .cbtrc membuat Anda siap mengerjakan project lain.

    1. Hapus tabel my-table:

      cbt deletetable my-table
    2. Hapus instance:

      cbt deleteinstance quickstart-instance
    3. Hapus file .cbtrc:

      rm ~/.cbtrc

    4. Opsional: Cabut kredensial dari gcloud CLI:

      gcloud auth revoke

    Langkah berikutnya