Skip to content

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
wants to merge 2 commits into
base: released
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/simple_query_in_go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.sum
simple_query
31 changes: 31 additions & 0 deletions examples/simple_query_in_go/go.mod
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
)
87 changes: 87 additions & 0 deletions examples/simple_query_in_go/main.go
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`
*/
Copy link
Contributor

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? 🤷

Copy link
Member Author

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 ..


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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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))
}
}