kusto

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2022 License: MIT Imports: 29 Imported by: 21

Documentation

Overview

Package kusto provides a client for accessing Azure Data Explorer, also known as Kusto.

For details on the Azure Kusto service, see: https://azure.microsoft.com/en-us/services/data-explorer/

For general documentation on APIs and the Kusto query language, see: https://docs.microsoft.com/en-us/azure/data-explorer/

Creating an Authorizer and a Client

To begin using this package, create an Authorizer and a client targeting your Kusto endpoint:

// auth package is: "github.com/Azure/go-autorest/autorest/azure/auth"

authorizer := kusto.Authorization{
	Config: auth.NewClientCredentialsConfig(clientID, clientSecret, tenantID),
}

client, err := kusto.New(endpoint, authorizer)
if err != nil {
	panic("add error handling")
}

For more examples on ways to create an Authorization object, see the Authorization object documentation.

Querying for Rows

Kusto provides a single method for querying, Query(). Query uses a Stmt object to provides SQL-like injection protection and accepts only string constants for arguments.

// table package is: data/table

// Query our database table "systemNodes" for the CollectionTimes and the NodeIds.
iter, err := client.Query(ctx, "database", kusto.NewStmt("systemNodes | project CollectionTime, NodeId"))
if err != nil {
	panic("add error handling")
}
defer iter.Stop()

// .Do() will call the function for every row in the table.
err = iter.Do(
	func(row *table.Row) error {
		if row.Replace {
			fmt.Println("---") // Replace flag indicates that the query result should be cleared and replaced with this row
		}
		fmt.Println(row) // As a convenience, printing a *table.Row will output csv
		return nil
	},
)
if err != nil {
	panic("add error handling")
}

Querying Rows Into Structs

Keeping our query the same, instead of printing the Rows we will simply put them into a slice of structs

// NodeRec represents our Kusto data that will be returned.
type NodeRec struct {
	// ID is the table's NodeId. We use the field tag here to to instruct our client to convert NodeId to ID.
	ID int64 `kusto:"NodeId"`
	// CollectionTime is Go representation of the Kusto datetime type.
	CollectionTime time.Time
}

iter, err := client.Query(ctx, "database", kusto.NewStmt("systemNodes | project CollectionTime, NodeId"))
if err != nil {
	panic("add error handling")
}
defer iter.Stop()

recs := []NodeRec{}
err = iter.Do(
	func(row *table.Row) error {
		rec := NodeRec{}
		if err := row.ToStruct(&rec); err != nil {
			return err
		}
		if row.Replace {
			recs = recs[:0]  // Replace flag indicates that the query result should be cleared and replaced with this row
		}
		recs = append(recs, rec)
		return nil
	},
)
if err != nil {
	panic("add error handling")
}

A struct object can use fields to store the Kusto values as normal Go values, pointers to Go values and as value.Kusto types. The value.Kusto types are useful when you need to distiguish between the zero value of a variable and the value not being set in Kusto.

All value.Kusto types have a .Value and .Valid field. .Value is the native Go value, .Valid is a bool which indicates if the value was set. More information can be found in the sub-package data/value.

The following is a conversion table from the Kusto column types to native Go values within a struct that are allowed:

From Kusto Type			To Go Kusto Type
==============================================================================
bool				value.Bool, bool, *bool
------------------------------------------------------------------------------
datetime			value.DateTime, time.Time, *time.Time
------------------------------------------------------------------------------
dynamic				value.Dynamic, string, *string
------------------------------------------------------------------------------
guid				value.GUID, uuid.UUID, *uuid.UUID
------------------------------------------------------------------------------
int				value.Int, int32, *int32
------------------------------------------------------------------------------
long				value.Long, int64, *int64
------------------------------------------------------------------------------
real				value.Real, float64, *float64
------------------------------------------------------------------------------
string				value.String, string, *string
------------------------------------------------------------------------------
timestamp			value.Timestamp, time.Duration, *time.Duration
------------------------------------------------------------------------------
decimal				value.Decimal, string, *string
==============================================================================

For more information on Kusto scalar types, see: https://docs.microsoft.com/en-us/azure/kusto/query/scalar-data-types/

Stmt

Every query is done using a Stmt. A Stmt is built with Go string constants and can do variable substitution using Kusto's Query Paramaters.

// rootStmt builds a query that will gather all nodes in the DB.
rootStmt := kusto.NewStmt("systemNodes | project CollectionTime, NodeId")

// singleNodeStmt creates a new Stmt based on rootStmt and adds a "where" clause to find a single node.
// We pass a definition that sets the word ParamNodeId to a variable that will be substituted for a
// Kusto Long type (which is a a Go int64).
singleNodeStmt := rootStmt.Add(" | where NodeId == ParamNodeId").MustDefinitions(
	kusto.NewDefinitions().Must(
		kusto.ParamTypes{
			"ParamNodeId": kusto.ParamType{Type: types.Long},
		},
	),
)

// Query using our singleNodeStmt, variable substituting for ParamNodeId
iter, err := client.Query(
	ctx,
	"database",
	singleNode.MustParameters(
		kusto.NewParameters().Must(
			kusto.QueryValues{"ParamNodeId": int64(100)},
		),
	),
)

Ingest

Support for Kusto ingestion from local files, Azure Blob Storage and streaming is supported in the sub-package ingest. See documentation in that package for more details

Mocking

To support mocking for this client in your code for hermetic testing purposes, this client supports mocking the data returned by our RowIterator object. Please see the MockRows documentation for code examples.

Package Examples

Below you will find a simple and complex example of doing Query() the represent compiled code:

Example (Complex)
// This example sets up a Query where we want to query for nodes that have a NodeId (a Kusto Long type) that has a
// particular NodeId. The will require inserting a value where ParamNodeId is in the query. We create the query
// and attach a Definition to it that indicates which words we will be substituing for and what the expected type will be.
// the MustDefinitions() will panic if the Definition is not valid. There is a non-panicing version that returns an
// error instead.
rootStmt := NewStmt("systemNodes | project CollectionTime, NodeId | where NodeId == ParamNodeId").MustDefinitions(
	NewDefinitions().Must(
		ParamTypes{
			"ParamNodeId": ParamType{Type: types.Long},
		},
	),
)

