/
hdfsstorage.go
52 lines (44 loc) · 1.12 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
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(url string) (*hdfsStorage, error) {
// connect to URL
client, err := hdfs.New(url) // FIXME: use NewForUser()
if err != nil {
return nil, err
}
return &hdfsStorage{
client: client}, nil
}
func (h *hdfsStorage) Store(key string, data []byte) error {
fw, err := h.client.Create(key)
if err != nil {
// TODO test and FIXME
// probably that is already existed... :(
log.Println("DEBUG: probably that file already exists:", err)
}
// defer close for all cases
defer fw.Close()
// actually write data
_, 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)
}