-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdriver.go
94 lines (84 loc) · 2.29 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gohive
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
bgohive "github.com/beltran/gohive"
hiveserver2 "sqlflow.org/gohive/hiveserver2/gen-go/tcliservice"
)
type drv struct{}
func (d drv) Open(dsn string) (driver.Conn, error) {
cfg, err := ParseDSN(dsn)
if err != nil {
return nil, err
}
tlsCfg, err := cfg.TLSCfg.Load()
if err != nil {
return nil, fmt.Errorf("load tls config: %w", err)
}
var socket thrift.TTransport
if tlsCfg != nil {
socket = thrift.NewTSSLSocketConf(
cfg.Addr,
&thrift.TConfiguration{TLSConfig: tlsCfg})
} else {
socket = thrift.NewTSocketConf(
cfg.Addr,
&thrift.TConfiguration{})
}
var transport thrift.TTransport
if cfg.Auth == "NOSASL" {
transport = thrift.NewTBufferedTransport(socket, 4096)
if transport == nil {
return nil, fmt.Errorf("BufferedTransport is nil")
}
} else if cfg.Auth == "PLAIN" || cfg.Auth == "GSSAPI" || cfg.Auth == "LDAP" {
saslCfg := map[string]string{
"username": cfg.User,
"password": cfg.Passwd,
}
bgTransport, err := bgohive.NewTSaslTransport(socket, cfg.Addr, cfg.Auth, saslCfg, bgohive.DEFAULT_MAX_LENGTH)
if err != nil {
return nil, fmt.Errorf("create SasalTranposrt failed: %v", err)
}
bgTransport.SetMaxLength(uint32(cfg.Batch))
transport = bgTransport
} else {
return nil, fmt.Errorf("unrecognized auth mechanism: %s", cfg.Auth)
}
if err = transport.Open(); err != nil {
return nil, err
}
protocol := thrift.NewTBinaryProtocolFactoryDefault()
client := hiveserver2.NewTCLIServiceClientFactory(transport, protocol)
s := hiveserver2.NewTOpenSessionReq()
s.ClientProtocol = hiveserver2.TProtocolVersion_HIVE_CLI_SERVICE_PROTOCOL_V6
if cfg.User != "" {
s.Username = &cfg.User
if cfg.Passwd != "" {
s.Password = &cfg.Passwd
}
}
config := cfg.SessionCfg
if cfg.DBName != "" {
config["use:database"] = cfg.DBName
}
s.Configuration = config
session, err := client.OpenSession(context.Background(), s)
if err != nil {
return nil, err
}
options := hiveOptions{PollIntervalSeconds: 5, BatchSize: int64(cfg.Batch)}
conn := &hiveConnection{
thrift: client,
session: session.SessionHandle,
options: options,
ctx: context.Background(),
}
return conn, nil
}
func init() {
sql.Register("hive", &drv{})
}