יצירת טבלת בדיקה

בדף הזה מתוארים השלבים ליצירת טבלת Bigtable קטנה שאפשר להשתמש בה כדי לבדוק קטעי קוד. הטבלה מכילה נתונים של סדרות עיתיות לסמארטפונים ולטאבלטים.

בהוראות האלה נעשה שימוש במסוף Google Cloud וב-cbt CLI, ממשק שורת פקודה שנוצר במיוחד ל-Bigtable.

לפני שמתחילים

לפני שיוצרים טבלת בדיקה, צריך לוודא שמתקיימות הדרישות המוקדמות הבאות.

מגדירים אימות

במסוף Google Cloud , מפעילים את Cloud Shell.

הפעלת Cloud Shell

בחלק התחתון של Google Cloud המסוף יתחיל סשן של Cloud Shell ותופיע הודעה של שורת הפקודה. Cloud Shell היא סביבת מעטפת שבה ה-CLI של Google Cloud מותקן ומוגדרים ערכים לפרויקט הקיים. הסשן יופעל תוך כמה שניות.

למידע נוסף על הגדרה של אימות בסביבת ייצור, ראו הגדרה של Application Default Credentials לקוד שפועל ב- Google Cloud .

מתן תפקיד IAM

כדי לקבל את ההרשאות שנדרשות ליצירת טבלת בדיקה, צריך לבקש מהאדמין להקצות לכם ב-IAM את התפקיד אדמין של Bigtable (roles/bigtable.admin) בפרויקט. כדי לקרוא הסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

התקנת ה-CLI של cbt

מריצים את הפקודה הבאה כדי להתקין את ה-CLI של cbt:

  gcloud components install cbt

יצירת מופע

בפרויקט שבוחרים, יוצרים מכונת בדיקה לאחסון טבלת הבדיקה. הטבלה קטנה, ולכן צריך רק מופע עם צומת יחיד.

  1. פותחים את הדף Create instance במסוף Google Cloud .

    יצירת מופע

  2. בשדה Instance name, מזינים Test instance.

  3. בשדה Instance ID, מזינים test-instance.

  4. בקטע Storage type (סוג האחסון), בוחרים באפשרות SSD.

  5. בשדה מזהה האשכול, מזינים test-instance-c1.

  6. בשדה Region, בוחרים אזור שקרוב אליכם.

  7. בשדה Zone, בוחרים אזור באזור שבחרתם.

  8. בקטע Nodes (צמתים), מזינים 1.

  9. לוחצים על יצירה כדי ליצור את המופע.

התחברות למכונה

  1. מגדירים את cbt CLI כך שישתמש בפרויקט ובמופע שלכם על ידי יצירת קובץ .cbtrc והחלפת PROJECT_ID במזהה של הפרויקט שבו יצרתם את מופע Bigtable:

    echo project = PROJECT_ID >> ~/.cbtrc && echo instance = test-instance >> ~/.cbtrc
    
  2. מוודאים שהגדרתם את הקובץ .cbtrc בצורה נכונה:

    cat ~/.cbtrc

    במסוף מוצג התוכן של הקובץ .cbtrc, שנראה כך:

    project = PROJECT_ID
    instance = test-instance

    עכשיו אפשר להשתמש ב-cbt CLI עם המופע.

יצירת טבלת בדיקה

  1. יוצרים טבלה בשם test_table עם שתי קבוצות עמודות. עבור כל משפחת עמודות, הגדר מדיניות איסוף אשפה שתשמור על מקסימום של 2 תאים בכל עמודה:

       cbt createtable test_table "families=stats_summary:maxversions=2,stats_detail:maxversions=2,cell_plan:maxversions=2"

  2. מציגים את רשימת קבוצות העמודות:

       cbt ls test_table

    הפלט במסוף אמור להיראות כך:

        Family Name       GC Policy
        -----------       ---------
        stats_detail        versions() > 2
        stats_summary       versions() > 2
        cell_plan           versions() > 2

