目的:
·创建目录
·递归显示文件
·文件上传
·文件下载
1 Hadoop官网学习文档使用
1.1 官网文档
·地址: https://hadoop.apache.org/docs/r2.9.2/


1.2、查询文档
·文档菜单上的Index按钮
·在索引页面Ctrl+F查询

2、Java API介绍
·Configuration类
封装了客户端、服务器的配置
·Path类
文件路径的封装
·FileSystem
该类的对象是一个文件系统对象,可用于对文件的操作
·FsDataInputStream
HDFS中的输入流,通过FileSystem的open方法获得
·FsDataOutputStream
HDFS中得输出流,通过FileSystem的Create方法获得
3、maven包厂库
https://mvnrepository.com/artifact/org.apache.hadoop

4、创建项目
创建maven项目:


选择simple project

填写项目group Id和Artifact Id
Group Id : cn.asu
Artifact Id : hadoop-demo1
Packaging : pom

创建好的目录结构,只有src和一个pom.xml文件

创建hdfs模块



创建一个目录和测试问题,确认环境是否成功
Package: org.hadoop.hdfs.demo
Class name:Test

创建一个main函数打印确认

5 引入maven包
5.1 maven方式引入
5.1.1 pom文件添加
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs- client -->
<dependency> <groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs-client</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
5.1.2 源码包查看pom
源码包下载
https://hadoop.apache.org/releases.html

源码查看



5.2 jar包引入
5.2.1 jar包位置
hadoop的安装包
\hadoop-2.9.2\share\hadoop目录下包含所有需要的jar包
需要使用common和hdfs里面的jar包
common包
hadoop-common-2.9.2.jar
hadoop-nfs-2.9.2.jar
common/lib目录下所有包hdfs包
hadoop-hdfs-2.9.2.jar
hadoop-hdfs-client-2.9.2.jar
hadoop-hdfs-native-client-2.9.2.jar
hadoop-hdfs-nfs-2.9.2.jar
hdfs/hadoop-hdfs-rbf-2.9.2.jar

5.2.2 拷贝jar包到工作空间
在工作空间创建hadoop-libs目录

把common目录和hdfs目录拷贝到hadoop-libs目录

5.2.3 把需要的jar包引入程序





6 JAVA程序操作
6.1 下载HDFS的配置文件加入项目
下载core-site.xml和hdfs-site
配置文件加入resources目录

6.1 java创建文件夹
创建CreateFolder.java类

代码实现
public static void main(String[] args) throws IOException {
//获取HDFS相关配置信息
Configuration configuration = new Configuration();
//获取FileSystem对象
FileSystem fileSystem = FileSystem.get(configuration);
//创建Path对象
Path path = new Path("/test");
//创建目录
fileSystem.mkdirs(path);
}运行结果

解决权限问题
当前我操作系统的user=1,但是hadoop目录权限是rwxr-xr-x,需要放开 hadoop 目录的权限但是
hadoop目录权限是rwxr-xr-x,需要放开 hadoop 目录的权限
hdfs dfs -chmod -R o+w /

6.2 java递归显示文件路径
创建listFile.java
public class ListFile { public static void main(String[] args) throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
Path path = new Path("/"); list(fileSystem, path );
}
public static void list(FileSystem fileSystem,Path path)throws FileNotFoundException, IOException {
FileStatus[] fileStatus = fileSystem.listStatus(path);
for (int i = 0; i < fileStatus.length; i++) {
FileStatus fStatus = fileStatus[i];
if (fStatus.isDirectory()) {
System.out.println("当前路径是:"+fStatus.getPath());
list(fileSystem, fStatus.getPath());
}else {
System.out.println("当前文件是:"+fStatus.getPath());
}
}
}
}6.3 java上传文件
创建uploadFile.java
public class UploadFile { public static void main(String[] args) throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration); System.setProperty("HADOOP_USER_NAME", "root");
Path srcPath = new Path("d://aaa.txt");
Path dest = new Path("/test1"); fileSystem.copyFromLocalFile(srcPath, dest);
}
}6.4 java下载文件
6.4.1 程序代码
创建
public class DownloadFile { public static void main(String[] args) throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
Path srcPath = new Path("/test1/aaa.txt");
Path destPath = new Path("C:\\Users\\1\\Desktop\\software"); fileSystem.copyToLocalFile(srcPath, destPath);
}
}6.4.2 unset错误解决
这个时候程序会报错,表示没有设置Hadoop目录

进入网站
https://wiki.apache.org/hadoop/WindowsProblems

点击进入问题说明页面
https://cwiki.apache.org/confluence/display/HADOOP2/WindowsProblems

到github上下载
https:///steveloughran/winutils

解压以后获得解压包


















