bigquery

package
v0.40.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultProjectID is the default project ID for the BigQuery container.
	DefaultProjectID = "test-project"
)

Variables

View Source
var WithProjectID = shared.WithProjectID

WithProjectID re-exports the common GCloud WithProjectID option

Functions

func WithDataYAML

WithDataYAML seeds the BigQuery project for the GCloud container with an io.Reader representing the data yaml file, which is used to copy the file to the container, and then processed to seed the BigQuery project.

Other GCloud containers will ignore this option.

Types

type Container

type Container struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

Container represents the BigQuery container type used in the module

func Run

Run creates an instance of the BigQuery GCloud container type. The URI uses http:// as the protocol.

Example
// runBigQueryContainer {
ctx := context.Background()

bigQueryContainer, err := tcbigquery.Run(
	ctx,
	"ghcr.io/goccy/bigquery-emulator:0.6.1",
	tcbigquery.WithProjectID("bigquery-project"),
)
defer func() {
	if err := testcontainers.TerminateContainer(bigQueryContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to run container: %v", err)
	return
}
// }

// bigQueryClient {
projectID := bigQueryContainer.ProjectID()

opts := []option.ClientOption{
	option.WithEndpoint(bigQueryContainer.URI()),
	option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
	option.WithoutAuthentication(),
	internaloption.SkipDialSettingsValidation(),
}

client, err := bigquery.NewClient(ctx, projectID, opts...)
if err != nil {
	log.Printf("failed to create bigquery client: %v", err)
	return
}
defer client.Close()
// }

createFnQuery := client.Query("CREATE FUNCTION testr(arr ARRAY<STRUCT<name STRING, val INT64>>) AS ((SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem))")
_, err = createFnQuery.Read(ctx)
if err != nil {
	log.Printf("failed to create function: %v", err)
	return
}

selectQuery := client.Query("SELECT testr([STRUCT<name STRING, val INT64>(\"foo\", 10), STRUCT<name STRING, val INT64>(\"bar\", 40), STRUCT<name STRING, val INT64>(\"foo\", 20)])")
it, err := selectQuery.Read(ctx)
if err != nil {
	log.Printf("failed to read query: %v", err)
	return
}

var val []bigquery.Value
for {
	err := it.Next(&val)
	if errors.Is(err, iterator.Done) {
		break
	}
	if err != nil {
		log.Printf("failed to iterate: %v", err)
		return
	}
}

fmt.Println(val)
Output:

[30]

func (*Container) ProjectID

func (c *Container) ProjectID() string

ProjectID returns the project ID of the BigQuery container.

func (*Container) URI

func (c *Container) URI() string

URI returns the URI of the BigQuery container.

type Option

type Option = shared.Option

Option aliases the common GCloud option type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL