package com.itstar.demo03;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
/**
* HDFS客户端API演示
*
* @author CDMloong
* @version 1.0
*/
public class HdfsClientTest01 {
FileSystem fs = null;
@Before
public void init() throws IOException, InterruptedException, URISyntaxException {
// 1、客户端加载配置文件
Configuration conf = new Configuration();
// 2、设置副本数,默认3
conf.set("dfs.replication", "2");
// 3、指定块大小,默认128M
conf.set("dfs.blocksize", "64m");
// 4、构造客戶端对象
fs = FileSystem.get(new URI("hdfs://192.168.244.129:9000"), conf, "root");
}
/**
* HDFS中创建文件夹->命令 hdfs dfs -mkdir /文件名
*
* @throws IOException
* @throws IllegalArgumentException
*/
// @Test
public void hdfsMkdir() throws IllegalArgumentException, IOException {
fs.mkdirs(new Path("/alongapi"));
fs.close();
}
/**
* HDFS中移动/修改文件
*
* @throws IOException
* @throws IllegalArgumentException
*/
// @Test
public void hdfsRename() throws IllegalArgumentException, IOException {
// 1、调用方法修改并移动
fs.rename(new Path("/words.txt"), new Path("/alongapi/words01.txt"));
fs.close();
}
/**
* HDFS中删除文件夹
*
* @throws IOException
* @throws IllegalArgumentException
*/
// @Test
public void hdfsRm() throws IllegalArgumentException, IOException {
// 有-表示已经过时了,但是可以使用
// fs.delete(new Path("/weiyulong/weiyulong.txt"));
// 1、参数1为删除的文件路径,参数2是否递归删除
fs.delete(new Path("/alongapi"), true);
fs.close();
}
/**
* 查询HDFS指定目录的信息
*
* @param listFiles
* @throws IOException
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
// @Test
public void hdfsLs() throws FileNotFoundException, IllegalArgumentException, IOException {
// 1、返回的是一个远程迭代器,第二个参数为是否递归查询文件
RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path("/"), false);
// 2、取迭代器的数据
while (iter.hasNext()) {
LocatedFileStatus status = iter.next();
System.out.println("文件的路径为:" + status.getPath());
System.out.println("块大小:" + status.getBlockSize());
System.out.println("文件长度:" + status.getLen());
System.out.println("副本数量:" + status.getReplication());
// Arrays是数组的意思
System.out.println("块信息:" + Arrays.toString(status.getBlockLocations()));
System.out.println("========================================");
}
fs.close();
}
/**
* 判断是文件还是文件夹
*
* @throws IOException
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
// @Test
public void hdfsFind() throws FileNotFoundException, IllegalArgumentException, IOException {
// 1、展示状态信息
FileStatus[] listStatus = fs.listStatus(new Path("/"));
// 2、遍历所有文件
for (FileStatus ls : listStatus) {
if (ls.isFile()) {
// 文件
System.out.println("文件-------f-------" + ls.getPath().getName());
} else {
System.out.println("文件夹--------d------" + ls.getPath().getName());
}
}
}
// 读数据方式1:直接读取指定字节数据
// @Test
public void hdfsFileReadData01() throws IllegalArgumentException, IOException {
// 1、拿到文件流
FSDataInputStream openFile = fs.open(new Path("/hdfs-site.xml"));
// 2、定义一个字节数组
byte[] buf = new byte[1024];
// 3、读取文件
openFile.read(buf);
// 4、因返回的是字节数,转换为String
System.out.println(new String(buf));
// 5、进行关闭
openFile.close();
fs.close();
}
// 读取数据方式2:缓冲流方式读取
// @Test
public void hdfsFileReadData02() throws IllegalArgumentException, IOException {
// 1、拿到文件流
FSDataInputStream openFile = fs.open(new Path("/hdfs-site.xml"));
// 2、缓冲流,效率高
BufferedReader br = new BufferedReader(new InputStreamReader(openFile));
// 3、按行读取
String line = null;
// 4、读数据
while ((line = br.readLine()) != null) {
System.out.println(line);
}
// 5、关闭文件
br.close();
openFile.close();
fs.close();
}
// 读取数据方式3:读取hdfs中指定的偏移量
// @Test
public void hdfsFileReadData03() throws IllegalArgumentException, IOException {
// 1、拿到文件流
FSDataInputStream openFile = fs.open(new Path("/hdfs-site.xml"));
// 2、偏移量
openFile.seek(200);
// 3、指定读取字节数
byte[] b = new byte[1024];
// 4、读取
openFile.read(b);
// 5、输出
System.out.println(new String(b));
// 6、关闭
openFile.close();
fs.close();
}
// 写数据方式1
// @Test
public void hdfsFileWriteData01() throws IllegalArgumentException, IOException {
// 1、定义输出流
FSDataOutputStream out = fs.create(new Path("/weiyulong1.txt"), false);
// 2、定义输入流
FileInputStream in = new FileInputStream("E:\\testData.txt");
// 3、字节数组
byte[] buf = new byte[1024];
int read = 0;
// 4、读取出来进行写入
while ((read = in.read(buf)) != -1) {
out.write(buf, 0, read);
}
in.close();
out.close();
fs.close();
}
// 写数据方式
@Test
public void hdfsFileWriteData02() throws IllegalArgumentException, IOException {
//1、创建输出流
FSDataOutputStream out = fs.create(new Path("/along2"));
//2、创建输入流
// FileInputStream in = new FileInputStream(new File("E:\\testData.txt"));
// 3、写数据
out.write("alonglaile".getBytes());
// 4、关闭流
IOUtils.closeStream(out);
fs.close();
}
// 文件上传
@Test
public void putFileToHdfs() throws IllegalArgumentException, IOException {
// 1.输入流
FileInputStream fis = new FileInputStream(new File("E:\\testData1.txt"));
// 2.输出流-hdfs
FSDataOutputStream fos = fs.create(new Path("/testData1.txt"));
// 3.流的拷贝
IOUtils.copyBytes(fis, fos, conf);
// 4.关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
//下载文件
@Test
public void getFileFromHdfs() throws IllegalArgumentException, IOException {
// 1.获取输入流
FSDataInputStream fis = fs.open(new Path("/weiyulong1.txt"));
// 2.获取输出流
FileOutputStream fos = new FileOutputStream(new File("E:\\along2.txt"));
// 3.流的拷贝
IOUtils.copyBytes(fis, fos, conf);
// 4.关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
}
HADOOP HDFS Client API
最新推荐文章于 2023-05-09 10:46:13 发布