// This takes our rootStmt and creates a new Stmt that will insert 100 where ParamNodeId is in the rootStmt.
// rootStmt will remain unchanged. The Must() will panic if the QueryValues{} passed is not valid. This can
// happen because you use a type that isn't valid, like a string or int32.
// There is a non-panicing version that returns an error instead.
stmt, err := rootStmt.WithParameters(NewParameters().Must(QueryValues{"ParamNodeId": int64(100)}))
if err != nil {
	panic("add error handling")
}

// NodeRec represents our Kusto data that will be returned.
type NodeRec struct {
	// ID is the table's NodeId. We use the field tag here to to instruct our client to convert NodeId in the Kusto
	// table to ID in our struct.
	ID int64 `kusto:"NodeId"`
	// CollectionTime is Go representation of the Kusto datetime type.
	CollectionTime time.Time
}

authorizer := Authorization{Config: auth.NewClientCredentialsConfig("clientID", "clientSecret", "tenantID")}

client, err := New("endpoint", authorizer)
if err != nil {
	panic("add error handling")
}
// Be sure to close the client when you're done. (Error handling omitted for brevity.)
defer client.Close()

ctx := context.Background()

// Query our database table "systemNodes" for our specific node. We are only doing a single query here as an example,
// normally you would take in requests of some type for different NodeIds.
iter, err := client.Query(ctx, "database", stmt)
if err != nil {
	panic("add error handling")
}
defer iter.Stop()

rec := NodeRec{} // We are assuming unique NodeId, so we will only get 1 row.
err = iter.Do(
	func(row *table.Row) error {
		return row.ToStruct(&rec)
	},
)

if err != nil {
	panic("add error handling")
}

fmt.Println(rec.ID)
Output:

Example (Simple)
// Query and capture the values and put them in a slice of structs representing the row.

// NodeRec represents our Kusto data that will be returned.
type NodeRec struct {
	// ID is the table's NodeId. We use the field tag here to to instruct our client to convert NodeId to ID.
	ID int64 `kusto:"NodeId"`
	// CollectionTime is Go representation of the Kusto datetime type.
	CollectionTime time.Time
}

authorizer := Authorization{
	Config: auth.NewClientCredentialsConfig("clientID", "clientSecret", "tenantID"),
}

client, err := New("endpoint", authorizer)
if err != nil {
	panic("add error handling")
}

// Be sure to close the client when you're done. (Error handling omitted for brevity.)
defer client.Close()

ctx := context.Background()

// Query our database table "systemNodes" for the CollectionTimes and the NodeIds.
iter, err := client.Query(ctx, "database", NewStmt("systemNodes | project CollectionTime, NodeId"))
if err != nil {
	panic("add error handling")
}
defer iter.Stop()

var recs []NodeRec

err = iter.DoOnRowOrError(
	func(row *table.Row, e *kustoErrors.Error) error {
		if e != nil {
			return e
		}
		rec := NodeRec{}
		if err := row.ToStruct(&rec); err != nil {
			return err
		}
		if row.Replace {
			recs = recs[:0]
		}
		recs = append(recs, rec)
		return nil
	},
)

if err != nil {
	panic("add error handling")
}

for _, rec := range recs {
	fmt.Println(rec.ID)
}
Output:

Index

Examples

Constants

View Source
const (
	// DSDefault is used to set a query's datascope to default.
	DSDefault dataScope = "default"
	// DSAll is used to set a query's datascope to all.
	DSAll dataScope = "all"
	// DSHotCache is used to set a query's datascope to hotcache.
	DSHotCache dataScope = "hotcache"
)
View Source
const ClientMaxRedirectCountValue = "client_max_redirect_count"
View Source
const DeferPartialQueryFailuresValue = "deferpartialqueryfailures"
View Source
const MaterializedViewShuffleValue = "materialized_view_shuffle"
View Source
const MaxMemoryConsumptionPerIteratorValue = "maxmemoryconsumptionperiterator"
View Source
const MaxMemoryConsumptionPerQueryPerNodeValue = "max_memory_consumption_per_query_per_node"
View Source
const MaxOutputColumnsValue = "maxoutputcolumns"
View Source
const NoRequestTimeoutValue = "norequesttimeout"
View Source
const NoTruncationValue = "notruncation"
View Source
const PushSelectionThroughAggregationValue = "push_selection_through_aggregation"
View Source
const QueryBinAutoAtValue = "query_bin_auto_at"
View Source
const QueryBinAutoSizeValue = "query_bin_auto_size"
View Source
const QueryConsistencyValue = "queryconsistency"
View Source
const QueryCursorAfterDefaultValue = "query_cursor_after_default"
View Source
const QueryCursorBeforeOrAtDefaultValue = "query_cursor_before_or_at_default"
View Source
const QueryCursorCurrentValue = "query_cursor_current"
View Source
const QueryCursorDisabledValue = "query_cursor_disabled"
View Source
const QueryCursorScopedTablesValue = "query_cursor_scoped_tables"
View Source
const QueryDatascopeValue = "query_datascope"
View Source
const QueryDateTimeScopeColumnValue = "query_datetimescope_column"
View Source
const QueryDateTimeScopeFromValue = "query_datetimescope_from"
View Source
const QueryDateTimeScopeToValue = "query_datetimescope_to"
View Source
const QueryDistributionNodesSpanValue = "query_distribution_nodes_span"
View Source
const QueryFanoutNodesPercentValue = "query_fanout_nodes_percent"
View Source
const QueryFanoutThreadsPercentValue = "query_fanout_threads_percent"
View Source
const QueryForceRowLevelSecurityValue = "query_force_row_level_security"
View Source
const QueryLanguageValue = "query_language"
View Source
const QueryLogQueryParametersValue = "query_log_query_parameters"
View Source
const QueryMaxEntitiesInUnionValue = "query_max_entities_in_union"
View Source
const QueryNowValue = "query_now"
View Source
const QueryPythonDebugValue = "query_python_debug"
View Source
const QueryResultsApplyGetschemaValue = "query_results_apply_getschema"
View Source
const QueryResultsCacheMaxAgeValue = "query_results_cache_max_age"
View Source
const QueryResultsCachePerShardValue = "query_results_cache_per_shard"
View Source
const QueryResultsProgressiveRowCountValue = "query_results_progressive_row_count"
View Source
const QueryResultsProgressiveUpdatePeriodValue = "query_results_progressive_update_period"
View Source
const QueryTakeMaxRecordsValue = "query_take_max_records"
View Source
const RequestAppNameValue = "request_app_name"
View Source
const RequestBlockRowLevelSecurityValue = "request_block_row_level_security"
View Source
const RequestCalloutDisabledValue = "request_callout_disabled"
View Source
const RequestDescriptionValue = "request_description"
View Source
const RequestExternalTableDisabledValue = "request_external_table_disabled"
View Source
const RequestImpersonationDisabledValue = "request_impersonation_disabled"
View Source
const RequestReadonlyValue = "request_readonly"
View Source
const RequestRemoteEntitiesDisabledValue = "request_remote_entities_disabled"
View Source
const RequestSandboxedExecutionDisabledValue = "request_sandboxed_execution_disabled"
View Source
const RequestUserValue = "request_user"
View Source
const ServerTimeoutValue = "servertimeout"
View Source
const TruncationMaxRecordsValue = "truncation_max_records"
View Source
const TruncationMaxSizeValue = "truncation_max_size"
View Source
const ValidatePermissionsValue = "validate_permissions"

