jiapeng 6 年之前
父節點
當前提交
3148f1f484
共有 2 個文件被更改,包括 74 次插入0 次删除
  1. 15 0
      pom.xml
  2. 59 0
      src/main/java/com/ekexiu/project/hdfs/HdfsService.java

+ 15 - 0
pom.xml

@ -75,6 +75,21 @@
75 75
        	<artifactId>decamino_nonames_rt</artifactId>
76 76
        	<version>0.0.1</version>
77 77
        </dependency>
78
<!-- 		<dependency>
79
			<groupId>org.apache.hadoop</groupId>
80
			<artifactId>hadoop-hdfs</artifactId>
81
			<version>3.0.3</version>
82
		</dependency> -->
83
		<dependency>
84
			<groupId>org.apache.hadoop</groupId>
85
			<artifactId>hadoop-client</artifactId>
86
			<version>3.0.3</version>
87
		</dependency>
88
<!-- 		<dependency>
89
			<groupId>org.apache.hadoop</groupId>
90
			<artifactId>hadoop-common</artifactId>
91
			<version>3.0.3</version>
92
		</dependency> -->
78 93
    </dependencies>
79 94
    <build>
80 95
        <plugins>

+ 59 - 0
src/main/java/com/ekexiu/project/hdfs/HdfsService.java

@ -0,0 +1,59 @@
1
package com.ekexiu.project.hdfs;
2
import java.io.File;
3
import java.io.IOException;
4

5
import org.apache.hadoop.conf.Configuration;
6
import org.apache.hadoop.fs.FileSystem;
7
import org.apache.hadoop.fs.Path;
8
import org.apache.hadoop.hdfs.DistributedFileSystem;
9
public class HdfsService {
10
	private Configuration configuration = new Configuration();
11
	private DistributedFileSystem dfs;
12
	private Path root = new Path("/");
13
	private String remoteUrl;
14
	
15

16

17
	public void setRootPath(String rootPath){
18
		this.root = new Path(rootPath);
19
	}
20
	
21
	public String getRemoteUrl() {
22
		return remoteUrl;
23
	}
24

25

26
	public void setRemoteUrl(String remoteUrl) {
27
		this.remoteUrl = remoteUrl;
28
		configuration.set("fs.default.name", this.remoteUrl);
29
		try {
30
			dfs = (DistributedFileSystem) FileSystem.get(configuration);
31
		} catch (IOException e) {
32
			throw new RuntimeException(e);
33
		}
34
	}
35
	
36
	public void upload(String path,File src) throws IOException{
37
		if(src.exists() && src.isDirectory()){
38
			Path dst  =  new Path(root,path);
39
			if(this.dfs.exists(dst)){
40
				if(!this.dfs.delete(dst,true)){
41
					throw new IOException("delete "+configuration.get("fs.default.name")+path.toString()+" error");
42
				}
43
			}
44
			if(!this.dfs.mkdirs(dst)){
45
				throw new IOException("mkdir "+configuration.get("fs.default.name")+path.toString()+" error");
46
			}
47
			this.dfs.copyFromLocalFile(new Path(src.getAbsolutePath()), dst);
48
		}
49
		
50
	}
51
	
52
	
53
	public static void main(String args[]) throws Exception{
54
		HdfsService service = new HdfsService();
55
		service.setRemoteUrl("hdfs://39.97.161.48:9000");
56
		service.upload("11",new File("D:\\pdftest"));
57
	}
58

59
}