HDFS的API操作
1 HDFS 核心类简介
Configuration类:处理HDFS配置的核心类。
FileSystem类:处理HDFS文件相关操作的核心类,包括对文件夹或文件的创建,删除,查看状态,复制,从本地挪动到HDFS文件系统中等。
Path类:处理HDFS文件路径。
IOUtils类:处理HDFS文件读写的工具类。
2 HDFS文件处理类FileSystem的核心方法介绍:
1. FileSystem get(URI uri, Configuration conf)根据HDFS的URI和配置,创建FileSystem实例2. public boolean mkdirs(Path f) throws IOException根据路径创建HDFS文件夹3. FSDataOutput Stream create(Path f, boolean overwrite)根据具体的路径创建文件,并且知名是否以重写的方式4. abstract boolean delete(Path f, boolean recursive)根据路径删除文件5. abstract FileStatus[] listStatus(Path f)根据路径,返回该路径下所有文件夹或文件的状态。6. Void moveFromLocalFile(Path src, Path dst)将本地路径下的文件,挪动到HDFS的指定路径下7. FSDataInputStream open(Path f)打开指定路径下的文件内容
3 执行流程
maven依赖
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>RELEASE</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.8.2</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.3.2</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.3.2</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>3.3.2</version></dependency></dependencies>
hdfs 创建文件夹
public static void main(String[] args) throws IOException, Exception, URISyntaxException {Configuration conf = new Configuration();
// conf.set("fs.defaultFS", "hdfs://hadoop102:9000");// 1 获取hdfs客户端对象
// FileSystem fs = FileSystem.get(conf );FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf, "root");// 2 在hdfs上创建路径fs.mkdirs(new Path("/dir01/"));// 3 关闭资源fs.close();System.out.println("over");}
1 HDFS文件上传(测试参数优先级)
// 1 文件上传@Testpublic void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException{// 1 获取fs对象Configuration conf = new Configuration();conf.set("dfs.replication", "2");FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 执行上传APIfs.copyFromLocalFile(new Path("e:/info.txt"), new Path("/file1.txt"));// 3 关闭资源fs.close();}
2 HDFS文件下载
// 2 文件下载@Testpublic void testCopyToLocalFile() throws URISyntaxException, IOException, InterruptedException {// 1 获取对象Configuration conf = new Configuration();// conf.set("dfs.replication", "2");FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 执行下载操作
// fs.copyToLocalFile(new Path("/banhua.txt"), new Path("e:/banhua.txt"));fs.copyToLocalFile(false, new Path("/file1.txt"), new Path("e:/file2.txt"), true);// 3 关闭资源fs.close();}
3 HDFS文件夹删除
// 3 文件删除@Testpublic void testDelete() throws IOException, InterruptedException, URISyntaxException{// 1 获取对象Configuration conf = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 文件删除fs.delete(new Path("/dir01"), true);// 3 关闭资源fs.close();}
4 HDFS文件名更改
// 4 文件更名@Testpublic void testRename() throws IOException, InterruptedException, URISyntaxException{// 1 获取对象Configuration conf = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 执行更名操作fs.rename(new Path("/file1.txt"), new Path("/file111.txt"));// 3 关闭资源fs.close();}
5 HDFS文件详情查看
查看文件名称、权限、长度、块信息
// 5 文件详情查看@Testpublic void testListFiles() throws IOException, InterruptedException, URISyntaxException{// 1 获取对象Configuration conf = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 查看文件详情RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);while(listFiles.hasNext()){LocatedFileStatus fileStatus = listFiles.next();// 查看文件名称、权限、长度、块信息System.out.println(fileStatus.getPath().getName());// 文件名称System.out.println(fileStatus.getPermission());// 文件权限System.out.println(fileStatus.getLen());// 文件长度BlockLocation[] blockLocations = fileStatus.getBlockLocations();for (BlockLocation blockLocation : blockLocations) {String[] hosts = blockLocation.getHosts();for (String host : hosts) {System.out.println(host);}}System.out.println("------ok分割线--------");}// 3 关闭资源fs.close();}
6 HDFS文件和文件夹判断
// 6 判断是文件还是文件夹@Testpublic void testListStatus() throws IOException, InterruptedException, URISyntaxException{// 1 获取对象Configuration conf = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 判断操作FileStatus[] listStatus = fs.listStatus(new Path("/"));for (FileStatus fileStatus : listStatus) {if (fileStatus.isFile()) {// 文件System.out.println("f:"+fileStatus.getPath().getName());}else{// 文件夹System.out.println("d:"+fileStatus.getPath().getName());}}// 3 关闭资源fs.close();}
4 HDFS的I/O流操作
上面我们学的API操作HDFS系统都是框架封装好的。那么如果我们想自己实现上述API的操作该怎么实现呢?
我们可以采用IO流的方式实现数据的上传和下载。
1 HDFS文件上传
1.需求:把本地e盘上的banhua.txt文件上传到HDFS根目录
2.编写代码
@Testpublic void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), configuration, "root");// 2 创建输入流FileInputStream fis = new FileInputStream(new File("e:/hahaha.txt"));// 3 获取输出流FSDataOutputStream fos = fs.create(new Path("/hahaha.txt"));// 4 流对拷IOUtils.copyBytes(fis, fos, configuration);// 5 关闭资源IOUtils.closeStream(fos);IOUtils.closeStream(fis);fs.close();}
2 HDFS文件下载
1.需求:从HDFS上下载banhua.txt文件到本地e盘上
2.编写代码
@Testpublic void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), configuration, "root");// 2 获取输入流FSDataInputStream fis = fs.open(new Path("/jinan/info/lenovo/hello.txt"));// 3 获取输出流FileOutputStream fos = new FileOutputStream(new File("e:/hello.txt"));// 4 流的对拷IOUtils.copyBytes(fis, fos, configuration);// 5 关闭资源IOUtils.closeStream(fos);IOUtils.closeStream(fis);fs.close();}