1.创建一个maven项目:

配置pom.xml文件
如下:
<dependencies>
<!-- 配置缺少的toold.jar -->
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>C:\Program Files\Java\jdk1.8.0_131\lib\tools.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.4</version>
</dependency>
</dependencies>
配置信信息在网址:
https://mvnrepository.com 分别查找:Hadoop HDFS 、Junit、Hadoop common
注:寻找自己对象的版本
public class Demo01 {
@Test
public void test01() throws URISyntaxException, IOException {
//首先需要获取文件系统
Configuration conf=new Configuration();
URI uri=new URI("hdfs://hadoop:8020");
FileSystem fs=FileSystem.get(uri,conf);
//fs.copyFromLocalFile(new Path("C:\\Users\\12042\\Desktop\\a1.txt"), new Path("/"));
RemoteIterator<LocatedFileStatus> it=fs.listFiles(new Path("/"), true);
while(it.hasNext()) {
LocatedFileStatus lfs=it.next();
System.out.println(lfs.getPath());
System.out.println("长度:"+lfs.getLen());
}
fs.close();
}
//文件的上传
@Test
public void uploadFiles() throws Exception {
Configuration conf=new Configuration();
URI uri=new URI("hdfs://hadoop:8020");
FileSystem fs=FileSystem.get(uri,conf,"root");
fs.copyFromLocalFile(true,new Path("C:\\Users\\12042\\Desktop\\1111.txt"), new Path("/"));
fs.close();
}
//创建文件夹
@Test
public void mkdirs11() throws Exception {
Configuration conf=new Configuration();
URI uri=new URI("hdfs://hadoop:8020");
FileSystem fs=FileSystem.get(uri,conf,"root");
fs.mkdirs(new Path("/ddd"));
RemoteIterator<LocatedFileStatus> it=fs.listFiles(new Path("/"), true);
while(it.hasNext()) {
LocatedFileStatus lfs=it.next();
System.out.println(lfs.getPath());
System.out.println("长度:"+lfs.getLen());
}
fs.close();
}
}
1261

被折叠的 条评论
为什么被折叠?



