### Starting and Stopping the Server Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Demonstrates the lifecycle of the BigQuery emulator server, including starting, serving requests, and stopping gracefully. ```Go s := server.New(server.NewMemoryStorage()) go s.Serve() // ... later ... s.Stop() s.Close() ``` -------------------------------- ### Install BigQuery Emulator Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/00-README.md Install the BigQuery emulator as a Go library or via Docker. ```bash # Go library go get github.com/goccy/bigquery-emulator # Docker docker pull ghcr.io/goccy/bigquery-emulator:latest ``` -------------------------------- ### Data Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/types-complete.md Example demonstrating how to initialize the `Data` type, which is a slice of maps representing table rows. ```go data := types.Data{ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}, } ``` -------------------------------- ### Install BigQuery Emulator CLI Source: https://github.com/goccy/bigquery-emulator/blob/main/README.md Install the latest version of the bigquery-emulator CLI using Go. This method requires Go to be installed on your system. ```console $ go install github.com/goccy/bigquery-emulator/cmd/bigquery-emulator@latest ``` -------------------------------- ### Create BigQuery Column Examples Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Provides examples for creating columns, including simple columns, columns with modes, STRUCT columns with nested fields, and ARRAY columns. ```go // Simple column col := types.NewColumn("age", types.INT64) // Column with mode col := types.NewColumn("score", types.FLOAT64, types.ColumnMode(types.RequiredMode)) // STRUCT column with fields person := types.NewColumn("person", types.STRUCT, types.ColumnFields( types.NewColumn("first_name", types.STRING), types.NewColumn("last_name", types.STRING), types.NewColumn("birth_date", types.DATE), )) // ARRAY column tags := types.NewColumn("tags", types.STRING, types.ColumnMode(types.RepeatedMode)) ``` -------------------------------- ### Create BigQuery Project Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Demonstrates how to create a new BigQuery project with datasets using the types package. ```go project := types.NewProject("my-project", types.NewDataset("dataset1"), types.NewDataset("dataset2"), ) ``` -------------------------------- ### Example: Enable Debug Logging Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Demonstrates how to enable debug logging for the BigQuery emulator server. ```go bqServer, _ := server.New(server.TempStorage) // Enable debug logging if err := bqServer.SetLogLevel(server.LogLevelDebug); err != nil { panic(err) } ``` -------------------------------- ### Create BigQuery Dataset Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Demonstrates how to create a new BigQuery dataset with tables using the types package. ```go dataset := types.NewDataset("analytics", types.NewTable("events", columns, data), types.NewTable("users", columns, data), ) ``` -------------------------------- ### Create BigQuery Table Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Demonstrates how to create a new BigQuery table with columns, specifying types and modes, and initial data. ```go table := types.NewTable("users", []*types.Column{ types.NewColumn("user_id", types.INT64, types.ColumnMode(types.RequiredMode)), types.NewColumn("email", types.STRING), types.NewColumn("created_at", types.TIMESTAMP), }, types.Data{ {"user_id": 1, "email": "alice@example.com", "created_at": "2024-01-01T00:00:00Z"}, {"user_id": 2, "email": "bob@example.com", "created_at": "2024-01-02T00:00:00Z"}, }, ) ``` -------------------------------- ### Start BigQuery Emulator with Initial Data from YAML Source: https://github.com/goccy/bigquery-emulator/blob/main/README.md Start the bigquery-emulator server and load initial data from a specified YAML file. This is useful for setting up a predictable state for testing or development. ```console $ ./bigquery-emulator --project=test --data-from-yaml=./server/testdata/data.yaml [bigquery-emulator] REST server listening at 0.0.0.0:9050 [bigquery-emulator] gRPC server listening at 0.0.0.0:9060 ``` -------------------------------- ### Custom Storage Value Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/types-complete.md Example of creating a custom Storage value with a specific SQLite connection URI. ```go // Custom SQLite connection URI server.Storage("file:/path/to/db.sqlite?cache=shared") ``` -------------------------------- ### Basic Server Setup in Go Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/00-README.md Demonstrates how to create a new BigQuery emulator server instance with temporary storage and set a project ID. Ensure to close the server and test server when done. ```go import ( "github.com/goccy/bigquery-emulator/server" "github.com/goccy/bigquery-emulator/types" ) // Create server bqServer, _ := server.New(server.TempStorage) def bqServer.Close() // Setup project bqServer.SetProject("my-project") // Create test server for BigQuery client testServer := bqServer.TestServer() def testServer.Close() ``` -------------------------------- ### Cross-Reference Example: Import Path Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Shows the format for referencing Go package import paths. ```Markdown github.com/goccy/bigquery-emulator/package ``` -------------------------------- ### Start BigQuery Emulator Server Source: https://github.com/goccy/bigquery-emulator/blob/main/README.md Launch the BigQuery emulator executable. Specify the project and dataset to be used. ```bash $ ./bigquery-emulator --project=test --dataset=dataset1 [bigquery-emulator] REST server listening at 0.0.0.0:9050 [bigquery-emulator] gRPC server listening at 0.0.0.0:9060 ``` -------------------------------- ### Type to TypeKind Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Shows how to get the zsqltypes.TypeKind from a BigQuery Type constant. ```go kind := types.INT64.TypeKind() // zsqltypes.INT64 ``` -------------------------------- ### Get BigQuery Storage API Client Options Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/server.md Returns `option.ClientOption` slices for BigQuery Storage API clients. Ensures proper gRPC connection setup for storage operations. ```go func (ts *TestServer) GRPCClientOptions(ctx context.Context) ([]option.ClientOption, error) ``` ```go opts, err := testServer.GRPCClientOptions(ctx) readClient := bigquery_storage.NewBigQueryReadClient(opts...) ``` -------------------------------- ### Example: Use JSON Logging Format Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Demonstrates how to set the logging format to JSON for the BigQuery emulator server. ```go // Use JSON logging if err := bqServer.SetLogFormat(server.LogFormatJSON); err != nil { panic(err) } ``` -------------------------------- ### Docker Configuration Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Shows how to run the BigQuery emulator in a Docker container, including volume mounts for persistent data. ```Shell docker run -d -p 9050:9050 -p 9060:9060 \ -v $(pwd)/data:/data \ goccy/bigquery-emulator:latest \ --data-from-yaml /data/data.yaml \ --host 0.0.0.0 \ --grpc-port 9060 \ --port 9050 ``` -------------------------------- ### Start Standalone BigQuery Emulator Server Source: https://github.com/goccy/bigquery-emulator/blob/main/README.md Start the bigquery-emulator server as a standalone process, specifying the project name. The server will listen on default ports for BigQuery API and gRPC storage API. ```console $ ./bigquery-emulator --project=test [bigquery-emulator] REST server listening at 0.0.0.0:9050 [bigquery-emulator] gRPC server listening at 0.0.0.0:9060 ``` -------------------------------- ### Submit Load Job Request Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Example JSON request for submitting a load job. Includes source URIs, destination table, format, schema definition, and write disposition. ```json { "jobReference": { "projectId": "my-project", "jobId": "load_abc123" }, "configuration": { "load": { "sourceUris": ["gs://bucket/file.csv"], "destinationTable": { "projectId": "my-project", "datasetId": "my-dataset", "tableId": "imported_table" }, "sourceFormat": "CSV", "schema": { "fields": [ {"name": "id", "type": "INTEGER"}, {"name": "name", "type": "STRING"} ] }, "autodetect": false, "skipLeadingRows": 1, "allowQuotedNewlines": true, "writeDisposition": "WRITE_TRUNCATE" } } } ``` -------------------------------- ### Submit Query Job Request Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Example JSON request for submitting a query job. Specifies job configuration, query details, and destination table settings. ```json { "jobReference": { "projectId": "my-project", "jobId": "job_abc123" }, "configuration": { "query": { "query": "SELECT * FROM dataset1.table1 WHERE id > 10", "destinationDataset": { "projectId": "my-project", "datasetId": "destination_dataset" }, "destinationTable": { "projectId": "my-project", "datasetId": "destination_dataset", "tableId": "result_table" }, "writeDisposition": "WRITE_TRUNCATE", "createDisposition": "CREATE_IF_NEEDED", "allowLargeResults": true, "useQueryCache": true, "useLegacySql": false, "maximumBillingTier": 100, "maximumBytesBilled": "1000000" } } } ``` -------------------------------- ### Loading Data from Structs Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Initializes the server with data defined in Go structs. This method allows for programmatic data setup. ```Go server.New( server.NewStructSource(myStructData), ) ``` -------------------------------- ### List Table Data Response Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Example JSON response when listing rows in a table. Includes total rows, row data, and pagination tokens. ```json { "kind": "bigquery#tableDataList", "etag": "...", "totalRows": "5", "rows": [ { "f": [ {"v": "1"}, {"v": "Alice"}, {"v": "2024-01-01"} ] } ], "pageToken": "..." } ``` -------------------------------- ### YAML Data Format Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md An example of the YAML structure used for loading data into the BigQuery emulator. It mirrors Go type definitions for projects, datasets, tables, columns, and data. ```yaml projects: - id: analytics-project datasets: - id: events tables: - id: page_views columns: - name: event_id type: INT64 mode: REQUIRED - name: user_id type: INT64 - name: event_time type: TIMESTAMP - name: page_url type: STRING - name: properties type: STRING mode: REPEATED data: - event_id: 1 user_id: 100 event_time: "2024-01-15T10:30:00Z" page_url: "https://example.com/home" properties: - utm_source=google - utm_medium=cpc - id: users columns: - name: user_id type: INT64 mode: REQUIRED - name: email type: STRING - name: profile type: STRUCT fields: - name: first_name type: STRING - name: last_name type: STRING - name: birth_date type: DATE - name: tags type: STRING mode: REPEATED data: - user_id: 100 email: alice@example.com profile: first_name: Alice last_name: Smith birth_date: "1990-05-15" tags: - premium - early_adopter models: - id: ml_model_v1 routines: - id: calculate_revenue ``` -------------------------------- ### Testing Configuration with Go API Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Go code example for setting up a test BigQuery emulator using MemoryStorage, configuring log level, project, and loading test data. Includes defer for test server cleanup. ```go bqServer, _ := server.New(server.MemoryStorage) bqServer.SetLogLevel(server.LogLevelWarn) bqServer.SetProject("test-project") bqServer.Load(server.StructSource( types.NewProject("test-project", types.NewDataset("test-dataset"), ), )) testServer := bqServer.TestServer() defer testServer.Close() ``` -------------------------------- ### Column FormatType Examples Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Demonstrates the usage of the FormatType method for basic, array, and struct column types. ```go col := types.NewColumn("age", types.INT64) fmt.Println(col.FormatType()) // "INT64" arrayCol := types.NewColumn("tags", types.STRING, types.ColumnMode(types.RepeatedMode)) fmt.Println(arrayCol.FormatType()) // "ARRAY" ``` -------------------------------- ### Type to FieldType Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Demonstrates obtaining a FieldType constant from a BigQuery Type. ```go ft := types.INT64.FieldType() // FieldInteger ``` -------------------------------- ### Create Initial Dataset Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Use the --dataset flag to specify an initial dataset to be created when the server starts. Additional datasets can be created via the REST API. ```bash ./bigquery-emulator --project=my-project --dataset=my-dataset ``` -------------------------------- ### List Datasets Response Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Example response for listing datasets in a project, including dataset details and pagination tokens. ```json { "kind": "bigquery#datasetList", "etag": "...", "datasets": [ { "kind": "bigquery#dataset", "id": "my-project:dataset1", "datasetReference": { "projectId": "my-project", "datasetId": "dataset1" }, "friendlyName": "", "location": "US" } ], "nextPageToken": "..." } ``` -------------------------------- ### Create Dataset Response Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md This is an example of a successful response (201) when creating a dataset. ```json { "kind": "bigquery#dataset", "etag": "...", "id": "my-project:my-dataset", "selfLink": "...", "datasetReference": { "projectId": "my-project", "datasetId": "my-dataset" }, "friendlyName": "", "description": "Optional dataset description", "defaultTableExpirationMs": "7776000000", "creationTime": "1234567890000", "lastModifiedTime": "1234567890000", "location": "US" } ``` -------------------------------- ### Create BigQuery Data Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Demonstrates how to define table data using the Data type, which is a slice of maps representing rows. ```go data := types.Data{ {"id": 1, "name": "Alice", "age": 30}, {"id": 2, "name": "Bob", "age": 25}, {"id": 3, "name": "Charlie", "age": nil}, // nullable column } ``` -------------------------------- ### Cross-Reference Example: File-Relative Link Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Shows how to create links between different Markdown files within the documentation. ```Markdown [configuration.md](configuration.md) ``` -------------------------------- ### Table Metadata Setup Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Populates the Metadata field of a Table struct by converting its schema to BigQuery v2 format. ```go func (t *Table) SetupMetadata(projectID, datasetID string) ``` -------------------------------- ### Initialize Custom File Storage Server Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Configure custom file storage for persistent data across restarts. Requires manual cleanup. Useful for development with persistent data or production emulator setups. ```go server, _ := server.New(server.Storage("file:/data/bigquery.db?cache=shared")) ``` -------------------------------- ### Docker Development Setup Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Docker command to run the BigQuery emulator for development. It maps ports, sets environment variables for project and dataset, mounts a local YAML file for data loading, and specifies log level. ```bash docker run -it \ --name bq-emulator \ -p 9050:9050 \ -p 9060:9060 \ -e BIGQUERY_EMULATOR_PROJECT=local \ -e BIGQUERY_EMULATOR_DATASET=dev \ -v ${PWD}/data.yaml:/data.yaml \ ghcr.io/goccy/bigquery-emulator:latest \ --data-from-yaml=/data.yaml \ --log-level=info ``` -------------------------------- ### Serve HTTP and gRPC Servers Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/server.md Starts the HTTP and gRPC servers. This method blocks until an error occurs or the context is cancelled, initiating a graceful shutdown. ```go func (s *Server) Serve(ctx context.Context, httpAddr, grpcAddr string) error ``` ```go ctx := context.Background() if err := bqServer.Serve(ctx, "0.0.0.0:9050", "0.0.0.0:9060"); err != nil { panic(err) } ``` -------------------------------- ### Setting Listen Callback Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Registers a callback function to be executed when the server starts listening. This is useful for performing actions immediately after the server is ready. ```Go server.New( server.NewMemoryStorage(), server.WithListenCallback(func() { fmt.Println("Server is listening!") }), ) ``` -------------------------------- ### TestServer.GRPCClientOptions Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/server.md Provides `option.ClientOption` slices pre-configured for BigQuery Storage API clients, ensuring correct gRPC connection setup. ```APIDOC ## func (ts *TestServer) GRPCClientOptions(ctx context.Context) ([]option.ClientOption, error) ### Description Returns `option.ClientOption` slices configured for the BigQuery Storage API clients with proper gRPC connection setup. ### Signature ```go func (ts *TestServer) GRPCClientOptions(ctx context.Context) ([]option.ClientOption, error) ``` ### Parameters #### Context - **ctx** (context.Context) - The context for the operation. ### Returns - `[]option.ClientOption`: Client options for use with BigQuery Storage clients. - `error`: Error if gRPC connection setup fails. ### Example ```go opts, err := testServer.GRPCClientOptions(ctx) readClient := bigquery_storage.NewBigQueryReadClient(opts...) ``` ``` -------------------------------- ### Serve Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/server.md Starts the HTTP and gRPC servers, blocking execution until an error occurs or the provided context is cancelled. This method is used to keep the server running. ```APIDOC ## Serve ### Description Starts the HTTP and gRPC servers and blocks until either encounters an error or receives a shutdown signal via context cancellation. ### Method `func (s *Server) Serve(ctx context.Context, httpAddr, grpcAddr string) error` ### Parameters #### Path Parameters (None) #### Query Parameters (None) #### Request Body (None) ### Parameters - **ctx** (context.Context) - Required - Context for lifecycle control (cancellation triggers graceful shutdown) - **httpAddr** (string) - Required - HTTP server address (host:port), use `:0` to request an OS-assigned port - **grpcAddr** (string) - Required - gRPC server address (host:port), use `:0` to request an OS-assigned port ### Returns: - `error`: Error if listener binding fails or if the server encounters a fatal error during operation ### Request Example ```go ctx := context.Background() if err := bqServer.Serve(ctx, "0.0.0.0:9050", "0.0.0.0:9060"); err != nil { panic(err) } ``` ### Response (None) ``` -------------------------------- ### Run BigQuery Emulator with Docker Source: https://github.com/goccy/bigquery-emulator/blob/main/README.md Starts the BigQuery emulator as a Docker container, exposing the necessary ports. Use this for quick local testing without cloud credentials. ```console $ docker run -it -p 9050:9050 -p 9060:9060 ghcr.io/goccy/bigquery-emulator:latest --project=test ``` -------------------------------- ### YAML Data Structure Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Illustrates the expected structure for defining BigQuery resources (projects, datasets, tables) in a YAML file for data loading. ```YAML projects: - project_id: my-gcp-project datasets: - dataset_id: my_dataset tables: - table_id: my_table schema: - name: id type: INTEGER - name: name type: STRING ``` -------------------------------- ### Create and Configure BigQuery Emulator Server Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/INDEX.md Demonstrates how to create a new server instance with temporary storage, set a project, and load data from a YAML file. ```go package main import ( "context" "log" "github.com/goccy/bigquery-emulator/server" ) func main() { ctx := context.Background() // Create a new server instance with temporary storage newServer, err := server.New(server.TempStorage) if err != nil { log.Fatalf("failed to create server: %v", err) } // Configure the server to use a specific project newServer.SetProject("my-project") // Load data from a YAML file data, err := server.YAMLSource("data.yaml") if err != nil { log.Fatalf("failed to load data from yaml: %v", err) } // Load the data into the server if err := newServer.Load(ctx, data); err != nil { log.Fatalf("failed to load data: %v", err) } // Start the server (in a real application, you'd likely run this in a goroutine) // For demonstration, we'll just show the setup. log.Println("Server configured and data loaded.") // To run the server, you would typically use Serve() and manage its lifecycle. // Example: go newServer.Serve() // defer newServer.Stop() } ``` -------------------------------- ### GET /projects Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Lists available projects. Supports pagination and result limits through query parameters. Returns a list of project objects. ```APIDOC ## GET /projects ### Description Lists projects. ### Method GET ### Endpoint /projects ### Query Parameters - **pageToken** (string) - Optional - Token for pagination. - **maxResults** (integer) - Optional - Maximum results. ### Response #### Success Response (200) List of project objects ``` -------------------------------- ### Run BigQuery Emulator with Docker Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Launches the BigQuery emulator in a Docker container, mapping ports and mounting volumes for data persistence and configuration. Use this command to start the emulator with specific project, dataset, and logging settings. ```bash docker run -it \ -p 9050:9050 \ -p 9060:9060 \ -v /path/to/data.yaml:/data/data.yaml \ -v /path/to/db:/data/db \ ghcr.io/goccy/bigquery-emulator:latest \ --project=my-project \ --dataset=my-dataset \ --port=9050 \ --grpc-port=9060 \ --log-level=info \ --log-format=json \ --database=/data/db/bq.db \ --data-from-yaml=/data/data.yaml ``` -------------------------------- ### Create and Configure BigQuery Emulator Server with Struct Source Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/INDEX.md Demonstrates creating a server, setting a project, and loading data directly from a Go struct. ```go package main import ( "context" "log" "github.com/goccy/bigquery-emulator/server" "github.com/goccy/bigquery-emulator/types" ) type MyData struct { Name string `bigquery:"name"` Age int `bigquery:"age"` } func main() { ctx := context.Background() // Create a new server instance with temporary storage newServer, err := server.New(server.TempStorage) if err != nil { log.Fatalf("failed to create server: %v", err) } // Configure the server to use a specific project newServer.SetProject("my-project") // Prepare data using StructSource projectData := &types.Project{ ID: "my-project", Datasets: []*types.Dataset{ { ID: "my-dataset", Tables: []*types.Table{ { ID: "my-table", Schema: []*types.Column{ { Name: "name", Type: types.STRING }, { Name: "age", Type: types.INT64 }, }, Rows: []map[string]interface{}{ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, }, }, }, }, }, } // Load the data into the server if err := newServer.Load(ctx, server.StructSource(projectData)); err != nil { log.Fatalf("failed to load data: %v", err) } log.Println("Server configured and data loaded from struct.") } ``` -------------------------------- ### Initiate Resumable Upload for Load Jobs Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Starts a resumable upload process for BigQuery load jobs. Requires specific headers to indicate the upload protocol and command. ```http POST /upload/bigquery/v2/projects/{projectId}/jobs Headers: - X-Goog-Upload-Protocol: resumable - X-Goog-Upload-Command: start ``` -------------------------------- ### Server Creation Options Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Demonstrates creating a server with different storage types: in-memory, persistent, or from YAML. Use this to initialize the emulator with your desired storage backend. ```Go server.New( server.NewMemoryStorage(), ) server.New( server.NewPersistentStorage("/path/to/data"), ) server.New( server.NewYAMLSource("path/to/data.yaml"), ) ``` -------------------------------- ### Example Table Schema and Data in YAML Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Defines a table with various BigQuery data types and provides sample data, including nested structures and arrays. This is useful for setting up initial emulator state. ```yaml tables: - id: example_types columns: - name: int_col type: INT64 - name: bool_col type: BOOL - name: float_col type: FLOAT64 - name: string_col type: STRING - name: bytes_col type: BYTES - name: date_col type: DATE - name: datetime_col type: DATETIME - name: time_col type: TIME - name: timestamp_col type: TIMESTAMP - name: numeric_col type: NUMERIC - name: json_col type: JSON - name: array_col type: STRING mode: REPEATED - name: struct_col type: STRUCT fields: - name: nested_field type: STRING data: - int_col: 42 bool_col: true float_col: 3.14 string_col: "hello" bytes_col: "aGVsbG8=" date_col: "2024-01-15" datetime_col: "2024-01-15T10:30:45" time_col: "10:30:45.123456" timestamp_col: "2024-01-15T10:30:45Z" numeric_col: "123.456" json_col: '{"key": "value"}' array_col: - item1 - item2 struct_col: nested_field: "nested value" ``` -------------------------------- ### Insert Table Data Response Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Example JSON response for a streaming insert operation, indicating success or detailing any insertion errors. ```json { "kind": "bigquery#tableDataInsertAllResponse", "insertErrors": [ { "index": 0, "errors": [ { "reason": "invalid", "message": "Field 'id' must be non-null" } ] } ] } ``` -------------------------------- ### Create a Test Server Instance Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/INDEX.md Shows how to create a test server instance, which is useful for integration testing with the BigQuery client SDK. ```go package main import ( "log" "github.com/goccy/bigquery-emulator/server" ) func main() { // Create a test server instance testServer := server.TestServer() // The testServer can now be used with BigQuery client SDKs. // For example, to get gRPC dial options: grpcOptions := testServer.GRPCClientOptions() log.Println("Test server created successfully.") _ = grpcOptions // Use grpcOptions as needed } ``` -------------------------------- ### GET /projects/{projectId}/serviceAccount Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Gets the service account for a project. This endpoint retrieves the service account associated with a specific project ID. ```APIDOC ## GET /projects/{projectId}/serviceAccount ### Description Gets the service account for a project. ### Method GET ### Endpoint /projects/{projectId}/serviceAccount ### Response #### Success Response (200) - **kind** (string) - The type of the resource. - **email** (string) - The email address of the service account. ### Response Example ```json { "kind": "bigquery#getServiceAccountResponse", "email": "test-project@bigquery-emulator.iam.gserviceaccount.com" } ``` ``` -------------------------------- ### Import Paths for BigQuery Emulator Server and Types Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/types-complete.md Demonstrates how to import and use the server and types packages from the BigQuery emulator. Shows initialization of server, project, dataset, and table objects. ```go import ( "github.com/goccy/bigquery-emulator/server" "github.com/goccy/bigquery-emulator/types" ) // Server types s, err := server.New(server.TempStorage) // Data types project := types.NewProject("my-project") dataset := types.NewDataset("my-dataset") table := types.NewTable("my-table", columns, data) ``` -------------------------------- ### Initialize and Use BigQuery Emulator in Go Source: https://github.com/goccy/bigquery-emulator/blob/main/README.md This snippet demonstrates how to initialize the BigQuery emulator, load data, set the project, create a BigQuery client pointing to the emulator, define a SQL function, run it, and then query the function with sample data. Use this for setting up end-to-end tests or local development environments. ```go package main import ( "context" "fmt" "cloud.google.com/go/bigquery" "github.com/goccy/bigquery-emulator/server" "github.com/goccy/bigquery-emulator/types" "google.golang.org/api/iterator" "google.golang.org/api/option" ) func main() { ctx := context.Background() const ( projectID = "test" datasetID = "dataset1" routineID = "routine1" ) bqServer, err := server.New(server.TempStorage) if err != nil { panic(err) } if err := bqServer.Load( server.StructSource( types.NewProject( projectID, types.NewDataset( datasetID, ), ), ), ); err != nil { panic(err) } if err := bqServer.SetProject(projectID); err != nil { panic(err) } testServer := bqServer.TestServer() defer testServer.Close() client, err := bigquery.NewClient( ctx, projectID, option.WithEndpoint(testServer.URL), option.WithoutAuthentication(), ) if err != nil { panic(err) } defer client.Close() routineName, err := client.Dataset(datasetID).Routine(routineID).Identifier(bigquery.StandardSQLID) if err != nil { panic(err) } sql := fmt.Sprintf( ` CREATE FUNCTION %s( arr ARRAY> ) AS ( (SELECT SUM(IF(elem.name = "foo",elem.val,null)) FROM UNNEST(arr) AS elem) )`, routineName ) job, err := client.Query(sql).Run(ctx) if err != nil { panic(err) } status, err := job.Wait(ctx) if err != nil { panic(err) } if err := status.Err(); err != nil { panic(err) } it, err := client.Query(fmt.Sprintf( ` SELECT %s([ STRUCT("foo", 10), STRUCT("bar", 40), STRUCT("foo", 20) ])`, routineName )).Read(ctx) if err != nil { panic(err) } var row []bigquery.Value if err := it.Next(&row); err != nil { if err == iterator.Done { return } panic(err) } fmt.Println(row[0]) // 30 } ``` -------------------------------- ### Insert Table Data Request Example Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Example JSON request for inserting multiple rows into a table using streaming inserts. Supports insert IDs and optional error handling. ```json { "rows": [ { "insertId": "row1", "json": { "id": 1, "name": "Alice", "tags": ["user", "admin"] } }, { "insertId": "row2", "json": { "id": 2, "name": "Bob", "tags": ["user"] } } ], "templateSuffix": "", "skipInvalidRows": false } ``` -------------------------------- ### Initialize Server with Storage Options Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Create a new BigQuery emulator server instance. Choose between in-memory SQLite, temporary file storage, or a custom database file path. ```go server, err := server.New(server.MemoryStorage) server, err := server.New(server.TempStorage) server, err := server.New(server.Storage("file:/path/to/db.db?cache=shared")) server, err := server.New(server.Storage("file:mydb?mode=memory&cache=shared")) ``` -------------------------------- ### GET /$discovery/rest Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md An alternative discovery endpoint for accessing API information. ```APIDOC ## GET /$discovery/rest ### Description Alternative discovery endpoint. ### Method GET ### Endpoint /$discovery/rest ``` -------------------------------- ### Create New BigQuery Emulator Server Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/server.md Initializes a new BigQuery emulator server instance using either temporary file storage or in-memory storage. Ensure to close the server when done. ```go package main import ( "github.com/goccy/bigquery-emulator/server" ) func main() { // Use temporary storage bqServer, err := server.New(server.TempStorage) if err != nil { panic(err) } defer bqServer.Close() // Or use in-memory storage bqServer, err := server.New(server.MemoryStorage) if err != nil { panic(err) } } ``` -------------------------------- ### GET /projects/{projectId}/jobs Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Lists all jobs within a specified project. ```APIDOC ## GET /projects/{projectId}/jobs ### Description Lists jobs in a project. ### Method GET ### Endpoint /projects/{projectId}/jobs ### Query Parameters #### Query Parameters - **projection** (string) - Optional - `full` (default) or `minimal` — fields to include - **stateFilter** (string) - Optional - Comma-separated job states: `pending`, `running`, `done` - **pageToken** (string) - Optional - Token for pagination - **maxResults** (integer) - Optional - Maximum results - **allUsers** (boolean) - Optional - If true, list all users' jobs ### Response #### Success Response (200) List of job objects ``` -------------------------------- ### Minimal Production Server CLI Configuration Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Command-line arguments for setting up a minimal production BigQuery emulator. Specifies project, database path, and error logging. ```bash ./bigquery-emulator \ --project=production \ --database=/data/bq.db \ --log-level=error ``` -------------------------------- ### GetWriteStream Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Gets the current state of a write stream using the BigQuery Storage Write API. ```APIDOC ## GetWriteStream ### Description Gets the current state of a write stream. ### Method `rpc GetWriteStream(GetWriteStreamRequest) returns (WriteStream)` ``` -------------------------------- ### BigQuery Emulator CLI Help Source: https://github.com/goccy/bigquery-emulator/blob/main/README.md Display the help message for the bigquery-emulator CLI, showing available options and their default values. This is useful for understanding configuration parameters. ```console $ ./bigquery-emulator -h Usage: bigquery-emulator [OPTIONS] Application Options: --project= specify the project name --dataset= specify the dataset name --port= specify the http port number. this port used by bigquery api (default: 9050) --grpc-port= specify the grpc port number. this port used by bigquery storage api (default: 9060) --log-level= specify the log level (debug/info/warn/error) (default: error) --log-format= specify the log format (console/json) (default: console) --database= specify the database file if required. if not specified, it will be on memory --data-from-yaml= specify the path to the YAML file that contains the initial data -v, --version print version Help Options: -h, --help Show this help message ``` -------------------------------- ### GET /projects/{projectId}/jobs/{jobId} Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Retrieves details for a specific job using its ID. ```APIDOC ## GET /projects/{projectId}/jobs/{jobId} ### Description Gets a specific job. ### Method GET ### Endpoint /projects/{projectId}/jobs/{jobId} ### Response #### Success Response (200) Job object ### Errors - `404 NotFound` — Job does not exist ``` -------------------------------- ### Create BigQuery Project Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/types.md Use NewProject to create a new project instance with a given ID and optionally add datasets. ```Go import "github.com/goccy/bigquery-emulator/types" dataset := types.NewDataset("my-dataset") project := types.NewProject("my-project", dataset) ``` -------------------------------- ### Display Version Information Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Use the --version or -v flag to print the emulator's version and revision, then exit. ```bash ./bigquery-emulator --version ``` -------------------------------- ### Initialize BigQuery Client in Python Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/INDEX.md Sets up a BigQuery client instance for interacting with the emulator using Python. Specify the project, use anonymous credentials, and set the API endpoint to the emulator's address. ```python client = bigquery.Client( project="my-project", credentials=AnonymousCredentials(), client_options=ClientOptions(api_endpoint="http://localhost:9050"), ) job = client.query("SELECT 1") ``` -------------------------------- ### Cross-Reference Example: Section Anchor Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Demonstrates linking to specific sections within a Markdown file using anchors. ```Markdown #section-name ``` -------------------------------- ### Configuration Options Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/INDEX.md Details on configuration options for the standalone BigQuery emulator server and the Go library, including command-line flags, environment variables, and Go API methods. ```APIDOC ## Configuration Options (`configuration.md`) ### Description Details on configuration options for the standalone BigQuery emulator server and the Go library. This covers command-line flags, environment variables, Go API configuration methods, data loading via YAML, and storage options. ### Covers: - Command-line flags for standalone server: `--project`, `--dataset`, `--host`, `--port`, `--grpc-port`, `--log-level`, `--log-format`, `--database`, `--data-from-yaml` - Environment variables: `BIGQUERY_EMULATOR_PROJECT`, `BIGQUERY_EMULATOR_DATASET` - Go API configuration methods: `SetLogLevel()`, `SetLogFormat()`, `SetProject()`, `SetListenCallback()` - Data loading via `Load()` with source functions - YAML file format with complete examples - Docker configuration and volume mounts - Storage options comparison (MemoryStorage, TempStorage, file-based) - Runtime limits and built-in constraints - Configuration best practices and example setups ``` -------------------------------- ### Register or Initialize a Project Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Set a specific project ID for the BigQuery emulator. This is used for registering or initializing projects. ```go if err := bqServer.SetProject("my-project"); err != nil { panic(err) } ``` -------------------------------- ### GET /discovery/v1/apis/bigquery/v2/rest Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Returns the BigQuery API discovery document. This is useful for SDK discovery and understanding the API surface. ```APIDOC ## GET /discovery/v1/apis/bigquery/v2/rest ### Description Returns the BigQuery API discovery document (for SDK discovery). ### Method GET ### Endpoint /discovery/v1/apis/bigquery/v2/rest ### Response #### Success Response (200) - OpenAPI/discovery document in JSON ``` -------------------------------- ### GET /projects/{projectId}/datasets/{datasetId} Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Retrieves the details of a specific dataset identified by its project and dataset ID. ```APIDOC ## GET /projects/{projectId}/datasets/{datasetId} ### Description Gets a specific dataset. ### Method GET ### Endpoint /projects/{projectId}/datasets/{datasetId} ### Response #### Success Response (200) Full dataset object ### Errors - `404 NotFound` — Dataset does not exist - `500 InternalError` — Database error ``` -------------------------------- ### NewProject Constructor Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/types-complete.md Constructor for creating a new `Project` instance. Requires a project ID and an optional list of datasets. ```go func NewProject(id string, datasets ...*Dataset) *Project ``` -------------------------------- ### Project File Organization Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/00-README.md This snippet outlines the directory structure of the BigQuery Emulator project's documentation. ```tree output/ ├── 00-README.md ← You are here ├── INDEX.md ← Start here for navigation ├── MANIFEST.md ← Documentation inventory ├── api-reference/ │ ├── server.md ← Server package API │ └── types.md ← Types package API ├── endpoints.md ← HTTP & gRPC endpoints ├── errors.md ← Error catalog ├── configuration.md ← Configuration options └── types-complete.md ← Complete type reference ``` -------------------------------- ### POST /upload/bigquery/v2/projects/{projectId}/jobs Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Initiates a resumable upload for load jobs. This endpoint starts the resumable upload process. ```APIDOC ## POST /upload/bigquery/v2/projects/{projectId}/jobs ### Description Initiates a resumable upload (for load jobs). ### Method POST ### Endpoint /upload/bigquery/v2/projects/{projectId}/jobs ### Headers - **X-Goog-Upload-Protocol** (string) - Required - Resumable upload protocol - **X-Goog-Upload-Command** (string) - Required - Start upload ### Response #### Success Response (200) - Location header contains upload URI ``` -------------------------------- ### Create Test Server for BigQuery Client Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/api-reference/server.md Creates a test server compatible with Go's net/http/httptest and BigQuery client library. Use this for in-process testing of BigQuery interactions. ```go func (s *Server) TestServer() *TestServer ``` ```go package main import ( "context" "cloud.google.com/go/bigquery" "google.golang.org/api/option" "github.com/goccy/bigquery-emulator/server" "github.com/goccy/bigquery-emulator/types" ) bqServer, _ := server.New(server.TempStorage) bqServer.SetProject("test") testServer := bqServer.TestServer() defer testServer.Close() // Use with BigQuery client ctx := context.Background() client, err := bigquery.NewClient( ctx, "test", option.WithEndpoint(testServer.URL), option.WithoutAuthentication(), ) if err != nil { panic(err) } defer client.Close() // Now you can use the client with the emulator job, err := client.Query("SELECT 1 as value").Run(ctx) ``` -------------------------------- ### GET /projects/{projectId}/datasets/{datasetId}/tables/{tableId} Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Retrieves the schema and metadata for a specific table within a dataset. ```APIDOC ## GET /projects/{projectId}/datasets/{datasetId}/tables/{tableId} ### Description Gets a specific table's schema and metadata. ### Method GET ### Endpoint /projects/{projectId}/datasets/{datasetId}/tables/{tableId} ### Response #### Success Response (200) Full table object with schema ### Errors - `404 NotFound` — Table does not exist ``` -------------------------------- ### Loading Data from YAML Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/MANIFEST.md Initializes the server with data defined in a YAML file. Ensure the YAML file adheres to the expected schema for data loading. ```Go server.New( server.NewYAMLSource("path/to/data.yaml"), ) ``` -------------------------------- ### GET /projects/{projectId}/datasets Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/endpoints.md Lists all datasets within a specified project. Supports pagination and limiting the number of results. ```APIDOC ## GET /projects/{projectId}/datasets ### Description Lists all datasets in a project. ### Method GET ### Endpoint /projects/{projectId}/datasets ### Query Parameters - **pageToken** (string) - Optional - Token for pagination. - **maxResults** (integer) - Optional - Maximum results to return (default varies). - **filter** (string) - Optional - Filter expression (currently unsupported). ### Response #### Success Response (200) - **kind** (string) - The type of resource. - **etag** (string) - The etag of the dataset list. - **datasets** (array) - An array of dataset resource objects. - **kind** (string) - The type of resource. - **id** (string) - The unique ID of the dataset. - **datasetReference** (object) - Reference to the dataset. - **projectId** (string) - The ID of the project. - **datasetId** (string) - The ID of the dataset. - **friendlyName** (string) - The friendly name of the dataset. - **location** (string) - The location of the dataset. - **nextPageToken** (string) - Token for the next page of results. #### Response Example ```json { "kind": "bigquery#datasetList", "etag": "...", "datasets": [ { "kind": "bigquery#dataset", "id": "my-project:dataset1", "datasetReference": { "projectId": "my-project", "datasetId": "dataset1" }, "friendlyName": "", "location": "US" } ], "nextPageToken": "..." } ``` ``` -------------------------------- ### Load Initial Data from YAML Source: https://github.com/goccy/bigquery-emulator/blob/main/_autodocs/configuration.md Use the --data-from-yaml flag to load initial projects, datasets, and tables from a specified YAML file. ```bash ./bigquery-emulator --project=my-project --data-from-yaml=/config/data.yaml ``` -------------------------------- ### Build Docker Image for BigQuery Emulator Source: https://github.com/goccy/bigquery-emulator/blob/main/_examples/python/README.md Run this command to build the Docker image for the BigQuery emulator and its associated container. ```bash make setup ```