Trasmetti il flusso di dati utilizzando l'API Storage Scrivi
Questo documento descrive come utilizzare API BigQuery StorageWrite per inviare flussi di dati in BigQuery.
Negli scenari in modalità flusso, i dati arrivano continuamente e dovrebbero essere disponibili legge con latenza minima. Quando utilizzi l'API BigQuery StorageWrite per i flussi di dati carichi di lavoro, valuta le garanzie di cui hai bisogno:
- Se la tua applicazione ha bisogno solo di semantica almeno una volta, utilizza il metodo predefinito streaming.
- Se hai bisogno di semantica "exactly-once", crea uno o più flussi in confermato e usare gli offset di flusso per garantire le scritture "exactly-once".
Nel tipo sottoposto a commit, i dati scritti nel flusso sono disponibili per la query non appena il server riconosce la richiesta di scrittura. Lo stream predefinito utilizza anche ma non fornisce garanzie "exactly-once".
Utilizza lo stream predefinito per la semantica almeno una volta
Se la tua applicazione accetta la possibilità di visualizzare record duplicati visualizzato nella tabella di destinazione, ti consigliamo di utilizzare stream predefinito per lo streaming diversi scenari.
Il codice seguente mostra come scrivere dati nel flusso predefinito:
Java
Per scoprire come installare e utilizzare la libreria client per BigQuery, consulta Librerie client di BigQuery. Per ulteriori informazioni, consulta API Java BigQuery documentazione di riferimento.
Per autenticarti a BigQuery, configura le credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per le librerie client.
Node.js
Per scoprire come installare e utilizzare la libreria client per BigQuery, consulta Librerie client di BigQuery.
Per autenticarti a BigQuery, configura le credenziali predefinite dell'applicazione. Per saperne di più, consulta Configurare l'autenticazione per le librerie client.
Utilizzare il multiplexing
Attiva
multiplexing
a livello di autore dello stream solo per lo stream predefinito. Per attivare il multiplexing in
Java, chiama il metodo setEnableConnectionPool
quando crei un
Oggetto StreamWriter
o 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();
Per attivare il multiplexing in Go, consulta Condivisione della connessione (multiplexing).
Utilizza il tipo di commit per la semantica "exactly-once"
Se hai bisogno di semantica della scrittura "exactly-once", crea un flusso di scrittura nel commit di testo. Con il tipo di commit, i record sono disponibili per la query non appena il client riceve conferma dal backend.
Il tipo di commit garantisce la consegna "exactly-once" all'interno di uno stream tramite l'utilizzo di offset dei record. Utilizzando gli offset di record, l'applicazione specifica
aggiungere l'offset in ogni chiamata a AppendRows
. L'operazione di scrittura
eseguita solo se il valore di offset corrisponde all'offset dell'aggiunta successiva. Per ulteriori informazioni
le informazioni, vedi
Gestisci gli offset dei flussi per ottenere una semantica "exactly-once".
Se non specifichi un offset, i record verranno aggiunti alla fine corrente di durante lo streaming. In questo caso, se una richiesta di aggiunta restituisce un errore, riprova il record potrebbe apparire più volte nello stream.
Per utilizzare il tipo di commit, segui questi passaggi:
Java
- Richiama
CreateWriteStream
per creare uno o più stream di tipo sottoposto a commit. - Per ogni stream, chiama
AppendRows
in un ciclo per scrivere batch di record. - Chiama
FinalizeWriteStream
per ogni stream per rilasciare lo stream. Dopo non puoi scrivere altre righe nel flusso. Questo passaggio è facoltativo nel tipo di commit, ma aiuta a evitare di superare il limite di stream attivi. Per ulteriori informazioni, consulta Limitare la frequenza di creazione dei flussi.
Node.js
- Richiama
createWriteStreamFullResponse
per creare uno o più stream di tipo sottoposto a commit. - Per ogni flusso, chiama
appendRows
in un loop per scrivere batch di record. - Chiama
finalize
per ogni stream per rilasciare lo stream. Dopo non puoi scrivere altre righe nel flusso. Questo passaggio è facoltativo nel tipo di commit, ma aiuta a evitare di superare il limite di stream attivi. Per ulteriori informazioni, vedi Limita la frequenza di creazione dello stream.
Non puoi eliminare uno stream in modo esplicito. Gli stream seguono la durata (TTL) definita dal sistema:
- Uno stream impegnato ha un TTL di tre giorni se non c'è traffico sullo stream.
- Per impostazione predefinita, uno stream con buffer ha un TTL di sette giorni se non c'è traffico nello stream.
Il seguente codice mostra come utilizzare il tipo di commit:
Java
Per scoprire come installare e utilizzare la libreria client per BigQuery, consulta Librerie client di BigQuery. Per ulteriori informazioni, consulta la documentazione di riferimento dell'API BigQuery Java.
Per eseguire l'autenticazione su BigQuery, configura Credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per le librerie client.
Node.js
Per scoprire come installare e utilizzare la libreria client per BigQuery, consulta Librerie client di BigQuery.
Per autenticarti a BigQuery, configura le credenziali predefinite dell'applicazione. Per saperne di più, consulta Configurare l'autenticazione per le librerie client.