Variables

This section is empty.

Functions

This section is empty.

Types

type Authorization

type Authorization struct {
	// Authorizer provides an authorizer to use when talking to Kusto. If this is set, the
	// Authorizer must have its Resource (also called Resource ID) set to the endpoint passed
	// to the New() constructor. This will be something like "https://somename.westus.kusto.windows.net".
	// This package will try to set that automatically for you.
	Authorizer autorest.Authorizer
	// Config provides the authorizer's config that can create the authorizer. We recommending setting
	// this instead of Authorizer, as we will automatically set the Resource ID with the endpoint passed.
	Config auth.AuthorizerConfig
}

Authorization provides the ADAL authorizer needed to access the resource. You can set Authorizer or Config, but not both.

Example (Config)
// Create an authorizer with your Azure ClientID, Secret and TenantID.
authorizer := Authorization{
	Config: auth.NewClientCredentialsConfig("clientID", "clientSecret", "tenantID"),
}

// Normally here you take a client.
_, err := New("endpoint", authorizer)
if err != nil {
	panic("add error handling")
}
Output:

Example (Msi)
// Create an authorizer with an Azure MSI (managed identities).
msi := auth.NewMSIConfig()

authorizer := Authorization{
	Config: msi,
}

// Normally here you take a client.
_, err := New("endpoint", authorizer)
if err != nil {
	panic("add error handling")
}
Output:

func (*Authorization) Validate

func (a *Authorization) Validate(endpoint string) error

Validate validates the Authorization object against the endpoint an preps it for use. For internal use only.

type Client

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

Client is a client to a Kusto instance.

func New

func New(endpoint string, auth Authorization, options ...Option) (*Client, error)

New returns a new Client. endpoint is the Kusto endpoint to use, example: https://somename.westus.kusto.windows.net .

func NewMockClient added in v0.6.0

func NewMockClient() *Client

func (*Client) Auth

func (c *Client) Auth() Authorization

Auth returns the Authorization passed to New().

func (*Client) Close added in v0.7.0

func (c *Client) Close() error

func (*Client) Endpoint

func (c *Client) Endpoint() string

Endpoint returns the endpoint passed to New().

func (*Client) HttpClient added in v0.7.0

func (c *Client) HttpClient() *http.Client

func (*Client) Mgmt

func (c *Client) Mgmt(ctx context.Context, db string, query Stmt, options ...MgmtOption) (*RowIterator, error)

Mgmt is used to do management queries to Kusto. Details can be found at: https://docs.microsoft.com/en-us/azure/kusto/management/ Mgmt accepts a Stmt, but that Stmt cannot have any query parameters attached at this time. Note that the server has a timeout of 10 minutes for a management call by default unless the context deadline is set. There is a maximum of 1 hour.

func (*Client) Query

func (c *Client) Query(ctx context.Context, db string, query Stmt, options ...QueryOption) (*RowIterator, error)

Query queries Kusto for data. context can set a timeout or cancel the query. query is a injection safe Stmt object. Queries cannot take longer than 5 minutes by default and have row/size limitations. Note that the server has a timeout of 4 minutes for a query by default unless the context deadline is set. Queries can take a maximum of 1 hour.

Example (Do)
// This is similar to our (Row) example. In this one though, we use the RowIterator.Do() method instead of
// manually iterating over the row. This makes for shorter code while maintaining readability.

authorizer := Authorization{
	Config: auth.NewClientCredentialsConfig("clientID", "clientSecret", "tenantID"),
}

client, err := New("endpoint", authorizer)
if err != nil {
	panic("add error handling")
}
// Be sure to close the client when you're done. (Error handling omitted for brevity.)
defer client.Close()

ctx := context.Background()

// Query our database table "systemNodes" for the CollectionTimes and the NodeIds.
iter, err := client.Query(ctx, "database", NewStmt("systemNodes | project CollectionTime, NodeId"))
if err != nil {
	panic("add error handling")
}
defer iter.Stop()

// Iterate through the returned rows until we get an error or receive an io.EOF, indicating the end of
// the data being returned.

err = iter.Do(
	func(row *table.Row) error {
		if row.Replace {
			fmt.Println("---") // Replace flag indicates that the query result should be cleared and replaced with this row
		}
		for _, v := range row.Values {
			fmt.Printf("%s,", v)
		}
		fmt.Println("") // Add a carriage return
		return nil
	},
)
if err != nil {
	panic("add error handling")
}
Output:

Example (Rows)
authorizer := Authorization{
	Config: auth.NewClientCredentialsConfig("clientID", "clientSecret", "tenantID"),
}

client, err := New("endpoint", authorizer)
if err != nil {
	panic("add error handling")
}
// Be sure to close the client when you're done. (Error handling omitted for brevity.)
defer client.Close()

ctx := context.Background()

// Query our database table "systemNodes" for the CollectionTimes and the NodeIds.
iter, err := client.Query(ctx, "database", NewStmt("systemNodes | project CollectionTime, NodeId"))
if err != nil {
	panic("add error handling")
}
defer iter.Stop()