מילוי הטבלה

  1. מעתיקים את הפקודות הבאות לחלון הטרמינל ומקישים על Enter.

      cbt set test_table phone#4c410523#20190501 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190405.003 cell_plan:data_plan_01gb=true cell_plan:data_plan_05gb=true
      cbt set test_table phone#4c410523#20190502 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190405.004 cell_plan:data_plan_05gb=true
      cbt set test_table phone#4c410523#20190505 stats_summary:connected_cell=0 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190406.000
      cbt set test_table phone#5c10102#20190501 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190401.002 cell_plan:data_plan_10gb=true
      cbt set test_table tablet#5c10102#20190502 stats_summary:connected_cell=1 stats_summary:connected_wifi=0 stats_summary:os_build=PQ2A.190406.000 cell_plan:data_plan_10gb=true
  2. משתמשים בפקודה cbt read כדי לקרוא את הנתונים שהוספתם לטבלה:

       cbt read test_table

    הפלט במסוף אמור להיראות כך: שימו לב: חותמות זמן מוקצות לתאים באופן אוטומטי כי בקשת הכתיבה לא כוללת חותמות זמן.

        ----------------------------------------
        phone#4c410523#20190501
          cell_plan:data_plan_01gb                 @ 2023/09/23-11:44:10.535000
            "true"
          cell_plan:data_plan_05gb                 @ 2023/09/23-11:44:10.535000
            "true"
          stats_summary:connected_cell             @ 2023/09/23-11:44:10.535000
            "1"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:10.535000
            "1"
          stats_summary:os_build                   @ 2023/09/23-11:44:10.535000
            "PQ2A.190405.003"
        ----------------------------------------
        phone#4c410523#20190502
          cell_plan:data_plan_05gb                 @ 2023/09/23-11:44:11.545000
            "true"
          stats_summary:connected_cell             @ 2023/09/23-11:44:11.545000
            "1"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:11.545000
            "1"
          stats_summary:os_build                   @ 2023/09/23-11:44:11.545000
            "PQ2A.190405.004"
        ----------------------------------------
        phone#4c410523#20190505\
          stats_summary:connected_cell             @ 2023/09/23-11:44:12.503000
            "0"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:12.503000
            "1"
          stats_summary:os_build                   @ 2023/09/23-11:44:12.503000
            "PQ2A.190406.000"
        ----------------------------------------
        phone#5c10102#20190501
          cell_plan:data_plan_10gb                 @ 2023/09/23-11:44:13.553000
            "true"
          stats_summary:connected_cell             @ 2023/09/23-11:44:13.553000
            "1"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:13.553000
            "1"
          stats_summary:os_build                   @ 2023/09/23-11:44:13.553000
            "PQ2A.190401.002"
        ----------------------------------------
        tablet#5c10102#20190502
          cell_plan:data_plan_10gb                 @ 2023/09/23-11:44:14.480000
            "true"
          stats_summary:connected_cell             @ 2023/09/23-11:44:14.480000
            "1"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:14.480000
            "0"
          stats_summary:os_build                   @ 2023/09/23-11:44:14.480000
                 "PQ2A.190406.000"
    
  3. אופציונלי: כותבים ערכים חדשים באותן שורות ועמודות. ‫Bigtable יוצר תאים חדשים עם חותמות זמן חדשות כדי לאחסן את הגרסאות החדשות של הנתונים. מעתיקים את הפקודות הבאות לחלון הטרמינל ומקישים על Enter.

      cbt set test_table phone#4c410523#20190501 stats_summary:connected_cell=2 stats_summary:connected_wifi=5 stats_summary:os_build=PQ2A.190405.003 cell_plan:data_plan_01gb=true cell_plan:data_plan_05gb=false
      cbt set test_table phone#4c410523#20190502 stats_summary:connected_cell=2 stats_summary:connected_wifi=5 stats_summary:os_build=PQ2A.190405.004 cell_plan:data_plan_05gb=false
      cbt set test_table phone#4c410523#20190505 stats_summary:connected_cell=1 stats_summary:connected_wifi=4 stats_summary:os_build=PQ2A.190406.000
      cbt set test_table phone#5c10102#20190501 stats_summary:connected_cell=3 stats_summary:connected_wifi=3 stats_summary:os_build=PQ2A.190401.002 cell_plan:data_plan_10gb=false
      cbt set test_table tablet#5c10102#20190502 stats_summary:connected_cell=2 stats_summary:connected_wifi=0 stats_summary:os_build=PQ2A.190406.000 cell_plan:data_plan_10gb=false
  4. משתמשים בפקודה cbt read כדי לקרוא את כל הנתונים בטבלה:

       cbt read test_table

    הפלט במסוף אמור להיראות כך: כל עמודה מכילה עכשיו שתי תאים עם חותמות זמן ייחודיות.

      ----------------------------------------
      phone#4c410523#20190501
        cell_plan:data_plan_01gb                 @ 2024/07/09-17:48:21.191000
        "true"
        cell_plan:data_plan_01gb                 @ 2024/07/09-17:46:09.369000
        "true"
        cell_plan:data_plan_05gb                 @ 2024/07/09-17:48:21.191000
        "false"
        cell_plan:data_plan_05gb                 @ 2024/07/09-17:46:09.369000
        "true"
        stats_summary:connected_cell             @ 2024/07/09-17:48:21.191000
        "2"
        stats_summary:connected_cell             @ 2024/07/09-17:46:09.369000
        "1"
        stats_summary:connected_wifi             @ 2024/07/09-17:48:21.191000
        "5"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:09.369000
        "1"
        stats_summary:os_build                   @ 2024/07/09-17:48:21.191000
        "PQ2A.190405.003"
        stats_summary:os_build                   @ 2024/07/09-17:46:09.369000
        "PQ2A.190405.003"
    
      ----------------------------------------
      phone#4c410523#20190502
        cell_plan:data_plan_05gb                 @ 2024/07/09-17:48:22.205000
        "false"
        cell_plan:data_plan_05gb                 @ 2024/07/09-17:46:10.455000
        "true"
        stats_summary:connected_cell             @ 2024/07/09-17:48:22.205000
        "2"
        stats_summary:connected_cell             @ 2024/07/09-17:46:10.455000
        "1"
         stats_summary:connected_wifi             @ 2024/07/09-17:48:22.205000
        "5"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:10.455000
        "1"
        stats_summary:os_build                   @ 2024/07/09-17:48:22.205000
        "PQ2A.190405.004"
        stats_summary:os_build                   @ 2024/07/09-17:46:10.455000
        "PQ2A.190405.004"
    
      ----------------------------------------
      phone#4c410523#20190505
        stats_summary:connected_cell             @ 2024/07/09-17:48:23.206000
        "1"
        stats_summary:connected_cell             @ 2024/07/09-17:46:11.402000
        "0"
        stats_summary:connected_wifi             @ 2024/07/09-17:48:23.206000
        "4"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:11.402000
        "1"
        stats_summary:os_build                   @ 2024/07/09-17:48:23.206000
        "PQ2A.190406.000"
        stats_summary:os_build                   @ 2024/07/09-17:46:11.402000
        "PQ2A.190406.000"
    
      ----------------------------------------
      phone#5c10102#20190501
        cell_plan:data_plan_10gb                 @ 2024/07/09-17:48:24.172000
        "false"
        cell_plan:data_plan_10gb                 @ 2024/07/09-17:46:12.388000
        "true"
        stats_summary:connected_cell             @ 2024/07/09-17:48:24.172000
        "3"
        stats_summary:connected_cell             @ 2024/07/09-17:46:12.388000
        "1"
        stats_summary:connected_wifi             @ 2024/07/09-17:48:24.172000
        "3"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:12.388000
        "1"
        stats_summary:os_build                   @ 2024/07/09-17:48:24.172000
        "PQ2A.190401.002"
        stats_summary:os_build                   @ 2024/07/09-17:46:12.388000
        "PQ2A.190401.002"
    
      ----------------------------------------
      tablet#5c10102#20190502
        cell_plan:data_plan_10gb                 @ 2024/07/09-17:48:25.194000
        "false"
        cell_plan:data_plan_10gb                 @ 2024/07/09-17:46:13.391000
        "true"
        stats_summary:connected_cell             @ 2024/07/09-17:48:25.194000
        "2"
        stats_summary:connected_cell             @ 2024/07/09-17:46:13.391000
        "1"
        stats_summary:connected_wifi             @ 2024/07/09-17:48:25.194000
        "0"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:13.391000
        "0"
        stats_summary:os_build                   @ 2024/07/09-17:48:25.194000
        "PQ2A.190406.000"
        stats_summary:os_build                   @ 2024/07/09-17:46:13.391000
        "PQ2A.190406.000"
    

שימוש בנתונים לבדיקה

עכשיו אפשר להשתמש בלחצן test_table ב-test-instance כדי לבדוק דוגמאות קוד או שאילתות.

הסרת המשאבים

כדי להימנע מחיובים בחשבון Google Cloud על המשאבים שנוצרו במהלך השלבים האלה, מוחקים את המופע מיד אחרי שמסיימים את הבדיקה. מחיקת קובץ ה-cbtrc מאפשרת לעבוד על פרויקט אחר.

  1. אם רוצים לשמור את המופע אבל למחוק את הטבלה, אפשר להשתמש בפקודה deletetable:

       cbt deletetable test_table

  2. אם אתם לא מתכננים להשתמש במופע לבדיקות נוספות, מוחקים את המופע. הפעולה הזו תמחק גם את הטבלה.

       cbt deleteinstance test-instance

  3. מוחקים את קובץ ה-cbtrc:

       rm ~/.cbtrc

המאמרים הבאים