Melakukan streaming data menggunakan Storage Write API
Dokumen ini menjelaskan cara menggunakan BigQuery Storage Write API untuk melakukan streaming data ke BigQuery.
Dalam skenario streaming, data tiba secara terus-menerus dan harus tersedia untuk bacaan dengan latensi minimal. Saat menggunakan BigQuery Storage Write API untukworkload streaming, pertimbangkan jaminan yang Anda perlukan:
- Jika aplikasi Anda hanya memerlukan semantik setidaknya satu kali, gunakan streaming default.
- Jika Anda memerlukan semantik tepat satu kali, buat satu atau beberapa streaming dalam jenis yang di-commit dan gunakan offset streaming untuk menjamin penulisan tepat satu kali.
Dalam jenis yang di-commit, data yang ditulis ke streaming data tersedia untuk kueri segera setelah server mengonfirmasi permintaan tulis. Aliran data default juga menggunakan jenis yang di-commit, tetapi tidak memberikan jaminan tepat satu kali.
Menggunakan aliran data default untuk semantik minimal satu kali
Jika aplikasi Anda dapat menerima kemungkinan data duplikat muncul di tabel tujuan, sebaiknya gunakan streaming default untuk skenario streaming.
Kode berikut menunjukkan cara menulis data ke streaming data default:
Java
Untuk mempelajari cara menginstal dan menggunakan library klien untuk BigQuery, lihat library klien BigQuery. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Java BigQuery.
Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.
Menggunakan multiplexing
Anda mengaktifkan multiplexing pada level penulis streaming hanya untuk streaming default. Untuk mengaktifkan multiplexing di
Java, panggil metode setEnableConnectionPool
saat Anda membuat
objek StreamWriter
atau JsonStreamWriter
:
// One possible way for constructing StreamWriter StreamWriter.newBuilder(streamName) .setWriterSchema(protoSchema) .setEnableConnectionPool(true) .build(); // One possible way for constructing JsonStreamWriter JsonStreamWriter.newBuilder(tableName, bigqueryClient) .setEnableConnectionPool(true) .build();
Untuk mengaktifkan multiplexing di Go, lihat Connection Sharing (Multiplexing).
Menggunakan jenis yang di-commit untuk semantik tepat satu kali
Jika Anda memerlukan semantik penulisan tepat satu kali, buat streaming data tulis dalam jenis yang di-commit. Dalam jenis commit, data tersedia untuk kueri segera setelah klien menerima konfirmasi dari backend.
Jenis yang di-commit memberikan pengiriman tepat satu kali dalam streaming melalui penggunaan
offset data. Dengan menggunakan offset data, aplikasi akan menentukan offset penambahan
berikutnya di setiap panggilan ke AppendRows
. Operasi tulis
hanya dilakukan jika nilai offset cocok dengan offset penambahan berikutnya. Untuk mengetahui informasi
selengkapnya, lihat
Mengelola offset streaming untuk mencapai semantik tepat satu kali.
Jika Anda tidak memberikan offset, kumpulan data akan ditambahkan ke akhir streaming saat ini. Dalam hal ini, jika permintaan tambahkan menampilkan error, mencoba lagi dapat menyebabkan data muncul lebih dari sekali dalam streaming.
Untuk menggunakan jenis yang di-commit, lakukan langkah-langkah berikut:
- Panggil
CreateWriteStream
untuk membuat satu atau beberapa streaming dalam jenis yang di-commit. - Untuk setiap aliran data, panggil
AppendRows
dalam satu loop untuk menulis batch kumpulan data. - Panggil
FinalizeWriteStream
untuk setiap streaming guna merilis streaming. Setelah memanggil metode ini, Anda tidak dapat menulis baris lagi ke aliran data. Langkah ini bersifat opsional dalam jenis commit, tetapi membantu mencegah melampaui batas pada streaming aktif. Untuk mengetahui informasi selengkapnya, lihat Membatasi kecepatan pembuatan streaming.
Anda tidak dapat menghapus streaming secara eksplisit. Streaming mengikuti time to live (TTL) yang ditentukan sistem:
- Streaming yang di-commit memiliki TTL selama empat hari jika tidak ada traffic pada streaming.
- Streaming yang di-buffer secara default memiliki TTL selama tujuh hari jika tidak ada traffic pada streaming.
Kode berikut menunjukkan cara menggunakan jenis yang di-commit:
Java
Untuk mempelajari cara menginstal dan menggunakan library klien untuk BigQuery, lihat library klien BigQuery. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Java BigQuery.
Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.