-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add read task in bigquery component (#156)
Because - we want to read from data components This commit - add read task to bigquery
- Loading branch information
1 parent
77fe2fc
commit 4d2e7ec
Showing
6 changed files
with
290 additions
and
18 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// TODO: chuang8511, add test code | ||
// It will be done before 2024-06-26. | ||
package bigquery |
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,59 @@ | ||
package bigquery | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"cloud.google.com/go/bigquery" | ||
"google.golang.org/api/iterator" | ||
) | ||
|
||
type ReadInput struct { | ||
ProjectID string | ||
DatasetID string | ||
TableName string | ||
Client *bigquery.Client | ||
Filtering string | ||
} | ||
|
||
type ReadOutput struct { | ||
Data []map[string]any `json:"data"` | ||
} | ||
|
||
func queryBuilder(input ReadInput) string { | ||
if input.Filtering == "" { | ||
return fmt.Sprintf("SELECT * FROM `%s.%s.%s`", input.ProjectID, input.DatasetID, input.TableName) | ||
} | ||
return fmt.Sprintf("SELECT * FROM `%s.%s.%s` %s", input.ProjectID, input.DatasetID, input.TableName, input.Filtering) | ||
} | ||
|
||
func readDataFromBigQuery(input ReadInput) (ReadOutput, error) { | ||
|
||
ctx := context.Background() | ||
client := input.Client | ||
|
||
sql := queryBuilder(input) | ||
q := client.Query(sql) | ||
it, err := q.Read(ctx) | ||
if err != nil { | ||
return ReadOutput{}, err | ||
} | ||
result := []map[string]any{} | ||
for { | ||
var values []bigquery.Value | ||
err := it.Next(&values) | ||
|
||
if err == iterator.Done { | ||
break | ||
} | ||
data := map[string]any{} | ||
|
||
for i, schema := range it.Schema { | ||
data[schema.Name] = values[i] | ||
} | ||
|
||
result = append(result, data) | ||
} | ||
|
||
return ReadOutput{Data: result}, nil | ||
} |