// Iterate through the returned rows until we get an error or receive an io.EOF, indicating the end of
// the data being returned.
for {
	row, err := iter.Next()
	if err != nil {
		if err == io.EOF {
			break
		}
		if err != nil {
			panic("add error handling")
		}
	}

	// Print out the row values
	for _, v := range row.Values {
		fmt.Printf("%s,", v)
	}
	fmt.Println("") // Add a carriage return
}
Output:

Example (Struct)
// Capture our values into a struct and sends those values into a channel. Normally this would be done between
// a couple of functions representing a sender and a receiver.

// NodeRec represents our Kusto data that will be returned.
type NodeRec struct {
	// ID is the table's NodeId. We use the field tag here to to instruct our client to convert NodeId to ID.
	ID int64 `kusto:"NodeId"`
	// CollectionTime is Go representation of the Kusto datetime type.
	CollectionTime time.Time

	// err is used internally to signal downstream that we encounter an error.
	err error
}

authorizer := Authorization{
	Config: auth.NewClientCredentialsConfig("clientID", "clientSecret", "tenantID"),
}

client, err := New("endpoint", authorizer)
if err != nil {
	panic("add error handling")
}
// Be sure to close the client when you're done. (Error handling omitted for brevity.)
defer client.Close()

ctx := context.Background()

// Query our database table "systemNodes" for the CollectionTimes and the NodeIds.
iter, err := client.Query(ctx, "database", NewStmt("systemNodes | project CollectionTime, NodeId"))
if err != nil {
	panic("add error handling")
}
defer iter.Stop()

// printCh is used to receive NodeRecs for printing.
printCh := make(chan NodeRec, 1)

// Iterate through the returned rows, convert them to NodeRecs and send them on printCh to be printed.
go func() {
	// Note: we ignore the error here because we send it on a channel and an error will automatically
	// end the iteration.
	_ = iter.Do(
		func(row *table.Row) error {
			rec := NodeRec{}
			rec.err = row.ToStruct(&rec)
			printCh <- rec
			return rec.err
		},
	)
}()

// Receive the NodeRecs on printCh and print them to the screen.
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
	defer wg.Done()
	for rec := range printCh {
		if rec.err != nil {
			fmt.Println("Got error: ", err)
			return
		}
		fmt.Printf("NodeID: %d, CollectionTime: %s\n", rec.ID, rec.CollectionTime)
	}
}()

wg.Wait()
Output:

func (*Client) QueryToJson added in v0.8.0

func (c *Client) QueryToJson(ctx context.Context, db string, query Stmt, options ...QueryOption) (string, error)

type DataScope added in v0.8.0

type DataScope interface {
	// contains filtered or unexported methods
}

DataScope is used with QueryDataScope() to control a query's datascope.

type Definitions

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

Definitions represents definitions of parameters that are substituted for variables in a Kusto Query. This provides both variable substitution in a Stmt and provides protection against SQL-like injection attacks. See https://docs.microsoft.com/en-us/azure/kusto/query/queryparametersstatement?pivots=azuredataexplorer for internals. This object is not thread-safe and passing it as an argument to a function will create a copy that will share the internal state with the original.

func NewDefinitions

func NewDefinitions() Definitions

NewDefinitions is the constructor for Definitions.

func (Definitions) IsZero

func (p Definitions) IsZero() bool

IsZero indicates if the Definitions object is the zero type.

func (Definitions) Must

func (p Definitions) Must(types ParamTypes) Definitions

Must is the same as With(), but it must succeed or it panics.

func (Definitions) String

func (p Definitions) String() string

String implements fmt.Stringer.

func (Definitions) With

func (p Definitions) With(types ParamTypes) (Definitions, error)

With returns a copy of the Definitions object with the parameters names and types defined in "types".

type MgmtOption

type MgmtOption func(m *mgmtOptions) error

MgmtOption is an option type for a call to Mgmt().

func AllowWrite deprecated

func AllowWrite() MgmtOption

Deprecated: Writing mode is now the default. Use the `RequestReadonly` option to make a read-only request.

func IngestionEndpoint

func IngestionEndpoint() MgmtOption

IngestionEndpoint will instruct the Mgmt call to connect to the ingest-[endpoint] instead of [endpoint]. This is not often used by end users and can only be used with a Mgmt() call.

type MockRows

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

MockRows provides the abilty to provide mocked Row data that can be played back from a RowIterator. This allows for creating hermetic tests from mock data or creating mock data from a real data fetch.

func NewMockRows

func NewMockRows(columns table.Columns) (*MockRows, error)

NewMockRows is the constructor for MockRows.

func (*MockRows) Error

func (m *MockRows) Error(err error) error

Error adds an error into the result stream. Nothing else added to this stream will matter once this is called.

func (*MockRows) Row

func (m *MockRows) Row(row value.Values) error

Row adds Row data that will be replayed in a RowIterator.

func (*MockRows) Struct

func (m *MockRows) Struct(p interface{}) error

Struct adds Row data that will be replayed in a RowIterator by parsing the passed *struct into value.Values.

type Option

type Option func(c *Client)

Option is an optional argument type for New().

func WithHttpClient added in v0.7.0

func WithHttpClient(client *http.Client) Option

type ParamType

type ParamType struct {
	// Type is the type of Column type this QueryParam will represent.
	Type types.Column
	// Default is a default value to use if the query doesn't provide this value.
	// The value that can be set is defined by the Type:
	// CTBool must be a bool
	// CTDateTime must be a time.Time
	// CTDynamic cannot have a default value
	// CTGuid must be an uuid.UUID
	// CTInt must be an int32
	// CTLong must be an int64
	// CTReal must be an float64
	// CTString must be a string
	// CTTimespan must be a time.Duration
	// CTDecimal must be a string or *big.Float representing a decimal value
	Default interface{}
	// contains filtered or unexported fields
}

ParamType provides type and default value information about the query parameter

type ParamTypes

type ParamTypes map[string]ParamType

ParamTypes is a list of parameter types and corresponding type data.

type Parameters

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

Parameters represents values that will be substituted for a Stmt's Parameter. Keys are the names of corresponding Parameters, values are the value to be used. Keys must exist in the Parameter and value must be a Go type that corresponds to the ParamType.

func NewParameters

