From 6b21e753d433499251fda7954d304ff37a8edc31 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 21 Apr 2020 18:18:12 +0900 Subject: [PATCH] Add datasource.NewBigqueryClient --- datasource/datasource.go | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/datasource/datasource.go b/datasource/datasource.go index fdaaa496..80bc8fca 100644 --- a/datasource/datasource.go +++ b/datasource/datasource.go @@ -161,24 +161,8 @@ func AnalyzeJSONString(str string) (*schema.Schema, error) { // AnalyzeBigquery analyze `bq://` func AnalyzeBigquery(urlstr string) (*schema.Schema, error) { s := &schema.Schema{} - u, err := url.Parse(urlstr) - if err != nil { - return s, err - } - - values := u.Query() - err = setEnvGoogleApplicationCredentials(values) - if err != nil { - return s, err - } - - splitted := strings.Split(u.Path, "/") - - projectID := u.Host - datasetID := splitted[1] - ctx := context.Background() - client, err := bigquery.NewClient(ctx, projectID) + client, projectID, datasetID, err := NewBigqueryClient(ctx, urlstr) if err != nil { return s, err } @@ -196,6 +180,27 @@ func AnalyzeBigquery(urlstr string) (*schema.Schema, error) { return s, nil } +// NewBigqueryClient returns new bigquery.Client +func NewBigqueryClient(ctx context.Context, urlstr string) (*bigquery.Client, string, string, error) { + u, err := url.Parse(urlstr) + if err != nil { + return nil, "", "", err + } + values := u.Query() + err = setEnvGoogleApplicationCredentials(values) + if err != nil { + return nil, "", "", err + } + + splitted := strings.Split(u.Path, "/") + + projectID := u.Host + datasetID := splitted[1] + + client, err := bigquery.NewClient(ctx, projectID) + return client, projectID, datasetID, err +} + // AnalyzeSpanner analyze `spanner://` func AnalyzeSpanner(urlstr string) (*schema.Schema, error) { s := &schema.Schema{}