赞
踩
分布式文件管理系统(hdfs)是一种来管理多台机器上文件的系统,是一种允许文件通过网络在多台主机上分享的文件系统,可让多机器上的多用户分享文件和存储空间。
(1)通透性。让实际上是通过网络来访问文件的动作,由程序与用户看来,就像是访问本地的磁盘一般。
(2)容错。即使系统中有某些节点宕机,整体来说系统仍然可以持续运作而不会有数据损失【通过副本机制实现】。
(3)分布式文件管理系统很多,hdfs只是其中一种,不合适小文件。
hdfs架构
datanode
首先在linux中找到连接的配置文件,vim /usr/local/soft/hadoop-2.7.6/etc/hadoop/core-site.xml
- dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-client</artifactId>
- <version>2.7.6</version>
- </dependency>
- //连接hadoop
- Configuration configuration =new Configuration();
- configuration.set("fs.defaultFs","hdfs://master:9000"); //连接指定的hdfs,在配置文件中。
- FileSystem fs = FileSystem.get(configuration); //要抛个异常
- //连接hadoop
- Configuration configuration =new Configuration();
- configuration.set("fs.defaultFS","hdfs://master:9000"); //连接指定的hdfs,在配置文件中。
- FileSystem fs = FileSystem.get(configuration); //要抛个异常
-
- fs.copyFromLocalFile(false,true,new Path("D:\\上课资料\\python\\day53\\2022年4月1日\\代码\\HadoopCode15\\data\\sanguo.txt"),new Path("/"));
- //第一个判断是是否删除源文件,第二个判断是是否覆盖相同文件名的文件,第三个参数是本地文件地址,第四个参数是hdfs中的地址
- fs.close();
(1)Linux的hadoop中配置文件/usr/local/soft/hadoop-2.7.6/etc/hadoop/hdfs-sitexml中设置副本数
(2)可在代码中加入以下内容可设置副本数
configuration.set("dfs.replication","3"); //设置副本数为三
(3)还可以在idea项目中文件resources中加入hadoop中的配置文件
设置副本数配置顺序:
1.hdfs-site.xml(Hadoop集群自定义配置) < 2.Resources中的 hdfs-site.xml < 3.代码中Conf配置
fs.copyToLocalFile(false,newPath("/sanguo/sanguo2.txt"),newPath("D:\\CodeSpace\\HadoopCode15\\data"));
fs.rename(new Path("/sanguo/sanguo2.txt"),new Path("/sanguo/sanguo3.txt"));
fs.delete(new Path("/sanguo/sanguo3.txt"), false); //false表示是否是文件夹
- //迭代器遍历文件夹
- RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/re"), true);
- //true表示是否要递归
- while (files.hasNext()) {
- LocatedFileStatus fileStatus = files.next();
- // 获取每个block的位置信息
- BlockLocation[] blocks = fileStatus.getBlockLocations();
- // 获取block大小
- System.out.println(fileStatus.getBlockSize());
- // 获取路径
- System.out.println(fileStatus.getPath());
- // 获取所有者
- System.out.println(fileStatus.getOwner());
-
-
- for (BlockLocation block : blocks) {
- System.out.println("hosts:" + block.getHosts());
- System.out.println("name:" + block.getNames());
- System.out.println("length:" + block.getLength());
- }
- }

- FileStatus[] fileStatuses = fs.listStatus(new Path("/re"));
- //for循环遍历整个文件夹,判断每个文件是文件还是文件夹
- for (FileStatus fileStatus : fileStatuses) {
- System.out.println(fileStatus.isFile());
- System.out.println(fileStatus.isDirectory());
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。