1、首先创建一个maven项目,在pom.xml中添加一些必要的依赖
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-jobclient</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
2、在windows中配置hadoop环境变量
从HDFS中下载内容到windows系统 , 是跨系统操作,所以需要在本地安装hdp的环境。
配置环境变量类似jdk的环境变量配置。
新建HADOOP_HOME=hadoop安装目录;
在path中添加%HADOOP_HOME%\bin。
注意:要保证在虚拟机和windows安装的hadoop,以及maven中依赖的hadoop版本保持一致,否则后续可能出现很多的问题。
3、入门程序
3.1 上传文件(单个)
//获取一个配置对象
Configuration conf = new Configuration();
//获取一个文件系统(hadoop.fs的)的实例对象
/**
* 参数一:URI 指定HDFS文件系统的位置
* 参数二:conf 配置对象,用户的自定义设置,例如:文件上传的副本个数
* 参数三:user 用户名 root
* */
FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
//文件上传
/**
* 参数一:源路径 windows的文件路径
* 参数二:目的路径 hdfs的抽象路径,例如根目录/
* */
fs.copyFromLocalFile(new Path("E:\\linux软件\\linux软件\\hbase-2.2.5-bin.tar.gz"),new Path("/"));
//释放资源
fs.close();
执行程序后,打开页面,一定要保证集群的开启。
3.2 上传文件(多个)
//工具类获取文件系统对象
FileSystem fs = FileSyetemUtils.getFileSystem();
/**
*参数一: 是否删除源文件
*参数二: 是否覆盖hdfs文件系统上已存在的同名文件
*参数三: 源文件路径数组(支持多个文件同时上传)
*参数四: hdfs目标路径
* */
fs.copyFromLocalFile(
false,
true,
new Path[]{new Path("E:\\linux软件\\linux软件\\hadoop-3.2.1.tar.gz"),new Path("E:\\linux软件\\linux软件\\jdk-8u141-linux-x64.tar.gz")},
new Path("/"));
fs.close();
工具类代码:
public class FileSyetemUtils {
public static FileSystem getFileSystem() throws Exception {
return FileSystem.newInstance(new URI("hdfs://linux01:8020"), new Configuration(), "root");
}
}
3.3 下载文件
FileSystem fs = FileSyetemUtils.getFileSystem();
/**
* 参数一: 是否删除源路径文件
* 参数二: 源路径文件(hdfs)
* 参数三: 目的路径(windows)
* 参数四: 是否不生成校验文件,默认是false,生成。
* */
fs.copyToLocalFile(true,new Path("/jdk-8u141-linux-x64.tar.gz"),new Path("E://"),true);
fs.close();
3.4 配置文件
//配置对象
Configuration conf = new Configuration();
//设置块的大小,默认是128M
conf.set("dfs.blocksize","64M");
//设置备份数,默认是3份
conf.set("dfs.replication","4");
//要操作的hdfs目录
URI uri = new URI("hdfs://linux01:8020");
//用户名
String user="root";
//获取hdfs文件系统对象
FileSystem fs = FileSystem.newInstance(uri, conf, user);
fs.copyFromLocalFile(new Path("E:\\linux软件\\linux软件\\apache-hive-3.1.2-bin.tar.gz"),new Path("/"));
//关闭对象
fs.close();
配置文件的优先级:代码>>resources中的配置文件>>hdfs中的配置
3.5 创建文件夹
FileSystem fs = FileSyetemUtils.getFileSystem();
fs.mkdirs(new Path("/wxx/java/se/hg"));
fs.close();
3.6 读数据
FileSystem fs= FileSyetemUtils.getFileSystem();
FSDataInputStream inputStream = fs.open(new Path("/f1.ssh"));
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while ((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
fs.close();
3.7 写数据
FileSystem fs = FileSyetemUtils.getFileSystem();
/* //追加,报错,执行不了
FSDataOutputStream fout = fs.append(new Path("/f1.ssh"));
fout.writeUTF("我想休息");*/
//覆盖
FSDataOutputStream fout = fs.create(new Path("/f1.ssh"));
fout.writeUTF("挺好:\n");
//刷新
fout.flush();
fout.close();
fs.close();
注意:读写的时候一定要给文件权限 hdfs dfs -chmod
-R 777 /f1.ssh
3.8 遍历文件夹下所有的内容
FileSystem fs = FileSyetemUtils.getFileSystem();
Path path = new Path("/");
FileStatus fileStatus = fs.getFileStatus(path);
// 判断路径是否是文件夹
boolean b = fileStatus.isDirectory();
/**
* 参数一 路径 文件夹
* 参数二 递归遍历子文件夹中的文件
*/
if (b){
// 遍历/目录下以及子目录下所有的文件 参数一:路径 参数二:是否递归
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(path, true);
// 遍历每个文件
while (iterator.hasNext()) {
// 每个文件
LocatedFileStatus file = iterator.next();
Path p = file.getPath();// 文件的路径
String name = p.getName();// 文件名
long len = file.getLen();//文件的实际大小
short replication = file.getReplication();// 副本个数
long blockSize = file.getBlockSize(); // 物理切块的大小
System.out.println(name);
// 获取数据文件的元数据信息 文件 物理块 多个副本
// 每个文件的多个物理块
BlockLocation[] blockLocations = file.getBlockLocations();
// 文件的一个物理切块
for (BlockLocation blockLocation : blockLocations) {
blockLocation.getOffset();// 起始位置
blockLocation.getLength();// 数据物理块的大小
//blockLocation 获取多个副本的主机名
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host); // 多个副本所在的主机名
}
}
}
}
fs.close();