יצירת מסד נתונים של Firestore באמצעות ספריית לקוח של שרת
במדריך המקוצר הזה מוסבר איך להגדיר את Firestore, להוסיף נתונים ולקרוא נתונים באמצעות ספריית לקוח של שרת ב-C#, ב-Go, ב-Java, ב-Node.js, ב-PHP, ב-Python או ב-Ruby.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
In the Google Cloud console, on the project selector page, select or create 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
In the Google Cloud console, on the project selector page, select or create 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
יצירת מסד נתונים של Firestore במצב Native
אם זה פרויקט חדש, צריך ליצור מופע של מסד נתונים של Firestore.
במסך Select a database service (בחירת שירות מסד נתונים), בוחרים באפשרות Firestore in Native mode (Firestore במצב Native).
בוחרים מיקום ל-Firestore.
אם אי אפשר לבחור מיקום, סימן שהמיקום של משאבי ברירת המחדל Google Cloud בפרויקט כבר הוגדר. חלק מהמשאבים בפרויקט (כמו מופע ברירת המחדל של Firestore) חולקים תלות משותפת במיקום, ואפשר להגדיר את המיקום שלהם במהלך יצירת הפרויקט או כשמגדירים שירות אחר שחולק את התלות הזו במיקום.
לוחצים על יצירת מסד נתונים.
כשיוצרים פרויקט Firestore, ה-API מופעל גם ב-Cloud API Manager.
מגדירים אימות
כדי להריץ את ספריית הלקוח, צריך קודם להגדיר אימות על ידי יצירת חשבון שירות והגדרת משתנה סביבה.
הוספה של ספריית הלקוח של השרת לאפליקציה
מוסיפים לאפליקציה את התלויות הנדרשות ואת ספריות הלקוח.
Java
מוסיפים את ספריית Java של Firestore לאפליקציה:
-
שימוש ב-Maven:
<dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>libraries-bom</artifactId> <version>26.65.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-firestore</artifactId> </dependency>
- אם אתם משתמשים ב-Gradle או מגדירים בלי BOM, תוכלו לעיין בקובץ ה-README של לקוח Firestore ל-Java.
-
שימוש בסביבת פיתוח משולבת (IDE):
אם משתמשים ב-VS Code, ב-IntelliJ או ב-Eclipse, אפשר להוסיף את ספריות הלקוח לפרויקט באמצעות יישומי הפלאגין הבאים של IDE:
באמצעות יישומי הפלאגין תוכלו להשתמש בפונקציות נוספות, כמו ניהול מפתחות לחשבונות שירות. לפרטים נוספים, קראו את מאמרי העזרה של כל אחד מיישומי הפלאגין.
Python
מוסיפים את ספריית Python של Firestore לאפליקציה:
pip install --upgrade google-cloud-firestore
Node.js
להוסיף את ספריית Firestore Node.js לאפליקציה:
npm install --save @google-cloud/firestore
המשך
מתקינים את ספריית Go של Firestore:
go get cloud.google.com/go/firestore
להוסיף את ספריית Firestore Go לאפליקציה:
import "cloud.google.com/go/firestore"
PHP
- מתקינים ומפעילים את התוסף gRPC ל-PHP, שנדרש לשימוש בספריית הלקוח.
-
מוסיפים את ספריית Firestore PHP לאפליקציה:
composer require google/cloud-firestore
C#
-
מוסיפים את ספריית Firestore C# לאפליקציה בקובץ
.csproj:<ItemGroup> <PackageReference Include="Google.Cloud.Firestore" Version="1.1.0-beta01" /> </ItemGroup>
-
מוסיפים לקובץ
Program.csאת הנתונים הבאים:using Google.Cloud.Firestore;
Ruby
-
מוסיפים את ספריית Firestore Ruby לאפליקציה בקובץ
Gemfile:gem "google-cloud-firestore"
-
מתקינים את יחסי התלות מ-
Gemfileבאמצעות הפקודה:bundle install
הפעלה של Firestore במצב Native
מאתחלים מופע של Firestore:
Java
import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.FirestoreOptions;
FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() .setProjectId(projectId) .setCredentials(GoogleCredentials.getApplicationDefault()) .build(); Firestore db = firestoreOptions.getService();
Python
from google.cloud import firestore # The `project` parameter is optional and represents which project the client # will act on behalf of. If not supplied, the client falls back to the default # project inferred from the environment. db = firestore.Client(project="my-project-id")
Python
(Async)
from google.cloud import firestore # The `project` parameter is optional and represents which project the client # will act on behalf of. If not supplied, the client falls back to the default # project inferred from the environment. db = firestore.AsyncClient(project="my-project-id")
Node.js
const Firestore = require('@google-cloud/firestore'); const db = new Firestore({ projectId: 'YOUR_PROJECT_ID', keyFilename: '/path/to/keyfile.json', });
המשך
import ( "context" "flag" "fmt" "log" "google.golang.org/api/iterator" "cloud.google.com/go/firestore" ) func createClient(ctx context.Context) *firestore.Client { // Sets your Google Cloud Platform project ID. projectID := "YOUR_PROJECT_ID" client, err := firestore.NewClient(ctx, projectID) if err != nil { log.Fatalf("Failed to create client: %v", err) } // Close client when done with // defer client.Close() return client }
PHP
use Google\Cloud\Firestore\FirestoreClient; /** * Initialize Cloud Firestore with default project ID. */ function setup_client_create(string $projectId = null) { // Create the Cloud Firestore client if (empty($projectId)) { // The `projectId` parameter is optional and represents which project the // client will act on behalf of. If not supplied, the client falls back to // the default project inferred from the environment. $db = new FirestoreClient(); printf('Created Cloud Firestore client with default project ID.' . PHP_EOL); } else { $db = new FirestoreClient([ 'projectId' => $projectId, ]); printf('Created Cloud Firestore client with project ID: %s' . PHP_EOL, $projectId); } }
C#
Ruby
require "google/cloud/firestore" # The `project_id` parameter is optional and represents which project the # client will act on behalf of. If not supplied, the client falls back to the # default project inferred from the environment. firestore = Google::Cloud::Firestore.new project_id: project_id puts "Created Cloud Firestore client with given project ID."
הוספת נתונים
נתונים ב-Firestore מאוחסנים במסמכים, שמאוחסנים באוספים. Firestore יוצר אוספים ומסמכים באופן מרומז בפעם הראשונה שמוסיפים נתונים למסמך. אתם לא צריכים ליצור אוספים או מסמכים באופן מפורש.
יוצרים אוסף ומסמך חדשים באמצעות קוד הדוגמה הבא.
Java
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Python
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Python
(Async)
Node.js
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Go
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
PHP
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
C#
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Ruby
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
עכשיו מוסיפים עוד מסמך לאוסף users. שימו לב שהמסמך הזה כולל צמד מפתח/ערך (שם אמצעי) שלא מופיע במסמך הראשון. מסמכים באוסף יכולים להכיל קבוצות שונות של מידע.
Java
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Python
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Python
(Async)
Node.js
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Go
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
PHP
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
C#
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Ruby
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
קריאת נתונים
כדי לוודא במהירות שהוספתם נתונים ל-Firestore, אפשר להשתמש בכלי להצגת נתונים במסוף Firebase.
אפשר גם להשתמש בשיטה get כדי לאחזר את כל האוסף.
Java
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Python
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Python
(Async)
Node.js
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Go
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
PHP
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
C#
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
Ruby
כדי לבצע אימות ב-Firestore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
השלבים הבאים
כדי להעמיק את הידע שלכם, כדאי לעיין בנושאים הבאים:
- מודל נתונים – מידע נוסף על מבנה הנתונים ב-Firestore, כולל נתונים היררכיים ותתי-אוספים.
- הוספת נתונים – מידע נוסף על יצירה ועדכון של נתונים ב-Firestore.
- קבלת נתונים – מידע נוסף על אחזור נתונים.
- איך מבצעים שאילתות פשוטות ומורכבות
- הזמנה והגבלה של שאילתות – כאן מוסבר איך להזמין ולהגביל את הנתונים שמוחזרים על ידי השאילתות.