1. Java操作
2. HDFS文件上传
//获取配置对象Configuration configuration = new Configuration();//设置配置 将默认分块设置为2configuration.set("dfs.replication","2");//新建HDFS对象FileSystem fileSystem = FileSystem.get(URI.create("hdfs://hadoop102:8020"), configuration, "atguigu");//操作集群fileSystem.copyFromLocalFile(new Path("D:\\图\\QQ图片20190517141148.jpg"),new Path("/"));//关闭资源fileSystem.close();
get(URI.crate(“hdfs://hadoop地址”),new Configuation(), 集群用户名); 获取HDFS操作对象
- copyFromLocalFile(new Path(“本地路径”),new Path(集群路径)); 上传文件到HDFS
3. HDFS下载到本地
fileSystem.copyToLocalFile(new Path("/QQ图片20190517141148.jpg"),new Path("d:/images"));
4. HDFS追加
FSDataOutputStream append = fileSystem.append(new Path("1.txt"));//写入流append.write("testApi".getBytes(StandardCharsets.UTF_8));//关闭流IOUtils.closeStream(append);
5. ls
//ls操作 返回数组FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));for (FileStatus fileStatus : fileStatuses) {//获取文件对象路径System.out.println(fileStatus.getPath());//获取文件所属者System.out.println(fileStatus.getOwner());
6. listFiles
//获取当前所有文件的迭代器 无论是文件夹还是文件 true返回一个迭代器RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"),true);while (locatedFileStatusRemoteIterator.hasNext()) {LocatedFileStatus fileStatus = locatedFileStatusRemoteIterator.next();System.out.println(fileStatus.getPath());//获取当前文件的块 返回数组BlockLocation[] blockLocations = fileStatus.getBlockLocations();for (int i = 0; i < blockLocations.length; i++) {System.out.println("第" + i + "块");//获取当前文件 每个块存储的集群情况String[] hosts = blockLocations[i].getHosts();for (String host : hosts) {System.out.print(host + " ");}System.out.println();}}
7. 移动或重命名
//重命名或者移动文件fileSystem.rename(new Path("/QQ图片20190517141148.jpg"),new Path("/test/233.jpg"));
