|
@ -26,6 +26,7 @@ public class HdfsService {
|
26
|
26
|
|
27
|
27
|
|
28
|
28
|
public void setRemoteUrl(String remoteUrl) {
|
|
29
|
System.setProperty("HADOOP_USER_NAME", "hadoop");
|
29
|
30
|
this.remoteUrl = remoteUrl;
|
30
|
31
|
configuration.set("fs.default.name", this.remoteUrl);
|
31
|
32
|
try {
|
|
@ -36,17 +37,24 @@ public class HdfsService {
|
36
|
37
|
}
|
37
|
38
|
|
38
|
39
|
public void upload(String path,File src) throws IOException{
|
|
40
|
Path dstPath = new Path(root,path);
|
39
|
41
|
if(src.exists() && src.isDirectory()){
|
40
|
|
Path dst = new Path(root,path);
|
41
|
|
if(this.dfs.exists(dst)){
|
42
|
|
if(!this.dfs.delete(dst,true)){
|
43
|
|
throw new IOException("delete "+configuration.get("fs.default.name")+path.toString()+" error");
|
|
42
|
if(!this.dfs.exists(dstPath)){
|
|
43
|
if(!this.dfs.mkdirs(dstPath)){
|
|
44
|
throw new IOException("mkdir "+configuration.get("fs.default.name")+"/"+path+" error");
|
44
|
45
|
}
|
45
|
46
|
}
|
46
|
|
if(!this.dfs.mkdirs(dst)){
|
47
|
|
throw new IOException("mkdir "+configuration.get("fs.default.name")+path.toString()+" error");
|
|
47
|
|
|
48
|
|
|
49
|
for(File file:src.listFiles()){
|
|
50
|
Path dst = new Path(dstPath,file.getName());
|
|
51
|
if(this.dfs.exists(dst)){
|
|
52
|
if(!this.dfs.delete(dst,true)){
|
|
53
|
throw new IOException("delete "+configuration.get("fs.default.name")+"/"+path+"/"+file.getName()+" error");
|
|
54
|
}
|
|
55
|
}
|
|
56
|
this.dfs.copyFromLocalFile(new Path(src.getAbsolutePath()), dst);
|
48
|
57
|
}
|
49
|
|
this.dfs.copyFromLocalFile(new Path(src.getAbsolutePath()), dst);
|
50
|
58
|
}
|
51
|
59
|
|
52
|
60
|
}
|
|
@ -55,7 +63,9 @@ public class HdfsService {
|
55
|
63
|
public static void main(String args[]) throws Exception{
|
56
|
64
|
HdfsService service = new HdfsService();
|
57
|
65
|
service.setRemoteUrl("hdfs://39.97.161.48:9000");
|
58
|
|
service.upload("11",new File("/ekexiu/storage_asd/dst/a"));
|
|
66
|
service.upload("25",new File("/ekexiu/storage_asd/task/result/test/data_desen"));
|
|
67
|
|
|
68
|
System.out.println("11111111111111111111111111111111");
|
59
|
69
|
}
|
60
|
70
|
|
61
|
71
|
}
|