-
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
124 additions
and
2 deletions.
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,116 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
// Imports the Google Cloud BigQuery client package. | ||
bq "cloud.google.com/go/bigquery" | ||
"golang.org/x/net/context" | ||
"google.golang.org/api/iterator" | ||
) | ||
|
||
type BigQueryTarget struct { | ||
Target | ||
Client *bq.Client | ||
} | ||
|
||
func (bqt BigQueryTarget) IsConnectable() bool { | ||
var err error = nil | ||
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: Failed to perform test query: %v", err) | ||
return false | ||
} | ||
|
||
var row []bq.Value | ||
err = it.Next(&row) | ||
if err != nil { | ||
log.Printf("ERROR: Failed to read test query results: %v", err) | ||
return false | ||
} | ||
|
||
return fmt.Sprint(row) == "[1]" | ||
} | ||
|
||
func NewBigQueryTarget(target Target) *BigQueryTarget { | ||
projectID := target.Project | ||
ctx := context.Background() | ||
|
||
client, err := bq.NewClient(ctx, projectID) | ||
if err != nil { | ||
log.Fatalf("ERROR: Failed to create client: %v", err) | ||
} | ||
|
||
return &BigQueryTarget{target,client} | ||
} | ||
|
||
func (bqt BigQueryTarget) GetTarget() Target { | ||
return bqt.Target | ||
} | ||
|
||
// Run a query against the target | ||
// One statement per API call | ||
func (bqt BigQueryTarget) RunQuery(query ReadyQuery, dryRun bool, showQueryOutput bool) QueryStatus { | ||
var affected int64 = 0 | ||
var err error = nil | ||
ctx := context.Background() | ||
|
||
if dryRun { | ||
if bqt.IsConnectable() { | ||
log.Printf("SUCCESS: Able to connect to target database, %s.", bqt.Project) | ||
} else { | ||
log.Printf("ERROR: Cannot connect to target database, %s.", bqt.Project) | ||
} | ||
return QueryStatus{query, query.Path, 0, nil} | ||
} | ||
|
||
scripts := strings.Split(query.Script, ";") | ||
|
||
for _, script := range scripts { | ||
if len(strings.TrimSpace(script)) > 0 { | ||
q := bqt.Client.Query(script) | ||
it, err := q.Read(ctx) | ||
|
||
if err != nil { | ||
log.Printf("ERROR: %s.", err) | ||
return QueryStatus{query, query.Path, int(affected), err} | ||
} | ||
|
||
if showQueryOutput { | ||
// TODO: replace with table output | ||
for { | ||
var values []bq.Value | ||
err := it.Next(&values) | ||
if err == iterator.Done { | ||
break | ||
} | ||
if err != nil { | ||
log.Printf("ERROR: %s.", err) | ||
return QueryStatus{query, query.Path, int(affected), err} | ||
} | ||
fmt.Println(values) | ||
} | ||
} else { | ||
jobId := q.JobID | ||
job, err := bqt.Client.JobFromID(ctx, jobId) | ||
|
||
if err != nil { | ||
log.Printf("ERROR: %s.", err) | ||
return QueryStatus{query, query.Path, int(affected), err} | ||
} | ||
|
||
aff := job.LastStatus().Statistics.Details.(*bq.QueryStatistics).NumDMLAffectedRows | ||
affected += aff | ||
} | ||
} | ||
} | ||
|
||
return QueryStatus{query, query.Path, int(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