|
1 package com.dajiangtai.Hadoop.HDFS;
2
3 import java.io.IOException;
4 import java.net.URISyntaxException;
5 import org.apache.hadoop.conf.Configuration;
6 import org.apache.hadoop.fs.BlockLocation;
7 import org.apache.hadoop.fs.FileStatus;
8 import org.apache.hadoop.fs.FileSystem;
9 import org.apache.hadoop.fs.FileUtil;
10 import org.apache.hadoop.fs.Path;
11 import org.apache.hadoop.hdfs.DistributedFileSystem;
12 import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
13
14 import java.net.URI;
15
16 public>
17 /**
18 * @param args //@param args是生成文档的时候用的东西,现在不用管。以后慢慢就知道了
19 * @throws IOException
20 * @throws URISyntaxException
21 */
22
23
24 public static void main(String[] args) throws IOException, URISyntaxException{
25 // TODO Auto-generated method stub
26 //这是在你用eclipse这样的Java集成开发环境是开发环境自动帮你写的,
27 //意思是告诉你这些代码是自动生成的,不是你自己写的,就是一个提示的作用,没大作用
28
29
30 // mkdir();
31 // copyToHDFS();
32 // getFile();
33 // ListAllFile();
34 // getFileLocal();
35 // rmdir();
36 getHDFSNodes();
37 }
38
39
40 //获取HDFS文件系统
41 public static FileSystem getFileSystem() throws IOException,URISyntaxException{
42 //getFileSystem()是获取文件系统
43 Configuration conf = new Configuration();//读取配置文件,比如core-site.xml、hdfs-site.xml等等。
44
45
46 //若是在集群里,则如下一行代码即可。
47 //FileSystem fs =FileSystem.get(conf);
48
49
50 //若是在本地里来想去获取,则如下两行代码即可。
51 URI uri = new URI("hdfs://djt002:9000");
52 FileSystem fs = FileSystem.get(uri,conf);
53
54
55
56 return fs;
57 }
58
59
60
61 //创建文件目录
62 public static void mkdir() throws IOException,URISyntaxException{
63 //第一步,获取文件系统
64 FileSystem fs =getFileSystem();//因为上述有个方法
65 //第二步,创建文件目录
66 fs.mkdirs(new Path("/dajiangtai/data"));
67 //第三步,释放资源
68 fs.close();
69 }
70
71
72
73 //文件上传至HDFS
74 public static void copyToHDFS() throws IOException,URISyntaxException{
75 //第一步
76 FileSystem fs=getFileSystem();//因为上述有个方法
77 //第二步
78 Path srcpath=new Path("D://Data/weibo.txt");//原路径
79 Path dstpath=new Path("/dajiangtai/data");//终路径
80 //第三步
81 fs.copyFromLocalFile(srcpath, dstpath);
82 //第四步
83 fs.close();
84 }
85
86
87 //获取目录下的所有文件
88 public static void getFile() throws IOException, URISyntaxException{
89 //第一步
90 FileSystem fs=getFileSystem();//因为上述有个方法
91 //第二步
92 Path srcpath=new Path("/dajiangtai/data/weibo.txt");//原路径
93 Path dstpath=new Path("D://Data/test");//终路径
94 //第三步
95 fs.copyToLocalFile(srcpath, dstpath);
96 //第四步
97 fs.close();
98
99 }
100
101
102 //列出指定路径下的所有文件
103 public static void ListAllFile() throws IOException, URISyntaxException{
104 //第一步
105 FileSystem fs=getFileSystem();//因为上述有个方法
106 //第二步
107 FileStatus[] status =fs.listStatus(new Path("/dajiangtai"));
108 // 任何文件系统的一个重要特性都是提供其目录结构浏览和检索它所存文件和目录相关信息的功能。
109 //FileStatus对象,即status,它封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及权限等信息。
110 //FileStatus对象,即status,由FileSystem的listStatus()方法获得,调用该方法的时候要把文件的Path传进去。
111 //第三步
112 Path[] listedPaths = FileUtil.stat2Paths(status);
113 //第四步
114 for(Path p:listedPaths)
115 {
116 System.out.println(p);
117
118 }
119 //第五步
120 fs.close();
121 }
122
123
124 //查找某个文件在HDFS集群的位置
125 public static void getFileLocal() throws IOException, URISyntaxException{
126 //第一步
127 FileSystem fs=getFileSystem();//因为上述有个方法
128 //第二步
129 Path path=new Path("/dajiangtai/data/weibo.txt");
130 //第三步
131 FileStatus fileStatus=fs.getFileLinkStatus(path);
132
133 // 任何文件系统的一个重要特性都是提供其目录结构浏览和检索它所存文件和目录相关信息的功能。
134 //FileStatus对象,即fileStatus,它封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及权限等信息。
135 //FileStatus对象,即fileStatus,由FileSystem的getFileStatus()方法获得,调用该方法的时候要把文件的Path传进去。
136 //第四步
137 BlockLocation[] blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
138 // 获取到整个文件的所有block的位置信息,比如示例BlockLocation[] blkLocations = fs.getFileBlockLocations(file, 0,length);
139 //第五步
140 for(int i=0;i< blkLocations.length;i++)
141 {
142 String[] hosts = blkLocations.getHosts();
143 System.out.println("block_"+i+"_location:"+hosts[0]);//这里为什么是hosts[0],因为单节点。
144 }
145 //第六步
146 fs.close();
147 }
148
149
150 //删除文件或文件的目录
151 public static void rmdir() throws IOException, URISyntaxException{
152 //第一步
153 FileSystem fs=getFileSystem();//因为上述有个方法
154 //第二步
155 fs.delete(new Path("/dajiangtai/data"),true);
156 //第三步
157 fs.close();
158 }
159
160
161 //获取HDFS集群节点信息
162 public static void getHDFSNodes() throws IOException, URISyntaxException{
163 //第一步
164 FileSystem fs=getFileSystem();//因为上述有个方法
165 //第二步
166 DistributedFileSystem hdfs = (DistributedFileSystem)fs;
167 //第三步
168 DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
169 //第四步
170 for(int i=0;i< dataNodeStats.length;i++)
171 {
172 System.out.println("DataNode_"+i+"_Name:"+dataNodeStats.getHostName());
173 }
174 //第五步
175 fs.close();
176 }
177
178 } |
|
|