欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Hadoop的Windows环境准备

Hadoop的Windows环境准备

2024/10/25 4:23:53 来源:https://blog.csdn.net/weixin_66158110/article/details/139410156  浏览:    关键词:Hadoop的Windows环境准备

一、将Hadoop传输到Windows中

1、备份副本

cp -r /opt/softs/hadoop3.1.3/ /opt/softs/hadoop3.1.3_temp

2、删除备份的share目录

cd /opt/softs/hadoop3.1.3_temp
rm -rf share/

3、下载到Windows中

重命名去掉_temp 

4、删除备份文件

rm -rf /opt/softs/hadoop3.1.3_temp

二、配置Windows环境

1、将Windos依赖目录下的bin目录中的全部文件,复制粘贴到hadoop3.1.3的bin目录下,有重复直接覆盖

2、双击winutils.exe,如果有窗体一闪而过,说明成功,如果失败则安装微软运行库

3、将hadoop.dll复制一份到C:\Windows\System32目录中

4、配置环境变量

(1)控制面板查找“高级系统设置”,打开后点击“环境变量”

(2)选择“系统变量”的“新建”输入如下信息:

(3)选中“系统变量”的Path选择“编辑”后选择“新建”,输入如下信息

D:\softs\dashuju\bigdata\hadoop3.1.3   //输入你自己的地址

三、创建Java的Maven项目

idea版本:2023.2.5

1、新建项目

创建好后目录如下:

2、添加项目依赖

<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.1.3</version>
</dependency><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>3.1.3</version>
</dependency><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.1.3</version>
</dependency>

3、编写代码

package cn.edu.just.hdfs;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;/***调用hdfs的Java Api*/
public class hdfsApiClient {//文件系统对象FileSystem fileSystem = null;/*** 初始化hdfs文件系统* @Before:在具体方法前会调用被注解的方法*/@Beforepublic void init() throws URISyntaxException, IOException, InterruptedException {//创建文件系统配置对象Configuration conf = new Configuration();//创建文件系统对象fileSystem = FileSystem.get(new URI("hdfs://bigdata03:8020"), conf, "root");System.out.println("hdfs文件系统初始化成功!");}/*** 创建hdfs目录*/@Testpublic void createPath() throws IOException {if (fileSystem != null) {boolean result = fileSystem.mkdirs(new Path("/hdfs_api"));if (result) {System.out.println("创建目录成功");}else {System.out.println("创建目录失败");}}}/*** 删除目录或者文件*/@Testpublic void deletePath() throws IOException {if (fileSystem != null) {Path deletePath = new Path("/hdfs_api");//判断hdfs上是否存在该目录if (fileSystem.exists(deletePath)){/*** 目录存在,调用delete(Path f, boolean recursive)方法进行删除* 第一个参数是要删除的目录的路径* 第二个参数表示:是否要递归删除*/boolean deleteResult = fileSystem.delete(deletePath, false);System.out.println(deleteResult == true ? "删除目录成功":"删除目录失败");}else {System.out.println("要删除的目录在hdfs上不存在");}}}/*** 在hdfs上创建一个文件,并写入指定的内容*/@Testpublic void createHdfsFile() throws IOException {//获取数据输出流对象FSDataOutputStream fsDataOutputStream =fileSystem.create(new Path("/api_file.txt"));//定义要输出的文件的内容String line = "Hello bigdata";//将指定内容写入文件fsDataOutputStream.write(line.getBytes());//对数据输出流对象进行刷新fsDataOutputStream.flush();//关闭输出流对象fsDataOutputStream.close();}/*** 先运行一下createPath,确保hdfs上目录存在* 修改hdfs上的文件的路径和名称*/@Testpublic void moveHdfsFile() throws IOException {//文件的原路径Path src = new Path("/api_file.txt");//文件的新路径Path dst = new Path("/hdfs_api/api_file_new.txt");boolean moveResult = fileSystem.rename(src, dst);if (moveResult){System.out.println("文件路径修改成功");}else {System.out.println("文件路径修改失败");}}/*** 读取hdfs上的文件内容*/@Testpublic void readHdfsFile() throws IOException {//获取数据输入流对象FSDataInputStream fsDataInputStream =fileSystem.open(new Path("/hdfs_api/api_file_new.txt"));//通过IO工具类读取文件中的数据//System.out代表队是PrintStream对象,该对象是OutputStream类的间接子类IOUtils.copyBytes(fsDataInputStream,System.out,2048,false);//换行System.out.println();}/*** 从本地上传文件到hdfs上*/@Testpublic void uploadFile() throws IOException {//本地文件的路径Path src = new Path("E:\\centos&&hadoop\\words.txt");//文件上传到到hdfs的路径Path dst = new Path("/hdfs_api");//文件上传成功后,本地文件是否删除,默认为falseboolean delSrc = true;//上传文件是否覆盖,默认为trueboolean overwrite = false;fileSystem.copyFromLocalFile(delSrc,overwrite,src,dst);}/***从hdfs上下载文件到本地*/@Testpublic void downloadFile() throws IOException {//在hdfs上的文件Path src = new Path("/hdfs_api/api_file_new.txt");//文件的下载路径Path dst = new Path("E:\\centos&&hadoop\\api_file_new.txt");//文件下载后,是否删除hdfs上的源文件boolean delSrc = false;//        false:下载的文件会存在crc校验文件
//        true:不会存在crc校验文件boolean useRawLocalFileSystem = false;fileSystem.copyToLocalFile(src, dst);}/*** 查看hdfs上的文件信息*/@Testpublic void queryHdfsFileInfo() throws IOException {//查询的起始路径Path path = new Path("/");//是否递归查询boolean recursive = true;//获取迭代器RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator =fileSystem.listFiles(path, recursive);//进行遍历输出while ( locatedFileStatusRemoteIterator.hasNext()) {//获取迭代器中需要迭代的元素LocatedFileStatus next = locatedFileStatusRemoteIterator.next();//获取文件路径Path filePath = next.getPath();System.out.println("文件的路径是:"+filePath);//获取文件的权限FsPermission permission = next.getPermission();System.out.println("文件的权限是:"+permission);//获取文件的所属用户String owner = next.getOwner();System.out.println("文件的所属用户是:"+owner);//获取文件的所属用户的用户组String group = next.getGroup();System.out.println("文件的所属用户的用户组是:"+group);//获取文件的副本数short replication = next.getReplication();System.out.println("文件的副本数是:"+replication);//获取文件的块大小,单位是字节long blockSize = next.getBlockSize();System.out.println("文件的块大小是:"+blockSize/1024/1024+"MB");System.out.println("--------------------------------------------------------");}}/*** 关闭文件系统对象* @After:在具体方法执行完毕后会调用被注解的方法*/@Afterpublic void close() throws IOException {if (fileSystem != null) {fileSystem.close();System.out.println("hdfs文件系统已关闭!");}}
}

运行前开启hadoop集群,在NameNode节点开启hdfs(start-dfs.sh)

可打开NameNode的web服务网址,便于后续查看

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com