-
Notifications
You must be signed in to change notification settings - Fork 2
added Go example #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CodiePP
wants to merge
2
commits into
released
Choose a base branch
from
add_go_example
base: released
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
go.sum | ||
simple_query |
This file contains hidden or 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,31 @@ | ||
module example/simple_query | ||
|
||
go 1.19 | ||
|
||
require ( | ||
cloud.google.com/go/bigquery v1.44.0 | ||
google.golang.org/api v0.103.0 | ||
) | ||
|
||
require ( | ||
cloud.google.com/go v0.105.0 // indirect | ||
cloud.google.com/go/compute v1.12.1 // indirect | ||
cloud.google.com/go/compute/metadata v0.2.1 // indirect | ||
cloud.google.com/go/iam v0.7.0 // indirect | ||
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/go-cmp v0.5.9 // indirect | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect | ||
github.com/googleapis/gax-go/v2 v2.7.0 // indirect | ||
go.opencensus.io v0.24.0 // indirect | ||
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect | ||
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect | ||
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect | ||
golang.org/x/text v0.4.0 // indirect | ||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/genproto v0.0.0-20221117204609-8f9c96812029 // indirect | ||
google.golang.org/grpc v1.50.1 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
) |
This file contains hidden or 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,87 @@ | ||
/* Cardano on BigQuery | ||
|
||
example: simple query | ||
*/ | ||
|
||
|
||
/* | ||
authenticate: `gcloud auth application-default login` | ||
prepare: `go mod tidy -v` | ||
compile: `go build` | ||
run: `go run . --epoch_no 321` | ||
or: `./simple_query -epoch_no 321` | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
"cloud.google.com/go/bigquery" | ||
"google.golang.org/api/iterator" | ||
) | ||
|
||
func main() { | ||
|
||
epoch_no := flag.Int("epoch_no", 251, "Indicate the epoch number for which you want to query its last block.") | ||
flag.Parse() | ||
if *epoch_no < 0 { log.Fatal("wrong epoch number specified.") } | ||
|
||
ctx := context.Background() | ||
|
||
// the GCP/BigQuery project id | ||
projectID := "blockchain-analytics-392322" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh good catch! I didn't notice that! |
||
|
||
bqclient, err := bigquery.NewClient(ctx, projectID) | ||
if err != nil { log.Fatalf("bigquery.NewClient: %v", err) } | ||
defer bqclient.Close() | ||
|
||
rows, err := runQuery(ctx, bqclient, *epoch_no) | ||
if err != nil { log.Fatalf("run query: %v", err) } | ||
|
||
if err := printResults(rows); err != nil { log.Fatalf("print results: %v",err) } | ||
} | ||
|
||
func runQuery(ctx context.Context, client *bigquery.Client, p_epoch_no int) (*bigquery.RowIterator, error) { | ||
query := client.Query( | ||
`SELECT epoch_no, slot_no, block_time, block_size, tx_count, | ||
sum_tx_fee, script_count, sum_script_size, pool_hash | ||
FROM ` + "`cardano_mainnet.block`" + ` | ||
WHERE epoch_no = ` + fmt.Sprintf("%d",p_epoch_no) + ` | ||
ORDER BY slot_no DESC LIMIT 1;`) | ||
return query.Read(ctx) | ||
} | ||
|
||
type Block struct { | ||
EpochNo int64 `bigquery:"epoch_no"` | ||
SlotNo int64 `bigquery:"slot_no"` | ||
BlockTime bigquery.NullDateTime `bigquery:"block_time"` | ||
BlockSize int64 `bigquery:"block_size"` | ||
TxCount int64 `bigquery:"tx_count"` | ||
SumTxFee int64 `bigquery:"sum_tx_fee"` | ||
ScriptCount int64 `bigquery:"script_count"` | ||
ScriptSize int64 `bigquery:"sum_script_size"` | ||
PoolHash string `bigquery:"pool_hash"` | ||
} | ||
func block2String(b *Block) string { | ||
return fmt.Sprintf("epoch: %d, slot: %d, timestamp: %v, block sz: %d, tx count: %d, fees: %d, scripts: %d/%d bytes, pool: %v", | ||
b.EpochNo, b.SlotNo, b.BlockTime, b.BlockSize, b.TxCount, b.SumTxFee, b.ScriptCount, b.ScriptSize, b.PoolHash) | ||
} | ||
|
||
func printResults(iter *bigquery.RowIterator) error { | ||
for { | ||
var row Block | ||
err := iter.Next(&row) | ||
if err == iterator.Done { | ||
return nil | ||
} | ||
if err != nil { | ||
return fmt.Errorf("error iterating through results: %w", err) | ||
} | ||
|
||
fmt.Println(block2String(&row)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be useful to mention here that the script retrieves the last block of the epoch that gets as a parameter? Or extract this into a README? 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am stuck at the step
gcloud auth application-default login
..