Skip to content

Commit

Permalink
Merge pull request #206 from k1LoW/new-bq-client
Browse files Browse the repository at this point in the history
Add datasource.NewBigqueryClient
  • Loading branch information
k1LoW authored Apr 21, 2020
2 parents cb187b4 + 6b21e75 commit 1aeb965
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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{}
Expand Down

0 comments on commit 1aeb965

Please sign in to comment.