rockset

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2020 License: Apache-2.0 Imports: 8 Imported by: 15

README

Rockset Go Client

CircleCI Documentation License GitHub issues Release

Official Go client library for Rockset

Installation

Install the Rockset Go client from Github:

go get github.com/rockset/rockset-go-client

or install it from a source code checkout:

cd $GOPATH/src/github.com
mkdir rockset
cd rockset
git clone git@github.com:rockset/rockset-go-client.git
go install rockset-go-client/rockclient.go

Usage

You can see a few examples in the godoc of how to create a collection, how to put documents in a collection and how to use SQL to query your collections.

client, err := rockset.NewClient(rockset.WithAPIKey("..."))
if err != nil {
    log.Fatal(err)
}

q := models.QueryRequest{
    Sql: &models.QueryRequestSql{
        Query: `SELECT * FROM "_events" LIMIT 1`,
    },
}

res, _, err := client.Query(q)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("%+v\n", res)

Testing

Tests are available in the test folder.

Set ROCKSET_APIKEY and ROCKSET_APISERVER endpoint in the environment variables. To run tests:

go test ./...

Support

Feel free to log issues against this client through GitHub.

License

The Rockset Go Client is licensed under the Apache 2.0 License

Documentation

Overview

Package rockset provides a go client to interact with the the online Rockset database using the REST API https://docs.rockset.com/rest-api/

Example (AddDocument)
client, err := rockset.NewClient(rockset.FromEnv())
if err != nil {
	log.Fatal(err)
}

// document to be inserted
m := map[string]interface{}{"name": "foo"}

// array of documents
docs := []interface{}{
	m,
}

dinfo := models.AddDocumentsRequest{
	Data: docs,
}

res, _, err := client.Documents.Add("commons", "test-collection", dinfo)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("document status: %s\n", res.Data[0].Status)
Output:

document status: ADDED
Example (CreateCollection)
client, err := rockset.NewClient(rockset.FromEnv())
if err != nil {
	log.Fatal(err)
}

// create collection
createResp, _, err := client.Collection.Create("commons", models.CreateCollectionRequest{
	Name:        "go-test-collection",
	Description: "go test collection",
})
fmt.Printf("created collection %s\n", createResp.Data.Name)

// get collection
getResp, _, err := client.Collection.Get("commons", "go-test-collection")
if err != nil {
	log.Fatal(err)
}
fmt.Printf("collection status for %s: %s\n", getResp.Data.Name, getResp.Data.Status)

// delete collection
deleteResp, _, err := client.Collection.Delete("commons", "go-test-collection")
if err != nil {
	log.Fatal(err)
}

fmt.Printf("deleted collection %s\n", deleteResp.Data.Name)
Output:

created collection go-test-collection
collection status for go-test-collection: CREATED
deleted collection go-test-collection
Example (CreateIntegration)
client, err := rockset.NewClient(rockset.FromEnv())
if err != nil {
	log.Fatal(err)
}

// create integration
req := models.CreateIntegrationRequest{
	Name:        "my-first-integration",
	Description: "my first integration",
	Kinesis: &models.KinesisIntegration{
		AwsAccessKey: &models.AwsAccessKey{
			AwsAccessKeyId:     "...",
			AwsSecretAccessKey: "...",
		},
		AwsRole: &models.AwsRole{
			AwsRoleArn: "arn:aws:iam::<account-id>:role/<role-name>",
		},
	},
}

res, _, err := client.Integration.Create(req)
if err != nil {
	log.Fatal(err)
}

res.PrintResponse()
Output:

Example (CreateS3Collection)
client, err := rockset.NewClient(rockset.FromEnv())
if err != nil {
	log.Fatal(err)
}

// create collection
req := models.CreateCollectionRequest{
	Name:        "s3_collection",
	Description: "s3 collection",
	Sources: []models.Source{
		{
			IntegrationName: "my-first-integration",
			S3: &models.SourceS3{
				Bucket: "s3://<bucket-name>",
			},
		},
	},
}

resp, _, err := client.Collection.Create("commons", req)
if err != nil {
	fmt.Printf("error: %s\n", err)
	return
}

resp.PrintResponse()
Output:

Example (SavedQuery)

Example code to create, use, and delete a saved query

client, err := rockset.NewClient(rockset.FromEnv())
if err != nil {
	log.Fatal(err)
}

var defaultValue interface{} = "Hello, world!"

// construct request
createQuery := models.CreateSavedQueryRequest{
	Name:     "MySavedQuery",
	QuerySql: `SELECT :param AS echo`,
	Parameters: []models.SavedQueryParameter{
		{
			Name:         "param",
			Type_:        "string",
			DefaultValue: &defaultValue,
		},
	},
}

// create saved query
createResp, _, err := client.QueryApi.Create("commons", createQuery)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("created saved query %s\n", createResp.Data.Name)

