Total Lines | 56 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package files |
||
2 | |||
3 | import ( |
||
4 | "os/user" |
||
5 | |||
6 | "github.com/colinmarc/hdfs" |
||
7 | "github.com/colinmarc/hdfs/rpc" |
||
8 | ) |
||
9 | |||
10 | // FileSystem defines a simple pointer to an HDFS file system. |
||
11 | type FileSystem struct { |
||
12 | |||
13 | // Name is a unique identifier for the file system. |
||
14 | Name string |
||
15 | |||
16 | // Description should be a brief note. |
||
17 | Description string |
||
18 | |||
19 | // Addresses are a list of the name nodes for the file system. |
||
20 | Addresses []string |
||
21 | } |
||
22 | |||
23 | func (r *FileSystem) user() (string, error) { |
||
24 | user, err := user.Current() |
||
25 | if err == nil { |
||
26 | return user.Username, nil |
||
27 | } |
||
28 | return "", err |
||
29 | } |
||
30 | |||
31 | // Connect opens a FileSystemClient. |
||
32 | func (r *FileSystem) Connect() (FileSystemClient, error) { |
||
33 | user, err := r.user() |
||
34 | if err == nil { |
||
35 | if conn, nerr := rpc.NewNamenodeConnectionWithOptions( |
||
36 | rpc.NamenodeConnectionOptions{ |
||
37 | Addresses: r.Addresses, |
||
38 | User: user, |
||
39 | }, |
||
40 | ); nerr == nil { |
||
41 | client, cerr := hdfs.NewClient( |
||
42 | hdfs.ClientOptions{ |
||
43 | Addresses: r.Addresses, |
||
44 | Namenode: conn, |
||
45 | User: user, |
||
46 | }, |
||
47 | ) |
||
48 | if cerr == nil { |
||
49 | return CreateHDFSFileSystemClient(r.Name, client), err |
||
50 | } |
||
51 | return nil, cerr |
||
52 | } else { |
||
53 | return nil, nerr |
||
54 | } |
||
55 | } |
||
56 | return nil, err |
||
57 | } |
||
58 |