1、创建一个Java项目,将一下代码粘贴到pom.xml中
UTF-8
2.6.0
junit
junit
4.10
test
org.apache.hadoop
hadoop-client
${hadoop.version}
org.apache.hadoop
hadoop-common
${hadoop.version}
org.apache.hadoop
hadoop-hdfs
${hadoop.version}
2、测试代码:
Configuration configuration = null;
FileSystem fileSystem = null;
String url = "hdfs://CentOS7:8020";
String user = "hadoop";
@Before
public void before() throws URISyntaxException, IOException, InterruptedException {
configuration = new Configuration();
configuration.set("dfs.replication","1");//设置副本数量为1
fileSystem = FileSystem.get(new URI(url),configuration,user);
System.out.println("before doing");
}
@After
public void after(){
System.out.println("after doing");
configuration = null;
try {
fileSystem.close();
} catch (IOException e) {
fileSystem = null;
e.printStackTrace();
}
}
/**
* 创建文件
* @throws IOException
*/
@Test
public void mkdir() throws IOException {
Path path = new Path("/mkdirTest1/123");
boolean re = fileSystem.mkdirs(path);
System.out.println(re);
}
3、运行,测试成功!
原文:https://www.cnblogs.com/bigdatadiary/p/12907011.html