fs.defaultFShdfs://192.168.3.61:9820hadoop.tmp.dir/opt/hadoopdata2.pom.xml
4.0.0com.qmkjhdfsclienttest0.1hdfsclienttesthttp://www.example.comUTF-81.71.7junitjunit4.11testorg.apache.hadoophadoop-hdfs-client3.2.1providedorg.apache.hadoophadoop-common3.2.1org.apache.hadoophadoop-hdfs3.2.1maven-clean-plugin3.1.0maven-resources-plugin3.0.2maven-compiler-plugin3.8.0maven-surefire-plugin2.22.1maven-jar-plugin3.0.2maven-install-plugin2.5.2maven-deploy-plugin2.8.2maven-site-plugin3.7.1maven-project-info-reports-plugin3.0.03.测试代码
package com.qmkj; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.junit.Before; import org.junit.Test; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; /** * Unit test for simple App. */ public class AppTest { FileSystem fs = null; @Before public void init() throws Exception { Configuration conf = new Configuration(); //设立的设置url请注意,设置core-site.xml中配置fs.defaultFS的地址 fs = FileSystem.get(new URI("hdfs://192.168.3.61:9820"), conf, "root"); } @Test public void testAdd() throws Exception { fs.copyFromLocalFile(new Path("D:\\KK_Movies\\kk 2020-02-20 20-45-55.mp4"), new Path("/zhanglei")); fs.close(); } /** * 从hdfs中复制文件到本地文件系统 * * @throws IOException * @throws IllegalArgumentException */ @Test public void testDownloadFileToLocal() throws IllegalArgumentException, IOException { // fs.copyToLocalFile(new Path("/mysql-connector-java-5.1.28.jar"), new // Path("d:/")); fs.copyToLocalFile(false, new Path("test.txt"), new Path("e:/"), true); fs.close(); } /** * 目录操作 * * @throws IllegalArgumentException * @throws IOException */ @Test public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException { // 创建目录 fs.mkdirs(new Path("/zhanglei/b1/c1")); // 删除文件夹 ,如果是非空文件夹,参数2必须给值true ,删除所有子文件夹 fs.delete(new Path("/b1"), true); // 重命名文件或文件夹 fs.rename(new Path("/zhanglei"), new Path("/qmkj")); } /** * 查看目录信息,只显示文件 * * @throws IOException * @throws IllegalArgumentException * @throws FileNotFoundException */ @Test public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException { RemoteIteratorlistFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) { LocatedFileStatus fileStatus = listFiles.next(); System.out.println(fileStatus.getPath().getName()); System.out.println(fileStatus.getBlockSize()); System.out.println(fileStatus.getPermission()); System.out.println(fileStatus.getLen()); BlockLocation[] blockLocations = fileStatus.getBlockLocations(); for (BlockLocation bl : blockLocations) { System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset()); String[] hosts = bl.getHosts(); for (String host : hosts) { System.out.println(host); } } System.out.println("--------------打印的分割线--------------"); } } /** * 查看文件及文件夹信息 * * @throws IOException * @throws IllegalArgumentException * @throws FileNotFoundException */ @Test public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException { //可以右击方法名,Run 测试一下。 FileStatus[] listStatus = fs.listStatus(new Path("/")); String flag = ""; for (FileStatus fstatus : listStatus) { if (fstatus.isFile()) { flag = "f-- "; } else { flag = "d-- "; } System.out.println(flag + fstatus.getPath().getName()); System.out.println(fstatus.getPermission()); } } }
testDownloadFileToLocal 这里测试请注意,本地也要装hdfs才可以