bigq

package module
v0.0.0-...-3027a9b Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2016 License: MIT Imports: 9 Imported by: 0

README

go-bigq Build Status GoDoc

go-bigq is a wrapper to make querying BigQuery easier for the Go programming language.

Even though there is an official package to interact with BigQuery the API of that library is sort of arcane, so I ended up making a layer on top of it to make the experience of querying BigQuery more pleasant.

This package only performs reads, if you are looking for a way to write to BigQuery I recommend you go-bqstreamer.

Usage

// Create the service
service, err := bigq.New(bigq.WithConfigFile("/path/to/token.json"), bigq.Config{
	ProjectID: "my-project",
	DatasetID: "my-dataset",
})
handleErr(err)

// Perform the query starting at 0 index and with 100 results per page
q, err := service.Query("SELECT foo FROM bar WHERE baz", 0, 100)
handleErr(err)

// Get the next page of results
rows, err := q.NextPage()
handleErr(err)
doSomethingWith(rows)

Loop through results using an iterator

// Create the service
service, err := bigq.New(bigq.WithConfigFile("/path/to/token.json"), bigq.Config{
	ProjectID: "my-project",
	DatasetID: "my-dataset",
})
handleErr(err)

// Perform the query starting at 0 index and with 100 results per page
q, err := service.Query("SELECT foo FROM bar WHERE baz", 0, 100)
handleErr(err)


// Get iterator
iter := q.Iter()

var myStruct struct {
	FirstCol  string
	SecondCol int
	ThirdCol  float64
	FourthCol bool
}

for iter.Next(&myStruct) {
	use(myStruct)
}
handleErr(iter.Err())

TODO

  • Service method to perform queries with large results (queries whose results have to go to another table).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewJWTConfig

func NewJWTConfig(keyPath string) (*jwt.Config, error)

NewJWTConfig returns a JWT Configuration with the BigQuery scope and the data in the provided token file.

Types

type ClientOptions

type ClientOptions interface {
	// Service returns the client service
	Service() (*bigquery.Service, error)
}

ClientOptions will construct the client service based on the options it was passed.

func WithConfigFile

func WithConfigFile(path string) ClientOptions

WithConfigFile returns a ClientOptions that will construct the client service using a config file.

func WithJWTConfig

func WithJWTConfig(config *jwt.Config) ClientOptions

WithJWTConfig returns a ClientOptions that will construct the client service using the given jwt config.

type Config

type Config struct {
	DatasetID string
	ProjectID string
}

Config has some parameters that are needed for the configuration of the service.

type Iter

type Iter interface {
	// Next fetches the next row and fills the fields of the given
	// struct pointer with the columns of the row in appearance order.
	// For example, given:
	//  struct {
	//          Foo int
	//          Bar int
	//  }
	//
	// The row 1, 2 would result in struct{Foo: 1, Bar:2}
	// This method returns a boolean reporting if the operation was
	// successful.
	Next(interface{}) bool

	// Err returns the latest error that happened.
	Err() error
}

Iter is a structure to loop through query results.

type Query

type Query interface {
	// NextPage returns the next page of rows in the query resultset. It returns up
	// to the max results that were given to the query on its creation. Note that
	// BigQuery has a limit of 10MB, that is, when your page reaches the 10MB limit
	// it will yield and will not return the max number of results instead.
	NextPage() ([][]interface{}, error)

	// Iter returns an iterator to retrieve the query results.
	// Using this method sets the query in "iter" mode, that is,
	// the NextPage method can't be used after using Iter, but it
	// can be used before retrieving the iterator.
	Iter() Iter
}

Query contains all the context of a query execution and has methods to retrieve the rows in pages. The Query instance can be seen as a cursor, it can't go back and it can't re-read the same page again. It is not thread safe and it is not intended to be.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service instances will be able to make queries. A Service is basically a Query constructor that holds the connection with BigQuery.

func New

func New(clientOptions ClientOptions, config Config) (*Service, error)

New creates a new Service with the given client options and config.

func (*Service) Query

func (s *Service) Query(query string, args ...uint64) (Query, error)

Query creates a new query with the SQL sentence passed and a series of arguments. You can pass none, which means no additional parameters. The first parameter passed will be the start, that is, the offset in the resultset. The second parameter passed will be the max results allowed per page.

Jump to

Keyboard shortcuts

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