Golang
Golang
Pinot Client for Golang.
Pinot는 Go 애플리케이션에서 데이터베이스를 직접 쿼리할 수 있는 네이티브 Go 클라이언트를 제공해요.
출처: 문서
본문
설치
- 이 Pinot quickstart를 따라 로컬에 Pinot를 설치하고 시작하세요.
bin/quick-start-batch.sh
- 클라이언트 라이브러리 GitHub 저장소를 확인하세요.
git clone [email protected]:startreedata/pinot-client-go.git
cd pinot-client-go
- 예시 애플리케이션을 빌드하고 실행해 Pinot batch quickstart에서 쿼리하세요.
go build ./examples/batch-quickstart
./batch-quickstart
Golang 클라이언트 사용
Pinot 연결 생성
아래 방법 중 하나로 Pinot 클라이언트를 초기화하세요.
Zookeeper 경로
pinotClient := pinot.NewFromZookeeper([]string{"localhost:2123"}, "", "QuickStartCluster")
Broker 주소 목록
pinotClient := pinot.NewFromBrokerList([]string{"localhost:8000"})
ClientConfig
Go 클라이언트 저장소에는 예시 스크립트가 있어요.
참고: ExtraHTTPHeader에서 헤더로 "content-type"을 구성할 필요는 없어요.
pinotClient := pinot.NewWithConfig(&pinot.ClientConfig{
ZkConfig: &pinot.ZookeeperConfig{
ZookeeperPath: zkPath,
PathPrefix: strings.Join([]string{zkPathPrefix, pinotCluster}, "/"),
SessionTimeoutSec: defaultZkSessionTimeoutSec,
},
ExtraHTTPHeader: map[string]string{
"extra-header": "value",
},
})
HTTP 클라이언트가 있는 ClientConfig
특수한 HTTP 클라이언트가 필요하다면 자신만의 HTTP 클라이언트를 만들고 NewWithConfigAndClient 함수를 사용해 사용자 지정 HTTP 클라이언트를 수용하는 Pinot 클라이언트를 만들 수 있어요.
Go 클라이언트 저장소에는 예시 스크립트가 있어요.
Pinot 쿼리
Go 클라이언트 저장소에는 예시 스크립트가 있어요.
코드 스니펫:
pinotClient, err := pinot.NewFromZookeeper([]string{"localhost:2123"}, "", "QuickStartCluster")
if err != nil {
log.Error(err)
}
brokerResp, err := pinotClient.ExecuteSQL("baseballStats", "select count(*) as cnt, sum(homeRuns) as sum_homeRuns from baseballStats group by teamID limit 10")
if err != nil {
log.Error(err)
}
log.Infof("Query Stats: response time - %d ms, scanned docs - %d, total docs - %d", brokerResp.TimeUsedMs, brokerResp.NumDocsScanned, brokerResp.TotalDocs)
응답 형식
쿼리 응답은 다음 형식을 가져요.
type BrokerResponse struct {
AggregationResults []*AggregationResult `json:"aggregationResults,omitempty"`
SelectionResults *SelectionResults `json:"SelectionResults,omitempty"`
ResultTable *ResultTable `json:"resultTable,omitempty"`
Exceptions []Exception `json:"exceptions"`
TraceInfo map[string]string `json:"traceInfo,omitempty"`
NumServersQueried int `json:"numServersQueried"`
NumServersResponded int `json:"numServersResponded"`
NumSegmentsQueried int `json:"numSegmentsQueried"`
NumSegmentsProcessed int `json:"numSegmentsProcessed"`
NumSegmentsMatched int `json:"numSegmentsMatched"`
NumConsumingSegmentsQueried int `json:"numConsumingSegmentsQueried"`
NumDocsScanned int64 `json:"numDocsScanned"`
NumEntriesScannedInFilter int64 `json:"numEntriesScannedInFilter"`
NumEntriesScannedPostFilter int64 `json:"numEntriesScannedPostFilter"`
NumGroupsLimitReached bool `json:"numGroupsLimitReached"`
TotalDocs int64 `json:"totalDocs"`
TimeUsedMs int `json:"timeUsedMs"`
MinConsumingFreshnessTimeMs int64 `json:"minConsumingFreshnessTimeMs"`
}
AggregationResults와 SelectionResults는 Pinot 0.7.1부터 deprecated된 레거시 PQL(Pinot Query Language) 쿼리의 보관자입니다. SQL API와 ResultTable을 대신 사용하세요.
한편 ResultTable은 SQL 쿼리의 보관자입니다. ResultTable은 다음과 같이 정의돼요.
// ResultTable is a ResultTable
type ResultTable struct {
DataSchema RespSchema `json:"dataSchema"`
Rows [][]interface{} `json:"rows"`
}
RespSchema는 다음과 같이 정의돼요.
// RespSchema is response schema
type RespSchema struct {
ColumnDataTypes []string `json:"columnDataTypes"`
ColumnNames []string `json:"columnNames"`
}
ResultTable에는 다음과 같은 여러 함수가 정의돼 있어요.
func (r ResultTable) GetRowCount() int
func (r ResultTable) GetColumnCount() int
func (r ResultTable) GetColumnName(columnIndex int) string
func (r ResultTable) GetColumnDataType(columnIndex int) string
func (r ResultTable) Get(rowIndex int, columnIndex int) interface{}
func (r ResultTable) GetString(rowIndex int, columnIndex int) string
func (r ResultTable) GetInt(rowIndex int, columnIndex int) int
func (r ResultTable) GetLong(rowIndex int, columnIndex int) int64
func (r ResultTable) GetFloat(rowIndex int, columnIndex int) float32
func (r ResultTable) GetDouble(rowIndex int, columnIndex int) float64
사용 중인 함수의 예시는 여기와 아래에서 볼 수 있어요.
// Print Response Schema
for c := 0; c < brokerResp.ResultTable.GetColumnCount(); c++ {
fmt.Printf("%s(%s)\t", brokerResp.ResultTable.GetColumnName(c), brokerResp.ResultTable.GetColumnDataType(c))
}
fmt.Println()
// Print Row Table
for r := 0; r < brokerResp.ResultTable.GetRowCount(); r++ {
for c := 0; c < brokerResp.ResultTable.GetColumnCount(); c++ {
fmt.Printf("%v\t", brokerResp.ResultTable.Get(r, c))
}
fmt.Println()
}