dongzhi7641 2014-09-23 08:54
浏览 370

如何使用golang连接到配置单元

I use https://github.com/apache/hive/blob/trunk/service/if/TCLIService.thrift to gen golang sdk, but it ddn't work. The golang sdk can only opensession. I compare my code within python sdk and didn't find any error usage. below is my golang code.

socket, err := thrift.NewTSocket("localhost:11000")
if err != nil {
    fmt.Printf("%s
", err)
    return
}
trans := thrift.NewTBufferedTransport(socket, 1024)

protocol := thrift.NewTBinaryProtocol(trans, true, true)
client := hive.NewTCLIServiceClientProtocol(trans, protocol, protocol)
trans.Open()
defer trans.Close()

sReq := hive.NewTOpenSessionReq()
sReq.ClientProtocol = 0

session, err := client.OpenSession(sReq)
fmt.Printf("!%#v %s
", session, err)

exeReq := hive.NewTExecuteStatementReq()
exeReq.SessionHandle = session.SessionHandle
exeReq.Statement = "USE default"

result, err := client.ExecuteStatement(exeReq)
fmt.Printf("result: %s !! %s
", result, err)

and the output is :

!&hive.TOpenSessionResp{Status:(*hive.TStatus)(0xc21001e460), ServerProtocolVersion:0, SessionHandle:(*hive.TSessionHandle)(0xc2100002b0), Configuration:map[string]string{}} %!s(<nil>)

result: <nil> !! Required field 'guid' is unset! Struct:THandleIdentifier(guid:null, secret:null)

Required field 'guid' is unset! Struct:THandleIdentifier(guid:null, secret:null) is return by the server. I have try other hive functions, always the same error.

Is there something wrong with my code or thirft didn't support golang yet?

  • 写回答

1条回答 默认 最新

  • dongxi9326 2014-09-28 01:49
    关注

    I have found the reason. In the thrift generated file ttypes.go, the THandleIdentifier struct write guid and secret using binary protocol, and I found official Python SDK write guid and secrete use WriteString function of binary protocol, so I modify the code manual.

    func (p *THandleIdentifier) writeField1(oprot thrift.TProtocol) (err error) {
    if p.Guid != nil {
    
        // the origin code is:
        //if err := oprot.WriteFieldBegin("guid", thrift.BINARY, 1); err != nil {
    
        if err := oprot.WriteFieldBegin("guid", thrift.STRING, 1); err != nil {
            return fmt.Errorf("%T write field begin error 1:guid: %s", p, err)
        }
    
        //the origin code is:
        //if err := oprot.WriteString(p.Guid; err != nil {
        if err := oprot.WriteString(string(p.Guid); err != nil {
            return fmt.Errorf("%T.guid (1) field write error: %s", p)
        }
        if err := oprot.WriteFieldEnd(); err != nil {
            return fmt.Errorf("%T write field end error 1:guid: %s", p, err)
        }
    }
      return err
    }
    

    and the same with secret property need to modify.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度