func NewParameters() Parameters

NewParameters is the construtor for Parameters.

func (Parameters) IsZero

func (q Parameters) IsZero() bool

IsZero returns if Parameters is the zero value.

func (Parameters) Must

func (q Parameters) Must(values QueryValues) Parameters

Must is the same as With() except any error is a panic.

func (Parameters) With

func (q Parameters) With(values QueryValues) (Parameters, error)

With returns a Parameters set to "values". values' keys represents Definitions names that will substituted for and the values to be subsituted.

type QueryOption

type QueryOption func(q *queryOptions) error

QueryOption is an option type for a call to Query().

func Application added in v0.8.1

func Application(appName string) QueryOption

Application sets the x-ms-app header, and can be used to identify the application making the request in the `.show queries` output.

func ClientMaxRedirectCount added in v0.8.0

func ClientMaxRedirectCount(i int64) QueryOption

ClientMaxRedirectCount If set and positive, indicates the maximum number of HTTP redirects that the client will process.

func CustomQueryOption added in v0.8.0

func CustomQueryOption(paramName string, i interface{}) QueryOption

CustomQueryOption exists to allow a QueryOption that is not defined in the Go SDK, as all options are not defined. Please Note: you should always use the type safe options provided below when available. Also note that Kusto does not error on non-existent parameter names or bad values, it simply doesn't work as expected.

func DeferPartialQueryFailures added in v0.8.0

func DeferPartialQueryFailures() QueryOption

DeferPartialQueryFailures disables reporting partial query failures as part of the result set.

func MaterializedViewShuffle added in v0.8.0

func MaterializedViewShuffle(s string) QueryOption

MaterializedViewShuffle A hint to use shuffle strategy for materialized views that are referenced in the query. The property is an array of materialized views names and the shuffle keys to use. Examples: 'dynamic([ { "Name": "V1", "Keys" : [ "K1", "K2" ] } ])' (shuffle view V1 by K1, K2) or 'dynamic([ { "Name": "V1" } ])' (shuffle view V1 by all keys)

func MaxMemoryConsumptionPerIterator added in v0.8.0

func MaxMemoryConsumptionPerIterator(i uint64) QueryOption

MaxMemoryConsumptionPerIterator overrides the default maximum amount of memory a query operator may allocate.

func MaxMemoryConsumptionPerQueryPerNode added in v0.8.0

func MaxMemoryConsumptionPerQueryPerNode(i uint64) QueryOption

MaxMemoryConsumptionPerQueryPerNode overrides the default maximum amount of memory a whole query may allocate per node.

func MaxOutputColumns added in v0.8.0

func MaxOutputColumns(i int) QueryOption

MaxOutputColumns overrides the default maximum number of columns a query is allowed to produce.

func NoRequestTimeout added in v0.8.0

func NoRequestTimeout() QueryOption

NoRequestTimeout enables setting the request timeout to its maximum value.

func NoTruncation added in v0.8.0

func NoTruncation() QueryOption

NoTruncation enables suppressing truncation of the query results returned to the caller.

func PushSelectionThroughAggregation added in v0.8.0

func PushSelectionThroughAggregation() QueryOption

PushSelectionThroughAggregation will push simple selection through aggregation .

func QueryBinAutoAt added in v0.8.0

func QueryBinAutoAt(s string) QueryOption

QueryBinAutoAt When evaluating the bin_auto() function, the start value to use.

func QueryBinAutoSize added in v0.8.0

func QueryBinAutoSize(s string) QueryOption

QueryBinAutoSize When evaluating the bin_auto() function, the bin size value to use.

func QueryConsistency added in v0.8.0

func QueryConsistency(c string) QueryOption

QueryConsistency Controls query consistency

func QueryCursorAfterDefault added in v0.8.0

func QueryCursorAfterDefault(s string) QueryOption

QueryCursorAfterDefault sets the default parameter value of the cursor_after() function when called without parameters.

func QueryCursorBeforeOrAtDefault added in v0.8.0

func QueryCursorBeforeOrAtDefault(s string) QueryOption

QueryCursorBeforeOrAtDefault sets the default parameter value of the cursor_before_or_at() function when called without parameters.

func QueryCursorCurrent added in v0.8.0

func QueryCursorCurrent(s string) QueryOption

QueryCursorCurrent overrides the cursor value returned by the cursor_current() or current_cursor() functions.

func QueryCursorDisabled added in v0.8.0

func QueryCursorDisabled(s string) QueryOption

QueryCursorDisabled overrides the cursor value returned by the cursor_current() or current_cursor() functions.

func QueryCursorScopedTables added in v0.8.0

func QueryCursorScopedTables(l []string) QueryOption

QueryCursorScopedTables is a list of table names that should be scoped to cursor_after_default .. cursor_before_or_at_default (upper bound is optional).

func QueryDataScope added in v0.8.0

func QueryDataScope(ds DataScope) QueryOption

QueryDataScope controls the query's datascope -- whether the query applies to all data or just part of it. ['default', 'all', or 'hotcache']

func QueryDateTimeScopeColumn added in v0.8.0

func QueryDateTimeScopeColumn(s string) QueryOption

QueryDateTimeScopeColumn controls the column name for the query's datetime scope (query_datetimescope_to / query_datetimescope_from)

func QueryDateTimeScopeFrom added in v0.8.0

func QueryDateTimeScopeFrom(t time.Time) QueryOption

QueryDateTimeScopeFrom controls the query's datetime scope (earliest) -- used as auto-applied filter on query_datetimescope_column only (if defined).

func QueryDateTimeScopeTo added in v0.8.0

func QueryDateTimeScopeTo(t time.Time) QueryOption

QueryDateTimeScopeTo controls the query's datetime scope (latest) -- used as auto-applied filter on query_datetimescope_column only (if defined).

func QueryDistributionNodesSpan added in v0.8.0

func QueryDistributionNodesSpan(i int64) QueryOption

QueryDistributionNodesSpan If set, controls the way the subquery merge behaves: the executing node will introduce an additional level in the query hierarchy for each subgroup of nodes; the size of the subgroup is set by this option.

func QueryFanoutNodesPercent added in v0.8.0

func QueryFanoutNodesPercent(i int) QueryOption

QueryFanoutNodesPercent The percentage of nodes to fan out execution to.