// execute saved query with default parameters
runResp, _, err := client.QueryApi.Run("commons", "MySavedQuery", 1, nil)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("query result: %v\n", runResp.Results[0])

// execute saved query with explicit parameters
var customValue interface{} = "All work and no play makes Jack a dull boy"
q := models.RunOpts{
	Body: optional.NewInterface(models.ExecuteSavedQueryRequest{
		Parameters: []models.ExecuteSavedQueryParameter{
			{
				Name:  "param",
				Value: &customValue,
			},
		},
	}),
}

runResp, _, err = client.QueryApi.Run("commons", "MySavedQuery", 1, &q)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("query result: %v\n", runResp.Results[0])

// delete saved query
deleteResp, _, err := client.QueryApi.Delete("commons", "MySavedQuery")
if err != nil {
	log.Fatal(err)
}

fmt.Printf("deleted saved query %s\n", deleteResp.Data.Name)
Output:

created saved query MySavedQuery
query result: map[echo:Hello, world!]
query result: map[echo:All work and no play makes Jack a dull boy]
deleted saved query MySavedQuery

Index

Examples

Constants

View Source
const DefaultAPIServer = "https://api.rs2.usw2.rockset.com"

DefaultAPIServer is the default Rockset API server to use

View Source
const Version = "0.8.0"

Version is the Rockset client version

Variables

This section is empty.

Functions

This section is empty.

Types

type RockClient

type RockClient struct {

	// API Services
	ApiKeys     *api.ApiKeysApiService
	Collection  *api.CollectionsApiService
	Integration *api.IntegrationsApiService
	Documents   *api.DocumentsApiService
	QueryApi    *api.QueriesApiService
	Users       *api.UsersApiService
	// contains filtered or unexported fields
}
Example (QueryParam)
client, err := rockset.NewClient(rockset.FromEnv())
if err != nil {
	log.Fatal(err)
}

// construct query
q := models.QueryRequest{
	Sql: &models.QueryRequestSql{
		Query: `SELECT * FROM _events WHERE kind = :k LIMIT 1`,
		Parameters: []models.QueryParameter{
			{Name: "k", Type_: "string", Value: "QUERY"},
		},
	},
}

// execute query
res, _, err := client.Query(q)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("got %d response(s)\n", len(res.Results))
Output:

got 1 response(s)

func Client deprecated

func Client(apiKey string, apiServer string) *RockClient

Create a Client object to securely connect to Rockset using an API key Optionally, an alternate API server host can also be provided.

Deprecated: this function has been superseded by NewClient()

func NewClient added in v0.8.0

func NewClient(options ...RockOption) (*RockClient, error)

NewClient creates a new Rockset client.

Accessing the online database requires an API key, which you either have to supply through the ROCKSET_APIKEY environment variable and pass the FromEnv() option

c, err := rockset.NewClient(rockset.FromEnv())

or explicitly using the WithAPIKey() option

c, err := rockset.NewClient(rockset.WithAPIKey("..."))

func (*RockClient) Query

func (rc *RockClient) Query(request api.QueryRequest) (api.QueryResponse, *http.Response, error)

Query executes a query request against Rockset

Example

This example runs a query against Rockset

// create the API client
client, err := rockset.NewClient(rockset.FromEnv())
if err != nil {
	log.Fatal(err)
}

// construct a query
q := models.QueryRequest{
	Sql: &models.QueryRequestSql{
		Query: `SELECT * FROM "_events" LIMIT 1`,
	},
}

// execute the query
res, _, err := client.Query(q)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("got %d row(s)\n", len(res.Results))
Output:

got 1 row(s)

func (*RockClient) Validate added in v0.8.0

func (rc *RockClient) Validate() error

Validate validates and sets the Rockset client configuration options

type RockOption added in v0.8.0

type RockOption func(rc *RockClient)

RockOption is the type for RockClient options

func FromEnv added in v0.8.0

func FromEnv() RockOption

FromEnv sets API key and API server from the environment variables ROCKSET_APIKEY and ROCKSET_APISERVER

func WithAPIKey added in v0.8.0

func WithAPIKey(key string) RockOption

WithAPIKey sets the API key to key

func WithAPIServer added in v0.8.0

func WithAPIServer(s string) RockOption

WithAPIServer sets the API server

func WithHTTPClient added in v0.8.0

func WithHTTPClient(c *http.Client) RockOption

WithHTTPClient sets the HTTP client. Without this option RockClient uses the http.DefaultClient, which does not have a timeout.

func WithTimeout added in v0.8.0

func WithTimeout(t time.Duration) RockOption

WithTimeout sets the HTTP client timeout, and will override any value set using using the WithHTTPClient() option

Directories

Path Synopsis
lib
go
openapi module

Jump to

Keyboard shortcuts

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