Implement select command (#2538)
This commit is contained in:
committed by
kannappanr
parent
a215fbb718
commit
a83ffe321c
@@ -95,6 +95,11 @@ func (f *fsClient) GetURL() clientURL {
|
||||
return *f.PathURL
|
||||
}
|
||||
|
||||
// Select replies a stream of query results.
|
||||
func (f *fsClient) Select(expression, sseKey string) (io.ReadCloser, *probe.Error) {
|
||||
return nil, probe.NewError(APINotImplemented{})
|
||||
}
|
||||
|
||||
// Watches for all fs events on an input path.
|
||||
func (f *fsClient) Watch(params watchParams) (*watchObject, *probe.Error) {
|
||||
eventChan := make(chan EventInfo)
|
||||
|
||||
@@ -23,11 +23,13 @@ import (
|
||||
"encoding/json"
|
||||
"hash/fnv"
|
||||
"io"
|
||||
"mime"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -384,6 +386,68 @@ func (c *s3Client) ListNotificationConfigs(arn string) ([]notificationConfig, *p
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
// Supported content types
|
||||
var supportedContentTypes = []string{
|
||||
"csv",
|
||||
"json",
|
||||
"gzip",
|
||||
"bzip2",
|
||||
}
|
||||
|
||||
func (c *s3Client) Select(expression, sseKey string) (io.ReadCloser, *probe.Error) {
|
||||
bucket, object := c.url2BucketAndObject()
|
||||
origContentType := mime.TypeByExtension(filepath.Ext(strings.TrimSuffix(strings.TrimSuffix(object, ".gz"), ".bz2")))
|
||||
contentType := mime.TypeByExtension(filepath.Ext(object))
|
||||
opts := minio.SelectObjectOptions{
|
||||
Expression: expression,
|
||||
ExpressionType: minio.QueryExpressionTypeSQL,
|
||||
}
|
||||
if strings.Contains(origContentType, "csv") {
|
||||
opts.InputSerialization = minio.SelectObjectInputSerialization{
|
||||
CompressionType: minio.SelectCompressionNONE,
|
||||
CSV: &minio.CSVInputOptions{
|
||||
FileHeaderInfo: minio.CSVFileHeaderInfoUse,
|
||||
RecordDelimiter: "\n",
|
||||
FieldDelimiter: ",",
|
||||
},
|
||||
}
|
||||
opts.OutputSerialization = minio.SelectObjectOutputSerialization{
|
||||
CSV: &minio.CSVOutputOptions{
|
||||
RecordDelimiter: "\n",
|
||||
FieldDelimiter: ",",
|
||||
},
|
||||
}
|
||||
} else if strings.Contains(origContentType, "json") {
|
||||
opts.InputSerialization = minio.SelectObjectInputSerialization{
|
||||
CompressionType: minio.SelectCompressionNONE,
|
||||
JSON: &minio.JSONInputOptions{
|
||||
Type: minio.JSONLinesType,
|
||||
},
|
||||
}
|
||||
opts.OutputSerialization = minio.SelectObjectOutputSerialization{
|
||||
JSON: &minio.JSONOutputOptions{
|
||||
RecordDelimiter: "\n",
|
||||
},
|
||||
}
|
||||
}
|
||||
if sseKey != "" {
|
||||
key, err := encrypt.NewSSEC([]byte(sseKey))
|
||||
if err == nil {
|
||||
opts.ServerSideEncryption = key
|
||||
}
|
||||
}
|
||||
if strings.Contains(contentType, "gzip") {
|
||||
opts.InputSerialization.CompressionType = minio.SelectCompressionGZIP
|
||||
} else if strings.Contains(contentType, "bzip") {
|
||||
opts.InputSerialization.CompressionType = minio.SelectCompressionBZIP
|
||||
}
|
||||
reader, e := c.api.SelectObjectContent(context.Background(), bucket, object, opts)
|
||||
if e != nil {
|
||||
return nil, probe.NewError(e)
|
||||
}
|
||||
return reader, nil
|
||||
}
|
||||
|
||||
// Start watching on all bucket events for a given account ID.
|
||||
func (c *s3Client) Watch(params watchParams) (*watchObject, *probe.Error) {
|
||||
eventChan := make(chan EventInfo)
|
||||
|
||||
@@ -58,6 +58,9 @@ type Client interface {
|
||||
// I/O operations
|
||||
Copy(source string, size int64, progress io.Reader, srcSSEKey, tgtSSEKey string) *probe.Error
|
||||
|
||||
// Runs select expression on object storage on specific files.
|
||||
Select(expression string, sseKey string) (io.ReadCloser, *probe.Error)
|
||||
|
||||
// I/O operations with metadata.
|
||||
Get(sseKey string) (reader io.Reader, err *probe.Error)
|
||||
Put(ctx context.Context, reader io.Reader, size int64, metadata map[string]string, progress io.Reader, sseKey string) (n int64, err *probe.Error)
|
||||
|
||||
@@ -277,6 +277,7 @@ func registerApp() *cli.App {
|
||||
registerCmd(cpCmd) // Copy objects and files from multiple sources to single destination.
|
||||
registerCmd(mirrorCmd) // Mirror objects and files from single source to multiple destinations.
|
||||
registerCmd(findCmd) // Find specific String patterns
|
||||
registerCmd(selectCmd) // Run select queries on a object or set of objects.
|
||||
registerCmd(statCmd) // Stat contents of a bucket/object
|
||||
registerCmd(diffCmd) // Computer differences between two files or folders.
|
||||
registerCmd(rmCmd) // Remove a file or bucket
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Minio Client, (C) 2018 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
)
|
||||
|
||||
var (
|
||||
selectFlags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "query, e",
|
||||
Usage: "Select query expression.",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "recursive, r",
|
||||
Usage: "Select query recursively.",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "encrypt-key",
|
||||
Usage: "Encrypt/Decrypt objects (using server-side encryption)",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// Display contents of a file.
|
||||
var selectCmd = cli.Command{
|
||||
Name: "select",
|
||||
Usage: "Run select queries on objects.",
|
||||
Action: mainSelect,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: append(selectFlags, globalFlags...),
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} [FLAGS] TARGET [TARGET...]
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. Run a query on a set of objects recursively on s3 account.
|
||||
$ {{.HelpName}} --recursive --query "select * from S3Object" s3/personalbucket/my-large-csvs/
|
||||
|
||||
2. Run a query on an object on minio account.
|
||||
$ {{.HelpName}} --query "select count(s.power) from S3Object" myminio/iot-devices/power-ratio.csv
|
||||
|
||||
3. Run a query on an encrypted object with client provided keys.
|
||||
$ {{.HelpName}} --encrypt-key "myminio/iot-devices=32byteslongsecretkeymustbegiven1" \
|
||||
--query "select count(s.power) from S3Object" myminio/iot-devices/power-ratio-encrypted.csv
|
||||
`,
|
||||
}
|
||||
|
||||
func selectQl(targetURL, expression string, encKeyDB map[string][]prefixSSEPair) *probe.Error {
|
||||
alias, _, _, err := expandAlias(targetURL)
|
||||
if err != nil {
|
||||
return err.Trace(targetURL)
|
||||
}
|
||||
|
||||
targetClnt, err := newClient(targetURL)
|
||||
if err != nil {
|
||||
return err.Trace(targetURL)
|
||||
}
|
||||
|
||||
sseKey := getSSEKey(targetURL, encKeyDB[alias])
|
||||
outputer, err := targetClnt.Select(expression, sseKey)
|
||||
if err != nil {
|
||||
return err.Trace(targetURL, expression)
|
||||
}
|
||||
defer outputer.Close()
|
||||
|
||||
_, e := io.Copy(os.Stdout, outputer)
|
||||
return probe.NewError(e)
|
||||
}
|
||||
|
||||
// check select input arguments.
|
||||
func checkSelectSyntax(ctx *cli.Context) {
|
||||
if !ctx.Args().Present() {
|
||||
cli.ShowCommandHelpAndExit(ctx, "select", 1) // last argument is exit code.
|
||||
}
|
||||
}
|
||||
|
||||
// mainSelect is the main entry point for select command.
|
||||
func mainSelect(ctx *cli.Context) error {
|
||||
// Parse encryption keys per command.
|
||||
encKeyDB, err := getEncKeys(ctx)
|
||||
fatalIf(err, "Unable to parse encryption keys.")
|
||||
|
||||
// validate select input arguments.
|
||||
checkSelectSyntax(ctx)
|
||||
|
||||
// extract URLs.
|
||||
URLs := ctx.Args()
|
||||
for _, url := range URLs {
|
||||
if !isAliasURLDir(url, encKeyDB) {
|
||||
errorIf(selectQl(url, ctx.String("query"), encKeyDB).Trace(url), "Unable to run select")
|
||||
continue
|
||||
}
|
||||
targetAlias, targetURL, _ := mustExpandAlias(url)
|
||||
clnt, err := newClientFromAlias(targetAlias, targetURL)
|
||||
if err != nil {
|
||||
errorIf(err.Trace(url), "Unable to initialize target `"+url+"`.")
|
||||
continue
|
||||
}
|
||||
|
||||
for content := range clnt.List(ctx.Bool("recursive"), false, DirNone) {
|
||||
if content.Err != nil {
|
||||
errorIf(content.Err.Trace(url), "Unable to list on target `"+url+"`.")
|
||||
continue
|
||||
}
|
||||
contentType := mime.TypeByExtension(filepath.Ext(content.URL.Path))
|
||||
for _, cTypeSuffix := range supportedContentTypes {
|
||||
if strings.Contains(contentType, cTypeSuffix) {
|
||||
errorIf(selectQl(targetAlias+content.URL.Path, ctx.String("expression"),
|
||||
encKeyDB).Trace(content.URL.String()), "Unable to run select")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Done.
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user