Membuat pipeline streaming menggunakan template Dataflow
Panduan memulai ini menunjukkan cara membuat pipeline streaming menggunakan template Dataflow yang disediakan oleh Google. Secara khusus, panduan memulai ini menggunakan template Pub/Sub to BigQuery sebagai contoh.
Template Pub/Sub to BigQuery adalah pipeline streaming yang dapat membaca pesan berformat JSON dari topik Pub/Sub dan menuliskannya ke tabel BigQuery.
Untuk mengikuti panduan langkah demi langkah untuk tugas ini langsung di Google Cloud konsol, klik Pandu saya:
Sebelum memulai
Selesaikan langkah-langkah berikut sebelum menjalankan pipeline Anda.
Menyiapkan project
- Sign in to your Google Cloud Platform 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.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataflow, Compute Engine, Cloud Logging, Cloud Storage, Google Cloud Storage JSON, BigQuery, Pub/Sub, and Resource Manager APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataflow, Compute Engine, Cloud Logging, Cloud Storage, Google Cloud Storage JSON, BigQuery, Pub/Sub, and Resource Manager APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Pengguna BigQuery (
roles/bigquery.user) -
Dataflow Admin (
roles/dataflow.admin) -
Service Account User (
roles/iam.serviceAccountUser) -
Storage Admin (
roles/storage.admin) -
BigQuery Data Editor (
roles/bigquery.dataEditor) -
Dataflow Worker (
roles/dataflow.worker) -
Pub/Sub Editor (
roles/pubsub.editor) -
Storage Object Admin (
roles/storage.objectAdmin) -
Pelihat (
roles/viewer) Membuat bucket Cloud Storage:
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click Create.
- On the Create a bucket page, enter your bucket information. To go to the next
step, click Continue.
- For Name your bucket, enter a unique bucket name. Don't include sensitive information in the bucket name, because the bucket namespace is global and publicly visible.
-
In the Choose where to store your data section, do the following:
- Select a Location type.
- Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
- If you select the dual-region location type, you can also choose to enable turbo replication by using the relevant checkbox.
- To set up cross-bucket replication, select
Add cross-bucket replication via Storage Transfer Service and
follow these steps:
Set up cross-bucket replication
- In the Bucket menu, select a bucket.
In the Replication settings section, click Configure to configure settings for the replication job.
The Configure cross-bucket replication pane appears.
- To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
- To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
- Click Done.
-
In the Choose how to store your data section, do the following:
- In the Set a default class section, select the following: Standard.
- To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
- In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
-
In the Choose how to protect object data section, do the
following:
- Select any of the options under Data protection that you
want to set for your bucket.
- To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
- To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
- To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
- To enable Object Retention Lock, click the Enable object retention checkbox.
- To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
- To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
- Select any of the options under Data protection that you
want to set for your bucket.
- Click Create.
Salin perintah berikut karena Anda akan memerlukannya di bagian selanjutnya:
- Nama bucket Cloud Storage Anda.
- ID project Google Cloud Anda.
Untuk menemukan ID ini, lihat Mengidentifikasi project.
Peran yang diperlukan
Untuk menyelesaikan panduan memulai ini, Anda memerlukan peran Identity and Access Management (IAM) berikut.
Untuk mendapatkan izin yang Anda perlukan untuk menyelesaikan panduan memulai cepat ini, minta administrator Anda untuk memberi Anda peran IAM berikut di project Anda:
Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.
Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.
Untuk memastikan bahwa akun layanan default Compute Engine memiliki izin yang diperlukan untuk menjalankan tugas Dataflow, minta administrator untuk memberi akun layanan default Compute Engine peran IAM berikut di project Anda:
Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.
Administrator Anda mungkin juga dapat memberikan izin yang diperlukan kepada akun layanan default Compute Engine melalui peran khusus atau peran bawaan lainnya.
Membuat bucket Cloud Storage
Sebelum dapat menjalankan pipeline, Anda harus membuat bucket Cloud Storage.
Jaringan VPC
Secara default, setiap project baru dimulai dengan jaringan
default. Jika jaringan default untuk project Anda dinonaktifkan atau dihapus, Anda harus memiliki jaringan di project Anda yang akun penggunanya memiliki peran Compute Network User (roles/compute.networkUser).
Membuat set data dan tabel BigQuery
Buat set data dan tabel BigQuery dengan skema yang sesuai untuk topik Pub/Sub menggunakan konsol Google Cloud .
Dalam contoh ini, nama set datanya adalah taxirides dan nama
tabelnya adalah realtime. Untuk membuat set data dan tabel ini, ikuti langkah-langkah berikut:
- Buka halaman BigQuery.
Buka BigQuery - Di panel Explorer, di samping project tempat Anda ingin membuat set data, klik View actions, lalu klik Create dataset.
- Di panel Create dataset, ikuti langkah-langkah berikut:
- Untuk Dataset ID, masukkan
taxirides. ID set data bersifat unik untuk setiap project Google Cloud . - Untuk Location type, pilih Multi-region, lalu pilih US (multiple regions in United States). Set data publik disimpan di
lokasi multi-region
US. Untuk mempermudah, simpan set data Anda di lokasi yang sama. - Tetap gunakan setelan default lainnya, lalu klik Create dataset
- Di panel
Explorer , luaskan project Anda. - Di samping set data
taxirides, klik Lihat tindakan, lalu klik Buat tabel. - Di panel Create table, ikuti langkah-langkah berikut:
- Di bagian Source, untuk Create table from, pilih Empty table.
- Di bagian Destination, untuk Table, masukkan
realtime. - Di bagian Schema, klik tombol Edit as text, lalu tempelkan
definisi skema berikut ke dalam kotak:
ride_id:string,point_idx:integer,latitude:float,longitude:float,timestamp:timestamp, meter_reading:float,meter_increment:float,ride_status:string,passenger_count:integer
- Di bagian Partition and cluster settings, untuk Partitioning, pilih kolom timestamp.
- Tetap gunakan setelan default lainnya, lalu klik Create table.
Menjalankan pipeline
Jalankan pipeline streaming menggunakan template Pub/Sub to BigQuery yang disediakan Google. Pipeline mendapatkan data masuk dari topik input.
- Buka halaman Tugas Dataflow.
Buka Tugas - Klik
Create job from template . - Masukkan
taxi-datasebagai Nama tugas untuk tugas Dataflow Anda. - Untuk Dataflow template, pilih template Pub/Sub to BigQuery.
- Untuk BigQuery output table, masukkan berikut ini:
PROJECT_ID:taxirides.realtime
Ganti
PROJECT_IDdengan project ID project tempat Anda membuat set data BigQuery. - Di bagian Optional source parameters, di kolom Input Pub/Sub topic, klik Enter topic manually.
- Dalam dialog, untuk Topic name, masukkan berikut ini, lalu klik Save:
projects/pubsub-public-data/topics/taxirides-realtime
Topik Pub/Sub yang tersedia secara publik ini didasarkan pada set data terbuka NYC Taxi & Limousine Commission. Berikut adalah contoh pesan dari topik ini, dalam format JSON:
{ "ride_id": "19c41fc4-e362-4be5-9d06-435a7dc9ba8e", "point_idx": 217, "latitude": 40.75399, "longitude": -73.96302, "timestamp": "2021-03-08T02:29:09.66644-05:00", "meter_reading": 6.293821, "meter_increment": 0.029003782, "ride_status": "enroute", "passenger_count": 1 }
- Untuk Lokasi sementara, masukkan yang berikut:
gs://BUCKET_NAME/temp/
Ganti
BUCKET_NAMEdengan nama bucket Cloud Storage Anda. Foldertempmenyimpan file sementara, seperti tugas pipeline yang di-stage. - Jika project Anda tidak memiliki jaringan default, masukkan Jaringan dan Subnetwork. Untuk mengetahui informasi selengkapnya, lihat Menentukan jaringan dan subnetwork.
- Klik Jalankan tugas.
Melihat hasil penelusuran Anda
Untuk melihat data yang ditulis ke tabelrealtime, ikuti langkah-langkah berikut:
Buka halaman BigQuery.
Klik Tulis kueri baru. Tab Editor baru akan terbuka.
SELECT * FROM `PROJECT_ID.taxirides.realtime` WHERE `timestamp` > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY) LIMIT 1000
Ganti
PROJECT_IDdengan project ID project tempat Anda membuat set data BigQuery. Mungkin perlu waktu hingga lima menit agar data mulai muncul di tabel Anda.Klik Run.
Kueri menampilkan baris yang telah ditambahkan ke tabel Anda dalam 24 jam terakhir. Anda juga dapat menjalankan kueri menggunakan SQL standar.
Pembersihan
Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, ikuti langkah-langkah berikut.
Menghapus project
Cara termudah untuk menghilangkan penagihan adalah dengan menghapus Google Cloud project yang Anda buat untuk panduan memulai.- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Menghapus resource satu per satu
Jika Anda ingin mempertahankan Google Cloud project yang Anda gunakan dalam panduan memulai ini, hapus masing-masing resource:
- Buka halaman Tugas Dataflow.
Buka Tugas - Pilih tugas streaming Anda dari daftar tugas.
- Di navigasi, klik Berhenti.
- Dalam dialog Stop job, batalkan atau hentikan pipeline Anda, lalu klik Stop job.
- Buka halaman BigQuery.
Buka BigQuery - Di panel Explorer, luaskan project Anda.
- Di samping set data yang ingin Anda hapus, klik Lihat tindakan, lalu klik Buka.
- Di panel detail, klik Hapus set data, lalu ikuti petunjuknya.
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click the checkbox for the bucket that you want to delete.
- To delete the bucket, click Delete, and then follow the instructions.