CDH6.0、6.1篇:7、Hadoop端代码测试

配置了CDH对hadoop端进行代码测试

POM文件的maven坐标

<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-auth</artifactId>
      <version>3.0.0</version>
</dependency>

代码展示

FileSystem fs = null;

	@Before
	public void init() throws Exception 
	{
		Configuration conf = new Configuration();
		// conf.set("fs.defaultFS", "hdfs://node1:8020"); //NameNode 端口 fs.defaultFS
		/**
		 * 参数优先级: 1、客户端代码中设置的值 2、classpath下的用户自定义配置文件 3、然后是jar中默认配置
		 */
		// 获取一个hdfs的访问客户端 NameNode 端口 fs.defaultFS
		fs = FileSystem.get(new URI("hdfs://node1:8020"), conf, "root");
	}

	/**
	 * 查看文件及文件夹信息
	 * 
	 * @throws IOException
	 * @throws IllegalArgumentException
	 * @throws FileNotFoundException
	 */
	@Test
	public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException 
	{
		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());
		}
	}

你可能感兴趣的:(CDH)