1. hadoop开发包下载
1.1 windows系统上下载对应的hadoop开发包,例如:hadoop3.3.5版本
1.1 解压开发包并重命名
2. 打补丁
2.1 由于是windows开发系统,需要安装一些补丁才能正常运行,在hadoop的bin目录下安装
将该三个补丁,复制到hadoop的bin目录下即可
3.配置hadoop的环境变量
3.1 前提JDK已安装及环境变量已配置
3.1 添加两个变量:HADOOP_HOME、PATH
4 . 打开IDEA创建Maven项目
4.1 项目创建
4.2. 在pom.xml文件中引入对应依赖
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.3.5</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.3.5</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>3.3.5</version> </dependency>
4.3. 从hadoop服务器上下载配置文件,导入到项目的src/resources下
(备注:有的情况下此项不需要配置,就能使用hadoop,可以跳过此项,如果最后链接hadoop失败,则可以在配置此项)
4.4. 使用硬编码方式连接hdfs伪分布式(注意:服务器的防火墙一定要关闭)
适用于单机模式与伪分布集群:因为很清楚namenode是哪台服务器
方式一:
try{
//-适用于单机模式与伪分布集群:因为都是固定的服务器地址
//-此处的地址要与core-site.xml中配置的fs.defaultFS内容相同
URI uri = new URI("hdfs://hadoop:9000");
Configuration conf = new Configuration();
String user = "root";
FileSystem fs = FileSystem.get(conf);
System.out.println("连接成功");
//-判断hdfs中某个目录是否存在
System.out.println(fs.exists(new Path("/wordcount")));
fs.close();
}catch (Exception ex){
ex.printStackTrace();
}
方式二:
try{
//-适用于单机模式与伪分布集群:因为都是固定的服务器地址
//-配置项
Configuration conf = new Configuration();
//-此处的地址要与core-site.xml中配置的fs.defaultFS内容相同
conf.set("fs.defaultFS","hdfs://hadoop:9000");
//-设置当前操作系统环境变量
System.setProperty("HADOOP_USER_NAME","ROOT");
FileSystem fs = FileSystem.get(conf);
System.out.println("连接成功");
//-判断hdfs中某个目录是否存在
System.out.println(fs.exists(new Path("/user")));
fs.close();
}catch (Exception ex){
ex.printStackTrace();
}
4.5. 连接hadoop高可用集群
4.5.1 将hadoop集群服务器中的如下配置文件,下载到本地并复制到项目的src/resources目录下
4.5.2 连接高可用集群代码
try{
System.setProperty("HADOOP_USER_NAME","ROOT");
Configuration conf = new Configuration();
DistributedFileSystem dfs = new DistributedFileSystem();
//-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
String nameService = conf.get("dfs.nameservices");
String hdfsRPCUrl = "http://"+nameService+":"+8020;
dfs.initialize(URI.create(hdfsRPCUrl),conf);
//-:获取hdfs中根目录下所有文件路径
FileStatus[] list = dfs.listStatus(new Path("/"));
for(FileStatus file:list){
System.out.println(file.getPath());
}
dfs.close();
}catch(Exception ex){
ex.printStackTrace();
}
4.5.3 自己在服务器上创建目录
try{
System.setProperty("HADOOP_USER_NAME","ROOT");
Configuration conf = new Configuration();
DistributedFileSystem dfs = new DistributedFileSystem();
//-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
String nameService = conf.get("dfs.nameservices");
String hdfsRPCUrl = "http://"+nameService+":"+8020;
dfs.initialize(URI.create(hdfsRPCUrl),conf);
//-:创建目录
Path path = new Path("/ceshi");
if(!dfs.exists(path)){
System.out.println(dfs.mkdirs(path)?"创建成功":"创建失败");
}else{
System.out.println(path.getName()+"已经存在无需创建");
}
dfs.close();
}catch(Exception ex){
ex.printStackTrace();
}
4.5.4 修改文件名
try{
System.setProperty("HADOOP_USER_NAME","ROOT");
Configuration conf = new Configuration();
DistributedFileSystem dfs = new DistributedFileSystem();
//-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
String nameService = conf.get("dfs.nameservices");
String hdfsRPCUrl = "http://"+nameService+":"+8020;
dfs.initialize(URI.create(hdfsRPCUrl),conf);
//-:创建目录
Path src = new Path("/ceshi");
Path dst = new Path("/测试");
if(dfs.exists(src)){
System.out.println(dfs.rename(src,dst)?"修改成功":"修改失败");
}else{
System.out.println(src.getName()+"文件不存在");
}
dfs.close();
}catch(Exception ex){
ex.printStackTrace();
}
4.5.5 删除目录或文件
try{
System.setProperty("HADOOP_USER_NAME","ROOT");
Configuration conf = new Configuration();
DistributedFileSystem dfs = new DistributedFileSystem();
//-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
String nameService = conf.get("dfs.nameservices");
String hdfsRPCUrl = "http://"+nameService+":"+8020;
dfs.initialize(URI.create(hdfsRPCUrl),conf);
//-:创建目录
Path path = new Path("/测试");
if(dfs.exists(path)){
System.out.println(dfs.delete(path,true)?"删除成功":"删除失败");
}else{
System.out.println(path.getName()+"文件不存在");
}
dfs.close();
}catch(Exception ex){
ex.printStackTrace();
}
4.5.6 文件上传
try{
System.setProperty("HADOOP_USER_NAME","ROOT");
Configuration conf = new Configuration();
DistributedFileSystem dfs = new DistributedFileSystem();
//-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
String nameService = conf.get("dfs.nameservices");
String hdfsRPCUrl = "http://"+nameService+":"+8020;
dfs.initialize(URI.create(hdfsRPCUrl),conf);
//-:创建目录
Path src = new Path("hadoop教程.txt");
Path dst = new Path("/study/hadoop教程.txt");
Path path = new Path("/study");
if(dfs.exists(path)){
dfs.copyFromLocalFile(src,dst);
System.out.println(dfs.exists(dst)?"上传成功":"上传失败");
}else{
System.out.println(path.getName()+"目标目录不存在无法上传");
}
dfs.close();
}catch(Exception ex){
ex.printStackTrace();
}