【笔记】HDFS简单API代码(Java)的使用
一、准备工作
1)Ecplise中hdfs的jar包导入
可以从hadoop官网https://hadoop.apache.org/releases.html中下载hadoop的binary版本,如2.8.5版本。下载完成后,解压到本地windows系统自己指定的文件夹中。
在Ecplise中建立Java工程后,需导入之前解压的文件夹目录下share目录中的一些jar包
share/hadoop/common/hadoop-common-2.8.5.jar
share/hadoop/common/lib/*.jar
share/hadoop/hdfs/hadoop-hdfs-2.8.5.jar
share/hadoop/hdfs/lib/*.jar
2)hadoop的Windows环境变量的设置
在我的电脑->属性->高级系统设置中打开环境变量
新建系统变量HADOOP_HOME(值的设置参考刚刚解压的hadoop包所在目录):
然后编辑Path变量,在Path变量中加入%HADOOP_HOME%/bin :
点击大概三次确定后即可。
从服务器hdfs系统中,下载文件到本地windows系统时,必须设置好Hadoop的环境变量。另外,还需要在 %HADOOP_HOME%/bin/目录中加入相应版本的winutils.exe文件
二、构造客户端基本代码
//1. 客户端加载配置文件
Configuration conf= new Configuration();
//2.指定配置,设置副本数、指定块大小等等
conf.set("dfs.replication", "1");
conf.set("dfs.blocksize", "64m");
//3.构造客户端
FileSystem fs=FileSystem.get(new URI("hdfs://192.168.193.128:9000/"), conf, "root");
三、基本操作代码
1)上传下载文件基本操作
从windows本地,上传到hdfs系统中
//上传文件
fs.copyFromLocalFile(new Path("D:/zTest/test.txt"), new Path("/test2.txt"));
//关闭资源
fs.close();
从hdfs系统中,下载到windows本地
//hdfs数据下载到windows本地
fs.copyToLocalFile(new Path("/hdfs-site.xml"), new Path("D:/zTest/"));
//关闭资源
fs.close();
2)利用IOUtils包实现文件的上传和下载操作
文件上传:
//1.获取文件输入流
FileInputStream fis=new FileInputStream(new File("D:/zTest/test2.txt"));
//2.获取文件输出流
FSDataOutputStream fos=fs.create(new Path("/testT.txt"));
//3.流的拷贝
IOUtils.copyBytes(fis, fos,conf);
//4.关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fs.close();
文件下载:
//1.获取文件输入流
FSDataInputStream fis=fs.open(new Path("/testT.txt"));
//2.获取输入流
FileOutputStream fos=new FileOutputStream(new File("D:/zTest/testTw.txt"));
//3.流的对拷
IOUtils.copyBytes(fis, fos, conf);
//4 .关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fs.close();
3)hdfs系统中创建目录
fs.mkdirs(new Path("/hello"));
fs.close();
4)hdfs系统中移动文件或修改文件名操作
fs.rename(new Path("/a.txt"), new Path("/hello/as.txt"));
fs.close();
5)hdfs系统中删除文件或者文件夹
fs.delete(new Path("/b.txt"), true);
fs.close();
6)查询hdfs系统下文件目录的相关信息
RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path("/"), true);
//2。取用迭代器
while(iter.hasNext()) {
LocatedFileStatus status=iter.next();
System.out.println("文件的路径为:"+status.getPath());
System.out.println("文件长度为:"+status.getLen());
System.out.println("块大小:"+status.getBlockSize());
System.out.println("副本数:"+status.getReplication());
System.out.println("块信息:"+Arrays.toString(status.getBlockLocations()));
System.out.println("==========");
}
//3.关闭资源
fs.close();
7)判断是文件还是文件夹示例代码
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus ls:listStatus) {
if(ls.isFile()) {
//文件
System.out.println("文件-----------f----------"+ls.getPath().getName());
}else {
//文件夹
System.out.println("文件------------d----------"+ls.getPath().getName());
}
}
fs.close();
8)从hdfs系统中文件读取文件内容
示例代码一:
//1.拿到流
FSDataInputStream in = fs.open(new Path("/test2.txt"));
byte[] buf=new byte[1024];
int read=in.read(buf);
System.out.println(new String(buf) +" ==== "+read);
//关闭资源
in.close();
fs.close();
示例代码二(使用缓冲流):
//拿到流
FSDataInputStream in = fs.open(new Path("/test2.txt"));
//缓冲流
BufferedReader br=new BufferedReader(new InputStreamReader(in,"UTF-8"));
//按行读取
String line=null;
while((line=br.readLine())!=null) {
System.out.println(line);
}
//关闭资源
br.close();
in.close();
fs.close();
示例代码三(指定偏移量读取):
//拿到流
FSDataInputStream in = fs.open(new Path("/test2.txt"));
//到指定便宜量
in.seek(6);
byte[]b=new byte[3];
in.read(b);
System.out.println(new String(b));
//关闭资源
in.close();
fs.close();
9)向hdfs系统中文件写入内容
示例代码一:
//1.输出流
FSDataOutputStream out=fs.create(new Path("/window.txt"), false);
//2.输入流
FileInputStream in = new FileInputStream("D:/zTest/test2.txt");
byte[]buf=new byte[3];
int read=0;
//先读后写
while((read=in.read(buf))!=-1) {
out.write(buf,0,read);
}
//3.关闭资源
in.close();
out.close();
fs.close();
示例代码二:
//1.创建输出流
FSDataOutputStream out = fs.create(new Path("/hellohello"));
//FileInputStream in =new FileInputStream(new File("D:/zTest/test2.txt"));
//2.写数据
out.write("heheheda".getBytes());
//out.close();
//fs.close();
//3.关闭资源
IOUtils.closeStream(out);
fs.close();