Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const (
// DefaultProjectID is the default project ID for the BigTable container.
DefaultProjectID = "test-project"
)
Variables ¶
View Source
var WithProjectID = shared.WithProjectID
WithProjectID re-exports the common GCloud WithProjectID option
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
testcontainers.Container
// contains filtered or unexported fields
}
Container represents the BigTable container type used in the module
func Run ¶
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)
Run creates an instance of the BigTable GCloud container type. The URI uses the empty string as the protocol.
Example ¶
// runBigTableContainer {
ctx := context.Background()
bigTableContainer, err := tcbigtable.Run(
ctx,
"gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators",
tcbigtable.WithProjectID("bigtable-project"),
)
defer func() {
if err := testcontainers.TerminateContainer(bigTableContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to run container: %v", err)
return
}
// }
// bigTableAdminClient {
projectID := bigTableContainer.ProjectID()
const (
instanceID = "test-instance"
tableName = "test-table"
)
options := []option.ClientOption{
option.WithEndpoint(bigTableContainer.URI()),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
}
adminClient, err := bigtable.NewAdminClient(ctx, projectID, instanceID, options...)
if err != nil {
log.Printf("failed to create admin client: %v", err)
return
}
defer adminClient.Close()
// }
err = adminClient.CreateTable(ctx, tableName)
if err != nil {
log.Printf("failed to create table: %v", err)
return
}
err = adminClient.CreateColumnFamily(ctx, tableName, "name")
if err != nil {
log.Printf("failed to create column family: %v", err)
return
}
// bigTableClient {
client, err := bigtable.NewClient(ctx, projectID, instanceID, options...)
if err != nil {
log.Printf("failed to create client: %v", err)
return
}
defer client.Close()
// }
tbl := client.Open(tableName)
mut := bigtable.NewMutation()
mut.Set("name", "firstName", bigtable.Now(), []byte("Gopher"))
err = tbl.Apply(ctx, "1", mut)
if err != nil {
log.Printf("failed to apply mutation: %v", err)
return
}
row, err := tbl.ReadRow(ctx, "1", bigtable.RowFilter(bigtable.FamilyFilter("name")))
if err != nil {
log.Printf("failed to read row: %v", err)
return
}
fmt.Println(string(row["name"][0].Value))
Output: Gopher
Click to show internal directories.
Click to hide internal directories.