-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"google.golang.org/api/option" | ||
"log" | ||
|
||
// Imports the Google Cloud BigQuery client package. | ||
bq "cloud.google.com/go/bigquery" | ||
"golang.org/x/net/context" | ||
"golang.org/x/oauth2/google" | ||
) | ||
|
||
type BigQueryTarget struct { | ||
Target | ||
Client *bq.Client | ||
} | ||
|
||
func (bqt BigQueryTarget) IsConnectable() bool { | ||
ctx := context.Background() | ||
client := bqt.Client | ||
query := client.Query("SELECT 1") // empty query to test connection | ||
it, err := query.Read(ctx) | ||
if err != nil { | ||
log.Printf("Error while performing test query: %v", err) | ||
return false | ||
} | ||
var row []bq.Value | ||
itErr := it.Next(&row) | ||
if itErr != nil { | ||
log.Printf("Error while reading test query results: %v", itErr) | ||
return false | ||
} | ||
return fmt.Sprint(row) == "[1]" | ||
} | ||
|
||
func NewBigQueryTarget(target Target) *BigQueryTarget { | ||
projectID := target.Project | ||
ctx := context.Background() | ||
|
||
creds, err := google.CredentialsFromJSON(ctx, []byte(target.Credentials), bq.Scope) | ||
if err != nil { | ||
log.Fatalf("Failed to create client: %v", err) | ||
} | ||
|
||
client, err := bq.NewClient(ctx, projectID, option.WithCredentials(creds)) | ||
if err != nil { | ||
log.Fatalf("Failed to create client: %v", err) | ||
} | ||
|
||
return &BigQueryTarget{target,client} | ||
} | ||
|
||
func (bqt BigQueryTarget) GetTarget() Target { | ||
return bqt.Target | ||
} | ||
|
||
func (bqt BigQueryTarget) RunQuery(query ReadyQuery, dryRun bool, showQueryOutput bool) QueryStatus { | ||
var err error = nil | ||
ctx := context.Background() | ||
if dryRun { | ||
if bqt.IsConnectable() { | ||
log.Printf("SUCCESS: Able to connect to target database.") | ||
} else { | ||
log.Printf("ERROR: Cannot connect to target database.") | ||
} | ||
return QueryStatus{query, query.Path, 0, nil} | ||
} | ||
|
||
affected := 0 // TODO are affected rows returned by the client library? | ||
q := bqt.Client.Query(query.Script) | ||
it, err := q.Read(ctx) | ||
if err != nil { | ||
log.Printf("ERROR: %s.", err) | ||
return QueryStatus{query, query.Path, int(affected), err} | ||
} | ||
println(it) | ||
return QueryStatus{query, query.Path, affected, err} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters