HDFSAPI创建目录

来源:3-11 Java API操作HDFS文件系统

慕码人1088981

2018-07-10

package hadooppTEst;


import static org.junit.Assert.*;

import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MyTest {
	private Configuration configuration=null;
	private FileSystem fileSystem=null;
	private static final String HDFS_PATH="hdfs://hadoop000:8020";
	/**
	 * 创建目录
	 * @throws Exception
	 */
	@Test
	public void createDir() throws Exception {
		fileSystem.mkdirs(new Path("/testApp"));
	}
		
	@Before
	public void setUp()throws Exception{
		System.out.println("begin");
		configuration =new Configuration();
		fileSystem=FileSystem.get(new URI(HDFS_PATH),configuration,"hadoop");
	}
	@After
	public void tearDown()throws Exception{
		configuration=null;
		fileSystem=null;
		System.out.println("HDFSApp.tearDown");
	}
}

maven的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>hadoop</groupId>
	<artifactId>2018-7-10_hdfs_01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!--没有添加cdh仓库 -->
	<repositories>
		<repository>
			<id>cloudera</id>
			<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
		</repository>
	</repositories>
	<!--添加hadoop的依赖 -->
	<properties>
		<hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
	</properties>
	<dependencies>
	
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>${hadoop.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<!-- 设置jdk的编译版本 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

begin

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Junit测试跑起来就不停了,控制台输出上面的语句这是怎么回事??

写回答

3回答

Michael_PK

2018-07-11

你debug到setup 的第一行,我咋感觉没走到呢

0
4
慕码人1088981
非常感谢!
2018-07-11
共4条回复

Michael_PK

2018-07-10

运行完去HDFS上看一下OK没

0
0

Michael_PK

2018-07-10

这只是普通日志输出而已,你运行的时候是选择一个case运行的吧

0
3
慕码人1088981
回复
Michael_PK
开启了,在linux上输出jps有nameNode,DataNode,SecondaryNameNode.在windows上从网页输入http://hadoop000:50070也可以访问到
2018-07-11
共3条回复

10小时入门大数据

【毕设】大数据零基础入门必备,轻松掌握Hadoop开发核心技能

1456 学习 · 656 问题

查看课程