func QueryFanoutThreadsPercent added in v0.8.0

func QueryFanoutThreadsPercent(i int) QueryOption

QueryFanoutThreadsPercent The percentage of threads to fan out execution to.

func QueryForceRowLevelSecurity added in v0.8.0

func QueryForceRowLevelSecurity() QueryOption

QueryForceRowLevelSecurity If specified, forces Row Level Security rules, even if row_level_security policy is disabled

func QueryLanguage added in v0.8.0

func QueryLanguage(s string) QueryOption

QueryLanguage Controls how the query text is to be interpreted (Kql or Sql).

func QueryLogQueryParameters added in v0.8.0

func QueryLogQueryParameters() QueryOption

QueryLogQueryParameters Enables logging of the query parameters, so that they can be viewed later in the .show queries journal.

func QueryMaxEntitiesInUnion added in v0.8.0

func QueryMaxEntitiesInUnion(i int64) QueryOption

QueryMaxEntitiesInUnion Overrides the default maximum number of entities in a union.

func QueryNow added in v0.8.0

func QueryNow(t time.Time) QueryOption

QueryNow Overrides the datetime value returned by the now(0s) function.

func QueryPythonDebug added in v0.8.0

func QueryPythonDebug(i int) QueryOption

QueryPythonDebug If set, generate python debug query for the enumerated python node (default first).

func QueryResultsApplyGetschema added in v0.8.0

func QueryResultsApplyGetschema() QueryOption

QueryResultsApplyGetschema If set, retrieves the schema of each tabular data in the results of the query instead of the data itself.

func QueryResultsCacheMaxAge added in v0.8.0

func QueryResultsCacheMaxAge(d time.Duration) QueryOption

QueryResultsCacheMaxAge If positive, controls the maximum age of the cached query results the service is allowed to return

func QueryResultsCachePerShard added in v0.8.0

func QueryResultsCachePerShard() QueryOption

QueryResultsCachePerShard If set, enables per-shard query cache.

func QueryResultsProgressiveRowCount added in v0.8.0

func QueryResultsProgressiveRowCount(i int64) QueryOption

QueryResultsProgressiveRowCount Hint for Kusto as to how many records to send in each update (takes effect only if OptionResultsProgressiveEnabled is set)

func QueryResultsProgressiveUpdatePeriod added in v0.8.0

func QueryResultsProgressiveUpdatePeriod(i int32) QueryOption

QueryResultsProgressiveUpdatePeriod Hint for Kusto as to how often to send progress frames (takes effect only if OptionResultsProgressiveEnabled is set)

func QueryTakeMaxRecords added in v0.8.0

func QueryTakeMaxRecords(i int64) QueryOption

QueryTakeMaxRecords Enables limiting query results to this number of records.

func RequestAppName added in v0.8.0

func RequestAppName(s string) QueryOption

RequestAppName Request application name to be used in the reporting (e.g. show queries). Does not set the `Application` property in `.show queries`, see `Application` for that.

func RequestBlockRowLevelSecurity added in v0.8.0

func RequestBlockRowLevelSecurity() QueryOption

RequestBlockRowLevelSecurity If specified, blocks access to tables for which row_level_security policy is enabled.

func RequestCalloutDisabled added in v0.8.0

func RequestCalloutDisabled() QueryOption

RequestCalloutDisabled If specified, indicates that the request can't call-out to a user-provided service.

func RequestDescription added in v0.8.0

func RequestDescription(s string) QueryOption

RequestDescription Arbitrary text that the author of the request wants to include as the request description.

func RequestExternalTableDisabled added in v0.8.0

func RequestExternalTableDisabled() QueryOption

RequestExternalTableDisabled If specified, indicates that the request can't invoke code in the ExternalTable.

func RequestImpersonationDisabled added in v0.8.0

func RequestImpersonationDisabled() QueryOption

RequestImpersonationDisabled If specified, indicates that the service should not impersonate the caller's identity.

func RequestReadonly added in v0.8.0

func RequestReadonly() QueryOption

RequestReadonly If specified, indicates that the request can't write anything.

func RequestRemoteEntitiesDisabled added in v0.8.0

func RequestRemoteEntitiesDisabled() QueryOption

RequestRemoteEntitiesDisabled If specified, indicates that the request can't access remote databases and clusters.

func RequestSandboxedExecutionDisabled added in v0.8.0

func RequestSandboxedExecutionDisabled() QueryOption

RequestSandboxedExecutionDisabled If specified, indicates that the request can't invoke code in the sandbox.

func RequestUser added in v0.8.0

func RequestUser(s string) QueryOption

RequestUser Request user to be used in the reporting (e.g. show queries). Does not set the `User` property in `.show queries`, see `User` for that.

func ResultsProgressiveDisable

func ResultsProgressiveDisable() QueryOption

ResultsProgressiveDisable disables the progressive query stream.

func TruncationMaxRecords added in v0.8.0

func TruncationMaxRecords(i int64) QueryOption

TruncationMaxRecords Overrides the default maximum number of records a query is allowed to return to the caller (truncation).

func TruncationMaxSize added in v0.8.0

func TruncationMaxSize(i int64) QueryOption

TruncationMaxSize Overrides the default maximum data size a query is allowed to return to the caller (truncation).

func User added in v0.8.1

func User(userName string) QueryOption

User sets the x-ms-user header, and can be used to identify the user making the request in the `.show queries` output.

func ValidatePermissions added in v0.8.0

func ValidatePermissions() QueryOption

ValidatePermissions Validates user's permissions to perform the query and doesn't run the query itself.

type QueryValues

type QueryValues map[string]interface{}

QueryValues represents a set of values that are substituted in Parameters. Every QueryValue key must have a corresponding Parameter name. All values must be compatible with the Kusto Column type it will go into (int64 for a long, int32 for int, time.Time for datetime, ...)

type Row added in v0.6.0

type Row struct {
	Values  value.Values
	Error   *errors.Error
	Replace bool
}

Row is a row of data from Kusto, or an error. Replace indicates whether the existing result set should be cleared and replaced with this row.

type RowIterator

type RowIterator struct {

	// RequestHeader is the http.Header sent in the request to the server.
	RequestHeader http.Header
	// ResponseHeader is the http.header sent in the response from the server.
	ResponseHeader http.Header
	// contains filtered or unexported fields
}

