in bigquery/snippets/managedwriter/bigquerystorage_append_rows_default.go [107:202]
func appendToDefaultStream(w io.Writer, projectID, datasetID, tableID string) error {
// projectID := "myproject"
// datasetID := "mydataset"
// tableID := "mytable"
ctx := context.Background()
// Instantiate a managedwriter client to handle interactions with the service.
client, err := managedwriter.NewClient(ctx, projectID,
managedwriter.WithMultiplexing(), // Enables connection sharing.
)
if err != nil {
return fmt.Errorf("managedwriter.NewClient: %w", err)
}
// Close the client when we exit the function.
defer client.Close()
// We need to communicate the descriptor of the protocol buffer message we're using, which
// is analagous to the "schema" for the message. Both SampleData and SampleStruct are
// two distinct messages in the compiled proto file, so we'll use adapt.NormalizeDescriptor
// to unify them into a single self-contained descriptor representation.
var m *exampleproto.SampleData
descriptorProto, err := adapt.NormalizeDescriptor(m.ProtoReflect().Descriptor())
if err != nil {
return fmt.Errorf("NormalizeDescriptor: %w", err)
}
// Build the formatted reference to the destination table.
tableReference := managedwriter.TableParentFromParts(projectID, datasetID, tableID)
// Instantiate a ManagedStream, which manages low level details like connection state and provides
// additional features like a future-like callback for appends, etc. Default streams are provided by
// the system, so there's no need to create them.
managedStream, err := client.NewManagedStream(ctx,
managedwriter.WithType(managedwriter.DefaultStream),
managedwriter.WithDestinationTable(tableReference),
managedwriter.WithSchemaDescriptor(descriptorProto),
)
if err != nil {
return fmt.Errorf("NewManagedStream: %w", err)
}
// Automatically close the writer when we're done.
defer managedStream.Close()
// First, we'll append a single row.
rows, err := generateExampleDefaultMessages(1)
if err != nil {
return fmt.Errorf("generateExampleMessages: %w", err)
}
// We can append data asyncronously, so we'll check our appends at the end.
var results []*managedwriter.AppendResult
result, err := managedStream.AppendRows(ctx, rows)
if err != nil {
return fmt.Errorf("AppendRows first call error: %w", err)
}
results = append(results, result)
// This time, we'll append three more rows in a single request.
rows, err = generateExampleMessages(3)
if err != nil {
return fmt.Errorf("generateExampleMessages: %w", err)
}
result, err = managedStream.AppendRows(ctx, rows)
if err != nil {
return fmt.Errorf("AppendRows second call error: %w", err)
}
results = append(results, result)
// Finally, we'll append two more rows.
rows, err = generateExampleMessages(2)
if err != nil {
return fmt.Errorf("generateExampleMessages: %w", err)
}
result, err = managedStream.AppendRows(ctx, rows)
if err != nil {
return fmt.Errorf("AppendRows third call error: %w", err)
}
results = append(results, result)
// We've been collecting references to our status callbacks to allow us to append in a faster
// asynchronous fashion. Normally you could do this in another goroutine or similar, but for
// this example we'll now iterate through those results and verify they were all successful.
for k, v := range results {
// GetResult blocks until we receive a response from the API.
recvOffset, err := v.GetResult(ctx)
if err != nil {
return fmt.Errorf("append %d returned error: %w", k, err)
}
fmt.Fprintf(w, "Successfully appended data at offset %d.\n", recvOffset)
}
// This stream is a default stream, which means it doesn't require any form of finalization
// or commit. The rows were automatically committed to the table.
return nil
}