您的位置:首页 > 编程语言 > Go语言

GoHive:支持SASL的go语言Hive客户端

2017-12-29 17:02 676 查看

简介

https://github.com/lwldcr/gohive

花了一周时间才调通,看log配合Hive的源码一步步debug相当费事,基本是按照pyhs2来实现的,支持SASL协议Plain模式,其他模式看后续是否需要酌情增加。客户端、查询操作返回数据还可以进一步封装,提供更便捷的使用,后面再更新吧。

欢迎试用、反馈。

GoHive

A hive client with SASL support based on thrift.

Usage

package main

import (
"fmt"
"tcliservice"
"github.com/lwldcr/gohive"
"os"
)

func main() {
// first build a new transport
// of course we can wrap all these routine operations into functions for repeatedly using
t, err := gohive.NewTSaslTransport(HIVE_HOST, PORT, HIVE_USER, HIVE_PASSWD)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// now open it
if err := t.Open(); err != nil {
fmt.Println(err)
os.Exit(1)
}

// then you get a session, and you can execute query now
sessionHandler := t.Session
execReq := tcliservice.NewTExecuteStatementReq()
execReq.SessionHandle = sessionHandler
execReq.Statement = "show databases"
execResp, err := t.Client.ExecuteStatement(execReq)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// fetch result after executing
operationHandler := execResp.GetOperationHandle()
fetchReq := tcliservice.NewTFetchResultsReq()
fetchReq.OperationHandle = operationHandler
fetchReq.Orientation = tcliservice.TFetchOrientation_FETCH_FIRST
fetchReq.MaxRows = 10
fetchResp, err := t.Client.FetchResults(fetchReq)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// there you get your data
data := make([]string, 0)
res := fetchResp.GetResults().GetRows()
for _,  r := range res {
row := r.GetColVals()
for _, field := range row {
data = append(data, field.GetStringVal().GetValue())
}
}

fmt.Println("data:", data)

// do cleaning
closeOperationReq := tcliservice.NewTCloseOperationReq()
closeOperationReq.OperationHandle = operationHandler
t.Client.CloseOperation(closeOperationReq)
t.Close()
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  go语言 hive thrift sasl