RowIterator is used to iterate over the returned Row objects returned by Kusto.

func (*RowIterator) Do deprecated

func (r *RowIterator) Do(f func(r *table.Row) error) error

Deprecated: Use DoOnRowOrError() instead for more robust error handling. In a future version, this will be removed, and NextRowOrError will replace it. Do calls f for every row returned by the query. If f returns a non-nil error, iteration stops. This method will fail on errors inline within the rows, even though they could potentially be recovered and more data might be available. This behavior is to keep the interface compatible.

func (*RowIterator) DoOnRowOrError added in v0.6.0

func (r *RowIterator) DoOnRowOrError(f func(r *table.Row, e *errors.Error) error) error

DoOnRowOrError calls f for every row returned by the query. If errors occur inline within the rows, they are passed to f. Other errors will stop the iteration and be returned. If f returns a non-nil error, iteration stops.

func (*RowIterator) GetExtendedProperties added in v0.6.0

func (r *RowIterator) GetExtendedProperties() (v2.DataTable, error)

GetExtendedProperties will return the extended properties' table from the iterator, if it exists. Returns io.ErrUnexpectedEOF if not found. May not have all tables until RowIterator has reached io.EOF.

func (*RowIterator) GetNonPrimary added in v0.6.0

func (r *RowIterator) GetNonPrimary(tableKind, tableName frames.TableKind) (v2.DataTable, error)

GetNonPrimary will return a non-primary dataTable if it exists from the last query. The non-primary table and common names are defined under the frames.TableKind enum. Returns io.ErrUnexpectedEOF if not found. May not have all tables until RowIterator has reached io.EOF.

func (*RowIterator) GetQueryCompletionInformation added in v0.6.0

func (r *RowIterator) GetQueryCompletionInformation() (v2.DataTable, error)

GetQueryCompletionInformation will return the query completion information table from the iterator, if it exists. Returns io.ErrUnexpectedEOF if not found. May not have all tables until RowIterator has reached io.EOF.

func (*RowIterator) Mock

func (r *RowIterator) Mock(m *MockRows) error

Mock is used to tell the RowIterator to return specific data for tests. This is useful when building fakes of the client's Query() call for hermetic tests. This can only be called in a test or it will panic.

func (*RowIterator) Next deprecated

func (r *RowIterator) Next() (row *table.Row, finalError error)

Deprecated: Use NextRowOrError() instead for more robust error handling. In a future version, this will be removed, and NextRowOrError will replace it. Next gets the next Row from the query. io.EOF is returned if there are no more entries in the output. This method will fail on errors inline within the rows, even though they could potentially be recovered and more data might be available. Once Next() returns an error, all subsequent calls will return the same error.

func (*RowIterator) NextRowOrError added in v0.6.0

func (r *RowIterator) NextRowOrError() (row *table.Row, inlineError *errors.Error, finalError error)

NextRowOrError gets the next Row or service-side error from the query. On partial success, inlineError will be set. Once finalError returns non-nil, all subsequent calls will return the same error. finalError will be set to io.EOF is when frame parsing completed with success or partial success (data + errors). if finalError is not io.EOF, reading the frame has resulted in a failure state (no data is expected).

func (*RowIterator) Progress

func (r *RowIterator) Progress() float64

Progress returns the progress of the query, 0-100%. This is only valid on Progressive data returns.

func (*RowIterator) Progressive

func (r *RowIterator) Progressive() bool

Progressive indicates if the RowIterator is unpacking progressive (streaming) frames.

func (*RowIterator) Stop

func (r *RowIterator) Stop()

Stop is called to stop any further iteration. Always defer a Stop() call after receiving a RowIterator.

type Stmt

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

Stmt is a Kusto Query statement. A Stmt is thread-safe, but methods on the Stmt are not. All methods on a Stmt do not alter the statement, they return a new Stmt object with the changes. This includes a copy of the Definitions and Parameters objects, if provided. This allows a root Stmt object that can be built upon. You should not pass *Stmt objects.

Example
package main

import (
	"fmt"

	"github.com/Azure/azure-kusto-go/kusto"
	"github.com/Azure/azure-kusto-go/kusto/data/types"
)

const (
	// qRoot is a a root query text that will be used in a Stmt. It contains everything that any
	// other Stmt objects will need.
	qRoot = "set notruncation;dcmInventoryComponentSystem\n| project NodeId, Version"
	// singleNode includes syntax that will be included only when we want to grab a single node.
	// The word "Node" here is substituted for variable.
	singleNode = "\n| where NodeId == Node\n"
)

var (
	// rootStmt represents our root Stmt object in which we can derive other Stmts. This definition
	// will always include NodeId and Version fields.
	rootStmt = kusto.NewStmt(qRoot)
	// singleStmt is derived from the rootStmt but includes a where clause to limit the query to a
	// single node. You will see that we input a Definitions object to define the "Node" word in the
	// query as a string.
	singleStmt = rootStmt.Add(singleNode).MustDefinitions(
		kusto.NewDefinitions().Must(
			kusto.ParamTypes{
				"Node": kusto.ParamType{Type: types.String},
			},
		),
	)
)

func main() {
	var err error

	// This will print the rootStmt that could be used to list all nodes in the table.
	fmt.Println("All Nodes Statement:\n", rootStmt.String())

	// If we wanted to query for a single node, we could build a Stmt fron singleStmt like so:
	params := kusto.NewParameters()
	params, err = params.With(kusto.QueryValues{"Node": "my_id"}) // Substitute "my_id" in every place in the query where "Node" is
	if err != nil {
		panic(err)
	}

	stmt, err := singleStmt.WithParameters(params)
	if err != nil {
		panic(err)
	}

	fmt.Println("Single Statement:\n", stmt)
	j, err := stmt.ValuesJSON()
	if err != nil {
		panic(err)
	}
	fmt.Println("Single Statement Parameters:\n", j)

	// Here is a more condensed version:
	stmt, err = singleStmt.WithParameters(kusto.NewParameters().Must(kusto.QueryValues{"Node": "my_id2"}))
	if err != nil {
		panic(err)
	}

	fmt.Println("Single Statement(Condensed):\n", stmt)

	// For repeated queries off a channel or loop, we can further optimize.
	params = kusto.NewParameters()
	qv := kusto.QueryValues{}

	qv["Node"] = "node id from channel"
	stmt, err = singleStmt.WithParameters(params.Must(qv))
	if err != nil {
		panic(err)
	}

	fmt.Println("Single Statement(Repeat):\n", stmt)

}
Output:

