/
streaming.go
184 lines (152 loc) · 4.9 KB
/
streaming.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package ingest
import (
"bytes"
"context"
"encoding/json"
"io"
"os"
"github.com/Azure/azure-kusto-go/kusto"
"github.com/Azure/azure-kusto-go/kusto/data/errors"
"github.com/Azure/azure-kusto-go/kusto/ingest/ingestoptions"
"github.com/Azure/azure-kusto-go/kusto/ingest/internal/gzip"
"github.com/Azure/azure-kusto-go/kusto/ingest/internal/properties"
"github.com/Azure/azure-kusto-go/kusto/ingest/internal/queued"
"github.com/Azure/azure-kusto-go/kusto/ingest/internal/utils"
"github.com/google/uuid"
)
type streamIngestor interface {
io.Closer
StreamIngest(ctx context.Context, db, table string, payload io.Reader, format kusto.DataFormatForStreaming, mappingName string, clientRequestId string, isBlobUri bool) error
}
// Streaming provides data ingestion from external sources into Kusto.
type Streaming struct {
db string
table string
client QueryClient
streamConn streamIngestor
}
type blobUri struct {
SourceUri string `json:"sourceUri"`
}
// NewStreaming is the constructor for Streaming.
// More information can be found here:
// https://docs.microsoft.com/en-us/azure/kusto/management/create-ingestion-mapping-command
func NewStreaming(client QueryClient, db, table string) (*Streaming, error) {
streamConn, err := kusto.NewConn(removeIngestPrefix(client.Endpoint()), client.Auth(), client.HttpClient(), client.ClientDetails())
if err != nil {
return nil, err
}
i := &Streaming{
db: db,
table: table,
client: client,
streamConn: streamConn,
}
return i, nil
}
// FromFile allows uploading a data file for Kusto from either a local path or a blobstore URI path.
// This method is thread-safe.
func (i *Streaming) FromFile(ctx context.Context, fPath string, options ...FileOption) (*Result, error) {
props := i.newProp()
file, err, local := prepFileAndProps(fPath, &props, options, StreamingClient)
if err != nil {
return nil, err
}
if !local {
return streamImpl(i.streamConn, ctx, generateBlobUriPayloadReader(fPath), props, true)
}
defer file.Close()
return streamImpl(i.streamConn, ctx, file, props, false)
}
// Returns the opened file, err, boolean indicator if its a local file
func prepFileAndProps(fPath string, props *properties.All, options []FileOption, client ClientScope) (*os.File, error, bool) {
var err error
for _, option := range options {
err := option.Run(props, client, FromFile)
if err != nil {
return nil, err, true
}
}
local, err := queued.IsLocalPath(fPath)
if err != nil {
return nil, err, local
}
if !local {
return nil, nil, false
}
props.Source.OriginalSource = fPath
compression := utils.CompressionDiscovery(fPath)
err = queued.CompleteFormatFromFileName(props, fPath)
if err != nil {
return nil, err, true
}
props.Source.DontCompress = !queued.ShouldCompress(props, compression)
file, err := os.Open(fPath)
if err != nil {
return nil, err, true
}
return file, nil, true
}
// FromReader allows uploading a data file for Kusto from an io.Reader. The content is uploaded to Blobstore and
// ingested after all data in the reader is processed. Content should not use compression as the content will be
// compressed with gzip. This method is thread-safe.
func (i *Streaming) FromReader(ctx context.Context, reader io.Reader, options ...FileOption) (*Result, error) {
props := i.newProp()
for _, prop := range options {
err := prop.Run(&props, StreamingClient, FromReader)
if err != nil {
return nil, err
}
}
return streamImpl(i.streamConn, ctx, reader, props, false)
}
func streamImpl(c streamIngestor, ctx context.Context, payload io.Reader, props properties.All, isBlobUri bool) (*Result, error) {
compress := queued.ShouldCompress(&props, ingestoptions.CTUnknown)
if compress && !isBlobUri {
payload = gzip.Compress(payload)
}
if props.Ingestion.Additional.Format == DFUnknown {
props.Ingestion.Additional.Format = CSV
}
err := c.StreamIngest(ctx, props.Ingestion.DatabaseName, props.Ingestion.TableName, payload, props.Ingestion.Additional.Format,
props.Ingestion.Additional.IngestionMappingRef,
props.Streaming.ClientRequestId,
isBlobUri)
if err != nil {
if e, ok := errors.GetKustoError(err); ok {
return nil, e
}
return nil, errors.E(errors.OpIngestStream, errors.KClientArgs, err)
}
err = props.ApplyDeleteLocalSourceOption()
if err != nil {
return nil, err
}
result := newResult()
result.putProps(props)
result.record.Status = "Success"
return result, nil
}
func (i *Streaming) newProp() properties.All {
return properties.All{
Ingestion: properties.Ingestion{
DatabaseName: i.db,
TableName: i.table,
},
Streaming: properties.Streaming{
ClientRequestId: "KGC.executeStreaming;" + uuid.New().String(),
},
}
}
func (i *Streaming) Close() error {
return i.streamConn.Close()
}
func generateBlobUriPayloadReader(fPath string) io.Reader {
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(
blobUri{
SourceUri: fPath,
},
)
return io.NopCloser(buf)
}