-
Notifications
You must be signed in to change notification settings - Fork 91
/
meta.go
101 lines (91 loc) · 2.71 KB
/
meta.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
95
96
97
98
99
100
101
package gohive
import (
"fmt"
"github.com/apache/thrift/lib/go/thrift"
"github.com/beltran/gohive/hive_metastore"
"os/user"
"strings"
)
type HiveMetastoreClient struct {
transport thrift.TTransport
Client *hive_metastore.ThriftHiveMetastoreClient
server string
port int
}
type MetastoreConnectConfiguration struct {
TransportMode string
Username string
Password string
}
func NewMetastoreConnectConfiguration() *MetastoreConnectConfiguration {
return &MetastoreConnectConfiguration{
TransportMode: "binary",
Username: "",
Password: "",
}
}
// Open connection to the metastore.
func ConnectToMetastore(host string, port int, auth string, configuration *MetastoreConnectConfiguration) (client *HiveMetastoreClient, err error) {
addr := fmt.Sprintf("%s:%d", host, port)
socket, err := thrift.NewTSocket(addr)
if err != nil {
return nil, fmt.Errorf("error resolving address %s: %v", host, err)
}
if err = socket.Open(); err != nil {
return
}
var transport thrift.TTransport
if configuration.TransportMode == "binary" {
if auth == "KERBEROS" {
saslConfiguration := map[string]string{"service": "hive", "javax.security.sasl.qop": auth, "javax.security.sasl.server.authentication": "true"}
transport, err = NewTSaslTransport(socket, host, "GSSAPI", saslConfiguration, DEFAULT_MAX_LENGTH)
if err != nil {
return
}
} else if auth == "NONE" {
if configuration.Password == "" {
configuration.Password = "x"
}
var _user *user.User
if configuration.Username == "" {
_user, err = user.Current()
if err != nil {
return
}
configuration.Username = strings.Replace(_user.Name, " ", "", -1)
}
saslConfiguration := map[string]string{"username": configuration.Username, "password": configuration.Password}
transport, err = NewTSaslTransport(socket, host, "PLAIN", saslConfiguration, DEFAULT_MAX_LENGTH)
if err != nil {
return
}
} else if auth == "NOSASL" {
transport = thrift.NewTBufferedTransport(socket, 4096)
if transport == nil {
return nil, fmt.Errorf("BufferedTransport was nil")
}
} else {
panic("Unrecognized auth")
}
} else {
panic("Unrecognized transport mode " + configuration.TransportMode)
}
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
iprot := protocolFactory.GetProtocol(transport)
oprot := protocolFactory.GetProtocol(transport)
c := hive_metastore.NewThriftHiveMetastoreClient(thrift.NewTStandardClient(iprot, oprot))
if !transport.IsOpen() {
if err = transport.Open(); err != nil {
return
}
}
return &HiveMetastoreClient{
transport: transport,
Client: c,
server: host,
port: port,
}, nil
}
func (c *HiveMetastoreClient) Close() {
c.transport.Close()
}