All Nodes Statement:
 set notruncation;dcmInventoryComponentSystem
| project NodeId, Version
Single Statement:
 declare query_parameters(Node:string);
set notruncation;dcmInventoryComponentSystem
| project NodeId, Version
| where NodeId == Node

Single Statement Parameters:
 {"Node":"my_id"}
Single Statement(Condensed):
 declare query_parameters(Node:string);
set notruncation;dcmInventoryComponentSystem
| project NodeId, Version
| where NodeId == Node

Single Statement(Repeat):
 declare query_parameters(Node:string);
set notruncation;dcmInventoryComponentSystem
| project NodeId, Version
| where NodeId == Node

func NewStmt

func NewStmt(query stringConstant, options ...StmtOption) Stmt

NewStmt creates a Stmt from a string constant.

func (Stmt) Add

func (s Stmt) Add(query stringConstant) Stmt

Add will add more text to the Stmt. This is similar to the + operator on two strings, except it only can be done with string constants. This allows dynamically building of a query from a root Stmt.

func (Stmt) MustDefinitions

func (s Stmt) MustDefinitions(defs Definitions) Stmt

MustDefinitions is the same as WithDefinitions with the exceptions that an error causes a panic.

func (Stmt) MustParameters

func (s Stmt) MustParameters(params Parameters) Stmt

MustParameters is the same as WithParameters with the exceptions that an error causes a panic.

func (Stmt) String

func (s Stmt) String() string

String implements fmt.Stringer. This can be used to see what the query statement to the server will be for debugging purposes.

func (Stmt) UnsafeAdd

func (s Stmt) UnsafeAdd(query string) Stmt

UnsafeAdd provides a method to add strings that are not injection protected to the Stmt. To utilize this method, you must create the Stmt with the UnsafeStmt() option and pass the unsafe.Stmt with .Add set to true. If not set, THIS WILL PANIC!

func (Stmt) ValuesJSON

func (s Stmt) ValuesJSON() (string, error)

ValuesJSON returns a string in JSON format representing the Kusto QueryOptions.Parameters value that will be passed to the server. These values are substitued for Definitions in the Stmt and are represented by the Parameters that was passed.

func (Stmt) WithDefinitions

func (s Stmt) WithDefinitions(defs Definitions) (Stmt, error)

WithDefinitions will return a Stmt that can be used in a Query() with Kusto Parameters to protect against SQL-like injection attacks. These Parameters must align with the placeholders in the statement. The new Stmt object will have a copy of the Parameters passed, not the original.

func (Stmt) WithParameters

func (s Stmt) WithParameters(params Parameters) (Stmt, error)

WithParameters returns a Stmt that has the Parameters that will be substituted for Definitions in the query. Must have supplied the appropriate Definitions using WithQueryParamaters().

type StmtOption

type StmtOption func(s *Stmt)

StmtOption is an optional argument to NewStmt().

func UnsafeStmt

func UnsafeStmt(options unsafe.Stmt) StmtOption

UnsafeStmt enables unsafe actions on a Stmt and all Stmts derived from that Stmt. This turns off safety features that could allow a service client to compromise your data store. USE AT YOUR OWN RISK!

type TableOfContents added in v0.4.2

type TableOfContents struct {
	Ordinal    int64
	Kind       string
	Name       string
	Id         string
	PrettyName string
}

Directories

Path Synopsis
data
errors
Package errors provides the error package for Kusto.
Package errors provides the error package for Kusto.
table
Package table contains types that represent the makeup of a Kusto table.
Package table contains types that represent the makeup of a Kusto table.
types
Package types holds Kusto type information that is used to describe what type would be held in a cell based on the column's type setting.
Package types holds Kusto type information that is used to describe what type would be held in a cell based on the column's type setting.
value
Package value holds Kusto data value representations.
Package value holds Kusto data value representations.
Package ingest provides data ingestion from various external sources into Kusto.
Package ingest provides data ingestion from various external sources into Kusto.
internal/conn
Package conn holds a streaming ingest connetion.
Package conn holds a streaming ingest connetion.
internal/gzip
Package gzip provides a streaming object for taking in io.ReadCloser that is being written to and providing an io.ReadCloser that outputs the original content gzip compressed.
Package gzip provides a streaming object for taking in io.ReadCloser that is being written to and providing an io.ReadCloser that outputs the original content gzip compressed.
internal/properties
Package properties provides Kusto REST properties that will need to be serialized and sent to Kusto based upon the type of ingestion we are doing.
Package properties provides Kusto REST properties that will need to be serialized and sent to Kusto based upon the type of ingestion we are doing.
internal/queued
Package filesystem provides a client with the ability to import data into Kusto via a variety of fileystems such as local storage or blobstore.
Package filesystem provides a client with the ability to import data into Kusto via a variety of fileystems such as local storage or blobstore.
internal/resources
Package resources contains objects that are used to gather information about Kusto resources that are used during various ingestion methods.
Package resources contains objects that are used to gather information about Kusto resources that are used during various ingestion methods.
internal
frames/unmarshal
Package unmarshal provides decoding of Kusto row data in a frame into []value.Values representing those rows.
Package unmarshal provides decoding of Kusto row data in a frame into []value.Values representing those rows.
frames/unmarshal/json
Package json implements encoding and decoding of JSON as defined in RFC 7159.
Package json implements encoding and decoding of JSON as defined in RFC 7159.
frames/v1
Package v1 holds framing information for the v1 REST API.
Package v1 holds framing information for the v1 REST API.
frames/v2
Package v2 holds framing information for the v2 REST API.
Package v2 holds framing information for the v2 REST API.
log
version
Package version keeps the internal version number of the client.
Package version keeps the internal version number of the client.
test
Package unsafe provides methods and types that loosen the native protections of the Kusto package.
Package unsafe provides methods and types that loosen the native protections of the Kusto package.

Jump to

Keyboard shortcuts

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