Repository navigation
Expand file tree
/
Copy pathHdfsDAO.java
More file actions
163 lines (144 loc) · 4.68 KB
/
Copy pathHdfsDAO.java
File metadata and controls
163 lines (144 loc) · 4.68 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package DAO;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.mapred.JobConf;
import java.io.IOException;
import java.net.URI;
/**
* HDFS的增删改查
*/
public class HdfsDAO
{
//HDFS访问地址
//private static final String HDFS = "hdfs://localhost:8020";
private static final String HDFS = "hdfs://master:9000";
private HdfsDAO(Configuration conf)
{
this(HDFS, conf);
}
public HdfsDAO(String hdfs, Configuration conf)
{
this.hdfsPath = hdfs;
this.conf = conf;
}
//hdfs路径
private String hdfsPath;
//Hadoop系统配置
private Configuration conf;
//启动函数
public static void main(String[] args) throws IOException
{
JobConf conf = config();
HdfsDAO hdfs = new HdfsDAO(conf);
//hdfs.mkdirs("/tmp/new/two");
hdfs.ls("/");
hdfs.copyFile("src/main/data/input/recommend/step1/output/par*","/boy/love");
//hdfs.download("/boy/recommend/step/step1/output","src/main/data/output/recommend/step1");
// hdfs.mkdirs("/boy/love");
//
// hdfs.ls("/");
//
// hdfs.rm("/boy");
//
// hdfs.ls("/");
}
//加载Hadoop配置文件
private static JobConf config()
{
JobConf conf = new JobConf(HdfsDAO.class);
conf.setJobName("other.HdfsDAO");
return conf;
}
//API实现
private void cat(String remoteFile) throws IOException
{
Path path = new Path(remoteFile);
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
FSDataInputStream fsdis = null;
System.out.println("cat: " + remoteFile);
try
{
fsdis = fs.open(path);
IOUtils.copyBytes(fsdis, System.out, 4096, false);
} finally
{
IOUtils.closeStream(fsdis);
fs.close();
}
}
public void rm(String folder) throws IOException
{
Path path = new Path(folder);
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
fs.deleteOnExit(path);
System.out.println("Delete: " + folder);
fs.close();
}
public void mkdirs(String folder) throws IOException
{
Path path = new Path(folder);
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
if (!fs.exists(path))
{
fs.mkdirs(path);
System.out.println("Create: " + folder);
}
fs.close();
}
private void ls(String folder) throws IOException
{
Path path = new Path(folder);
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
System.out.println("ls: " + folder);
/*
FileStatus[] list = fs.listStatus(path);
System.out.println("ls: " + folder);
System.out.println("==========================================================");
for (LocatedFileStatus f : list)
{
System.out.printf("name: %s, folder: %s, size: %d\n", f.getPath(), f.isDir(), f.getLen());
}
*/
RemoteIterator<LocatedFileStatus> list = fs.listFiles(path, true);
while (list.hasNext())
{
LocatedFileStatus status = list.next();
System.out.printf("name: %s, folder: %s, size: %d\n", status.getPath(), status.isDirectory(), status.getLen());
}
System.out.println("==========================================================");
fs.close();
}
public void copyFile(String local, String remote) throws IOException
{
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
fs.copyFromLocalFile(new Path(local), new Path(remote));
System.out.println("copy from: " + local + " to " + remote);
fs.close();
}
public void download(String remote, String local) throws IOException
{
Path path = new Path(remote);
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
fs.copyToLocalFile(path, new Path(local));
System.out.println("download: from" + remote + " to " + local);
fs.close();
}
public void createFile(String file, String content) throws IOException
{
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
byte[] buff = content.getBytes();
FSDataOutputStream os = null;
try
{
os = fs.create(new Path(file));
os.write(buff, 0, buff.length);
System.out.println("Create: " + file);
} finally
{
if (os != null)
os.close();
}
fs.close();
}
}