参数优先级排序:
- 客户端代码中设置的值
- ClassPath 下的用户自定义配置文件
- 服务器的默认配置
一、HDFS文件上传
/*** @Description 上传本地文件到hdfs* @MethodName testCopyFromLocalFile* @Param* @Return void* @Date 2019年10月28日 17:59:22* @Author Wells*/@Testpublic void testCopyFromLocalFile() throws Exception { // 1.创建配置 Configuration conf = new Configuration(); conf.set("dfs.replication", "2"); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml")); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml")); // 2.获取文件系统,其中 uri 举例为:hdfs:xxx-xx-xx 或者 file:///,不填的话默认为本地系统:file:/// FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root"); // 3.上传本地文件到hdfs,其中 path 为全目录,不会拼接上面的 uri fs.copyFromLocalFile(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/data"), new Path("/tmp/wells/20191029/data")); // 4.关闭资源 fs.close();}
二、HDFS文件下载
/** * @Description 从hdfs上下载文件到本地 * @MethodName testCopyToLocalFile * @Param * @Return void * @Date 2019年10月28日 18:03:54 * @Author Wells */@Testpublic void testCopyToLocalFile() throws Exception { // 1.创建配置 Configuration conf = new Configuration(); conf.set("dfs.replication", "2"); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml")); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml")); // 2.获取文件系统 FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root"); // 3.下载hdfs文件到本地 fs.copyToLocalFile(new Path("/tmp/wells/20191029/data"), new Path("/home/wells/Downloads/")); // 4.关闭资源 fs.close();}
三、HDFS文件夹删除
/*** @Description 删除hdfs文件夹/文件* @MethodName testDelete* @Param* @Return void* @Date 2019年10月28日 18:07:52* @Author Wells*/@Testpublic void testDelete() throws Exception { Configuration conf = new Configuration(); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml")); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml")); // 1.获取文件系统 FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root"); // 2.执行删除 fs.delete(new Path("/tmp/wells/20191029/data"), true); // 3.关闭资源 fs.close();}
四、HDFS文件夹名修改
/*** @Description 文件或文件夹重命名* @MethodName testRename* @Param* @Return void* @Date 2019年10月28日 18:10:08* @Author Wells*/@Testpublic void testRename() throws Exception { Configuration conf = new Configuration(); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml")); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml")); // 1.获取文件系统 FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root"); // 2 修改文件名称 fs.rename(new Path("/tmp/wells/20191029/data"), new Path("/tmp/wells/20191029/data2")); // 3 关闭资源 fs.close();}
五、HDFS文件详情查看
/*** @Description 获取文件夹下文件详情* @MethodName testListFiles* @Param* @Return void* @Date 2019年10月28日 18:13:41* @Author Wells*/@Testpublic void testListFiles() throws Exception { Configuration conf = new Configuration(); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml")); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml")); // 1.获取文件系统 FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root"); // 2.获取文件夹下详情 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/tmp/wells"), true); while (listFiles.hasNext()) { LocatedFileStatus status = listFiles.next(); // 3.输出详情 // 文件名称 System.out.println(status.getPath().getName()); // 长度 System.out.println(status.getLen()); // 权限 System.out.println(status.getPermission()); // 分组 System.out.println(status.getGroup()); // 获取存储的块信息 BlockLocation[] blockLocations = status.getBlockLocations(); for (BlockLocation blockLocation : blockLocations) { // 获取块存储的主机节点 String[] hosts = blockLocation.getHosts(); for (String host : hosts) { System.out.println(host); } } System.out.println("---------------------"); } // 3.关闭资源 fs.close();}
六、HDFS文件和文件夹判断
/*** @Description 判断是否是文件* @MethodName testListStatus* @Param* @Return void* @Date 2019年10月28日 18:15:08* @Author Wells*/@Testpublic void testListStatus() throws Exception { // 1.获取文件配置信息 Configuration conf = new Configuration(); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml")); conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml")); // 2.创建文件系统 FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root"); // 3.判断是文件还是文件夹 FileStatus[] listStatus = fs.listStatus(new Path("/tmp/wells")); 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();}