Skip to content

Commit

Permalink
Adding pinot-live example code
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangfu0 committed Feb 27, 2022
1 parent 8129592 commit ebfadb4
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 13 deletions.
43 changes: 30 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Pinot Client GO
===============
# Pinot Client GO

[![GoDoc](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/startreedata/pinot-client-go)
[![Build Status](https://travis-ci.com/startreedata/pinot-client-go.svg?branch=master)](https://travis-ci.com/startreedata/pinot-client-go)
[![Coverage Status](https://coveralls.io/repos/github/startreedata/pinot-client-go/badge.svg?branch=master)](https://coveralls.io/github/startreedata/pinot-client-go?branch=master)
Expand All @@ -8,8 +8,9 @@ Pinot Client GO

Applications can use this golang client library to query Apache Pinot.

Examples
========
# Examples

## Local Pinot test

Please follow this [Pinot Quickstart](https://docs.pinot.apache.org/basics/getting-started/running-pinot-locally) link to install and start Pinot batch quickstart locally.

Expand All @@ -31,11 +32,18 @@ go build ./examples/batch-quickstart
./batch-quickstart
```

Usage
=====
## Pinot Live Demo cluster

Build and run the example application to query from Pinot Batch Quickstart

```
go build ./examples/pinot-live-demo
./pinot-live-demo
```

# Usage

Create a Pinot Connection
-------------------------
## Create a Pinot Connection

Pinot client could be initialized through:

Expand All @@ -47,8 +55,18 @@ pinotClient := pinot.NewFromZookeeper([]string{"localhost:2123"}, "", "QuickStar

2. A list of broker addresses.

- For HTTP
Default scheme is HTTP if not specified.

```
pinotClient := pinot.NewFromBrokerList([]string{"localhost:8000"})
pinotClient, err := pinot.NewFromBrokerList([]string{"localhost:8000"})
```

- For HTTPS
Scheme is required to be part of the URI.

```
pinotClient, err := pinot.NewFromBrokerList([]string{"https://pinot-broker.pinot.live"})
```

3. ClientConfig
Expand All @@ -66,12 +84,12 @@ pinotClient := pinot.NewWithConfig(&pinot.ClientConfig{
})
```

Query Pinot
-----------
## Query Pinot

Please see this [example](https://github.com/startreedata/pinot-client-go/blob/master/examples/batch-quickstart/main.go) for your reference.

Code snippet:

```
pinotClient, err := pinot.NewFromZookeeper([]string{"localhost:2123"}, "", "QuickStartCluster")
if err != nil {
Expand All @@ -84,8 +102,7 @@ if err != nil {
log.Infof("Query Stats: response time - %d ms, scanned docs - %d, total docs - %d", brokerResp.TimeUsedMs, brokerResp.NumDocsScanned, brokerResp.TotalDocs)
```

Response Format
---------------
## Response Format

Query Response is defined as the struct of following:

Expand Down
81 changes: 81 additions & 0 deletions examples/pinot-live-demo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"encoding/json"
"fmt"

pinot "github.com/startreedata/pinot-client-go/pinot"

log "github.com/sirupsen/logrus"
)

func main() {
pinotClient, err := pinot.NewFromBrokerList([]string{"https://pinot-broker.pinot.live"})
if err != nil {
log.Error(err)
}
table := "airlineStats"
pinotQueries := []string{
"select * from airlineStats limit 10",
"select count(*) as cnt from airlineStats limit 1",
"select count(*) as cnt, sum(ArrDelay) as sum_ArrDelay from airlineStats limit 1",
"select Dest, count(*) as cnt, sum(ArrDelay) as sum_ArrDelay from airlineStats group by Dest limit 10",
"select max(ActualElapsedTime) from airlineStats limit 10",
}

log.Infof("Querying SQL")
for _, query := range pinotQueries {
log.Infof("Trying to query Pinot: %v", query)
brokerResp, err := pinotClient.ExecuteSQL(table, query)
if err != nil {
log.Error(err)
}
printBrokerResp(brokerResp)
}

log.Infof("Querying PQL")
for _, query := range pinotQueries {
log.Infof("Trying to query Pinot: %v", query)
brokerResp, err := pinotClient.ExecutePQL(table, query)
if err != nil {
log.Error(err)
}
printBrokerResp(brokerResp)
}
}

func printBrokerResp(brokerResp *pinot.BrokerResponse) {
log.Infof("Query Stats: response time - %d ms, scanned docs - %d, total docs - %d", brokerResp.TimeUsedMs, brokerResp.NumDocsScanned, brokerResp.TotalDocs)
if brokerResp.Exceptions != nil && len(brokerResp.Exceptions) > 0 {
jsonBytes, _ := json.Marshal(brokerResp.Exceptions)
log.Infof("brokerResp.Exceptions:\n%s\n", jsonBytes)
return
}
if brokerResp.ResultTable != nil {
jsonBytes, _ := json.Marshal(brokerResp.ResultTable)
log.Infof("brokerResp.ResultTable:\n%s\n", jsonBytes)
line := ""
for c := 0; c < brokerResp.ResultTable.GetColumnCount(); c++ {
line += fmt.Sprintf("%s(%s)\t", brokerResp.ResultTable.GetColumnName(c), brokerResp.ResultTable.GetColumnDataType(c))
}
line += "\n"
for r := 0; r < brokerResp.ResultTable.GetRowCount(); r++ {
for c := 0; c < brokerResp.ResultTable.GetColumnCount(); c++ {
line += fmt.Sprintf("%v\t", brokerResp.ResultTable.Get(r, c))
}
line += "\n"
}
log.Infof("ResultTable:\n%s", line)
return
}
if brokerResp.AggregationResults != nil {
jsonBytes, _ := json.Marshal(brokerResp.AggregationResults)
log.Infof("brokerResp.AggregationResults:\n%s\n", jsonBytes)
return
}
if brokerResp.SelectionResults != nil {
jsonBytes, _ := json.Marshal(brokerResp.SelectionResults)
log.Infof("brokerResp.SelectionResults:\n%s\n", jsonBytes)
return
}
}

0 comments on commit ebfadb4

Please sign in to comment.