From de1098747b07956aef204cf355f215df7cf73bc1 Mon Sep 17 00:00:00 2001 From: Alexander Diemand Date: Thu, 3 Aug 2023 22:19:45 +0200 Subject: [PATCH 1/2] added Go example Signed-off-by: Alexander Diemand --- examples/simple_query_in_go/.gitignore | 2 + examples/simple_query_in_go/go.mod | 31 +++++++++ examples/simple_query_in_go/main.go | 87 ++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 examples/simple_query_in_go/.gitignore create mode 100644 examples/simple_query_in_go/go.mod create mode 100644 examples/simple_query_in_go/main.go diff --git a/examples/simple_query_in_go/.gitignore b/examples/simple_query_in_go/.gitignore new file mode 100644 index 0000000..d7661fd --- /dev/null +++ b/examples/simple_query_in_go/.gitignore @@ -0,0 +1,2 @@ +go.sum +simple_query diff --git a/examples/simple_query_in_go/go.mod b/examples/simple_query_in_go/go.mod new file mode 100644 index 0000000..5a735e8 --- /dev/null +++ b/examples/simple_query_in_go/go.mod @@ -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 +) diff --git a/examples/simple_query_in_go/main.go b/examples/simple_query_in_go/main.go new file mode 100644 index 0000000..77c5b0e --- /dev/null +++ b/examples/simple_query_in_go/main.go @@ -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 := "iog-data-analytics" + + 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)) + } +} From 1af2e90aab4813d4c3206006343684a952fbedc1 Mon Sep 17 00:00:00 2001 From: Alexander Diemand Date: Sun, 6 Aug 2023 11:45:55 +0200 Subject: [PATCH 2/2] change project id Signed-off-by: Alexander Diemand --- examples/simple_query_in_go/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_query_in_go/main.go b/examples/simple_query_in_go/main.go index 77c5b0e..b1ed050 100644 --- a/examples/simple_query_in_go/main.go +++ b/examples/simple_query_in_go/main.go @@ -33,7 +33,7 @@ func main() { ctx := context.Background() // the GCP/BigQuery project id - projectID := "iog-data-analytics" + projectID := "blockchain-analytics-392322" bqclient, err := bigquery.NewClient(ctx, projectID) if err != nil { log.Fatalf("bigquery.NewClient: %v", err) }