forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
132 lines (109 loc) · 3.23 KB
/
client.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package hdfs
import (
"github.com/golang/protobuf/proto"
hdfs "github.com/colinmarc/hdfs/protocol/hadoop_hdfs"
"github.com/colinmarc/hdfs/rpc"
"io"
"io/ioutil"
"os"
"os/user"
)
// A Client represents a connection to an HDFS cluster
type Client struct {
namenode *rpc.NamenodeConnection
defaults *hdfs.FsServerDefaultsProto
}
// New returns a connected Client, or an error if it can't connect. The user
// will be the user the code is running under.
func New(address string) (*Client, error) {
currentUser, err := user.Current()
if err != nil {
return nil, err
}
return NewForUser(address, currentUser.Username)
}
// NewForUser returns a connected Client with the user specified, or an error if
// it can't connect.
func NewForUser(address string, user string) (*Client, error) {
namenode, err := rpc.NewNamenodeConnection(address, user)
if err != nil {
return nil, err
}
return &Client{namenode: namenode}, nil
}
// ReadFile reads the file named by filename and returns the contents.
func (c *Client) ReadFile(filename string) ([]byte, error) {
f, err := c.Open(filename)
if err != nil {
return nil, err
}
return ioutil.ReadAll(f)
}
// CopyToLocal copies the HDFS file specified by src to the local file at dst.
// If dst already exists, it will be overwritten.
func (c *Client) CopyToLocal(src string, dst string) error {
remote, err := c.Open(src)
if err != nil {
return err
}
local, err := os.Create(dst)
if err != nil {
return err
}
_, err = io.Copy(local, remote)
return err
}
// CreateEmptyFile creates a empty file named by filename, with the permissions
// 0644.
func (c *Client) CreateEmptyFile(filename string) error {
_, err := c.getFileInfo(filename)
if err == nil {
return &os.PathError{"create", filename, os.ErrExist}
} else if !os.IsNotExist(err) {
return &os.PathError{"create", filename, err}
}
defaults, err := c.fetchDefaults()
if err != nil {
return err
}
createReq := &hdfs.CreateRequestProto{
Src: proto.String(filename),
Masked: &hdfs.FsPermissionProto{Perm: proto.Uint32(uint32(0644))},
ClientName: proto.String(rpc.ClientName),
CreateFlag: proto.Uint32(1),
CreateParent: proto.Bool(false),
Replication: proto.Uint32(defaults.GetReplication()),
BlockSize: proto.Uint64(defaults.GetBlockSize()),
}
createResp := &hdfs.CreateResponseProto{}
err = c.namenode.Execute("create", createReq, createResp)
if err != nil {
if nnErr, ok := err.(*rpc.NamenodeError); ok {
err = interpretException(nnErr.Exception, err)
}
return &os.PathError{"create", filename, err}
}
completeReq := &hdfs.CompleteRequestProto{
Src: proto.String(filename),
ClientName: proto.String(rpc.ClientName),
}
completeResp := &hdfs.CompleteResponseProto{}
err = c.namenode.Execute("complete", completeReq, completeResp)
if err != nil {
return &os.PathError{"create", filename, err}
}
return nil
}
func (c *Client) fetchDefaults() (*hdfs.FsServerDefaultsProto, error) {
if c.defaults != nil {
return c.defaults, nil
}
req := &hdfs.GetServerDefaultsRequestProto{}
resp := &hdfs.GetServerDefaultsResponseProto{}
err := c.namenode.Execute("getServerDefaults", req, resp)
if err != nil {
return nil, err
}
c.defaults = resp.GetServerDefaults()
return c.defaults, nil
}