-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdfsstorage.go
60 lines (52 loc) · 1.26 KB
/
hdfsstorage.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
package main
import (
"log"
"github.com/colinmarc/hdfs" // NOTE: requires the write support branch for now!
)
/*
hdfsStorage is a storage implementation that reads and writes data via a Hadoop
file system.
Implements encrypted/Storage interface.
*/
type hdfsStorage struct {
client *hdfs.Client
}
/*
createHDFSStorage creates a structure that writes all files to a Hadoop file system.
*/
func createHDFSStorage(address, user string) (*hdfsStorage, error) {
// connect to URL
client, err := hdfs.NewForUser(address, user)
if err != nil {
return nil, err
}
return &hdfsStorage{
client: client}, nil
}
func (h *hdfsStorage) Store(key string, data []byte) error {
// if it already exists first delete the old version
_, err := h.client.Stat(key)
if err != nil {
err := h.client.Remove(key)
if err != nil {
log.Println("Failed to remove previous version!", err)
return err
}
}
// try writing file
fw, err := h.client.Create(key)
if err != nil {
return err
}
// defer close for all cases
defer fw.Close()
// actually write data to file
_, err = fw.Write(data)
return err
}
func (h *hdfsStorage) Retrieve(key string) ([]byte, error) {
return h.client.ReadFile(key)
}
func (h *hdfsStorage) Remove(key string) error {
return h.client.Remove(key)
}