当前位置: 首页 > news >正文

Hadoop学习之hdfs的操作

Hadoop学习之hdfs的操作

1.将HDFS中的文件复制到本地

package com.shujia.hdfs;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;public class Demo02Download {FileSystem fileSystem;// 创建执行对象// @Before: 前置通知, 在方法执行之前执行@Beforepublic void getFileSystem() throws IOException {Configuration entries = new Configuration();entries.set("fs.defaultFS", "hdfs://master:9000");fileSystem = FileSystem.get(entries);}// 实现文件复制到本地// @Test的作用,省略了public static void main(String[] args) {,表示测试类的方法@Testpublic void getData() throws IOException {String hdfsPath = "/NOTICE.txt";String localPath = "data/";// 将HDFS中的文件复制到本地fileSystem.copyToLocalFile(new Path(hdfsPath),new Path(localPath));}// @After: 后置通知, 在方法执行之后执行 。@Afterpublic void close() throws IOException {fileSystem.close();}}

2.上传数据到HDFS中

package com.shujia.hdfs;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;public class Demo04PutData {public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {/*上传数据到HDFS中*/putData();putData2();}public static void putData() throws IOException {// 没有设置用户信息Configuration entries = new Configuration();entries.set("fs.defaultFS","hdfs://master:9000");FileSystem fileSystem = FileSystem.get(entries);// 从本地上传文件到HDFS上fileSystem.copyFromLocalFile(new Path("hadoop/data/students.txt"),new Path("/data/"));fileSystem.close();}public static void putData2() throws IOException, URISyntaxException, InterruptedException {// 设置了用户信息Configuration entries = new Configuration();entries.set("fs.defaultFS","hdfs://master:9000");/*FileSystem get(final URI uri, final Configuration conf,final String user)*/URI uri = new URI("hdfs://master:9000");// 获取FileSystem的实体类对象(传递uri到get函数中吗,会更改上传到HDFS中文件的用户信息)FileSystem fileSystem = FileSystem.get(uri,entries,"root");fileSystem.copyFromLocalFile(new Path("hadoop/data/students.txt"),new Path("/data/"));fileSystem.close();}
}

3.在HDFS上创建文件目录

package com.shujia.hdfs;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;public class Demo05MakeDir {public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {/*上传数据到HDFS中*/mkdir();}public static void mkdir() throws IOException, URISyntaxException, InterruptedException {// 设置了用户信息Configuration entries = new Configuration();entries.set("fs.defaultFS","hdfs://master:9000");URI uri = new URI("hdfs://master:9000");FileSystem fileSystem = FileSystem.get(uri,entries,"root");fileSystem.mkdirs(new Path("/api"));
//        fileSystem.mkdirs(new Path("/api/1/2"));fileSystem.close();}
}

4.删除HDFS上的文件目录

package com.shujia.hdfs;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;public class Demo06Delete {public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {delete();}public static void delete() throws IOException, URISyntaxException, InterruptedException {// 设置了用户信息Configuration entries = new Configuration();entries.set("fs.defaultFS","hdfs://master:9000");URI uri = new URI("hdfs://master:9000");FileSystem fileSystem = FileSystem.get(uri,entries,"root");//        fileSystem.delete(new Path("/api/1/2"));//TODO 参数recursive:如果path是一个目录并设置为true,则删除该目录,否则抛出异常。//               在文件的情况下,递归可以设置为true或false。fileSystem.delete(new Path("/api"),true);fileSystem.close();}
}

5.查看HDFS文件系统中文件和目录的元数据

package com.shujia.hdfs;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;public class Demo07Liststatus {public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {getBlockLocation();}public static void getBlockLocation() throws IOException, URISyntaxException, InterruptedException {Configuration entries = new Configuration();entries.set("fs.defaultFS","hdfs://master:9000");URI uri = new URI("hdfs://master:9000");FileSystem fileSystem = FileSystem.get(uri,entries,"root");FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hadoop-3.1.3.tar.gz"));System.out.println("路径:"+fileStatus.getPath());System.out.println("长度:"+fileStatus.getLen());System.out.println("副本数:"+fileStatus.getReplication());/*获取一个文件的文件指定开始和结束的部分数据所在的Block块位置BlockLocation[] getFileBlockLocations(FileStatus file,long start, long len)*/BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());for (BlockLocation fileBlockLocation : fileBlockLocations) {System.out.println("整个长度:"+fileBlockLocation.getLength());System.out.println("偏移量,从文件的什么位置开始:"+fileBlockLocation.getOffset());System.out.println("整个主机:"+fileBlockLocation.getHosts());System.out.println("整个名称:"+fileBlockLocation.getNames());}fileSystem.close();}public static void getFileStatus() throws IOException, URISyntaxException, InterruptedException {Configuration entries = new Configuration();entries.set("fs.defaultFS","hdfs://master:9000");URI uri = new URI("hdfs://master:9000");FileSystem fileSystem = FileSystem.get(uri,entries,"root");// getFileStatus()获取FileStatus对象// FileStatus对象封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及权限等信息。FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hadoop-3.1.3.tar.gz"));System.out.println("路径:"+fileStatus.getPath());System.out.println("长度:"+fileStatus.getLen());System.out.println("副本数:"+fileStatus.getReplication());fileSystem.close();}public static void listStatus() throws IOException, URISyntaxException, InterruptedException {// 没有设置用户信息Configuration entries = new Configuration();entries.set("fs.defaultFS","hdfs://master:9000");URI uri = new URI("hdfs://master:9000");FileSystem fileSystem = FileSystem.get(uri,entries,"root");// listStatus()获取FileStatus对象数组,遍历根目录下的所有文件和目录的元数据FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));for (FileStatus fileStatus : fileStatuses) {// 判断其是否为文件(检查这个抽象路径名表示的文件是否是普通文件),若为目录则输出其路径if (fileStatus.isFile()) {long blockSize = fileStatus.getBlockSize();System.out.println(fileStatus.getPath());System.out.println("Block块大小:"+blockSize);System.out.println("长度:"+fileStatus.getLen());}else {System.out.println(fileStatus.getPath());}}fileSystem.close();}
}
http://www.lryc.cn/news/354565.html

相关文章:

  • DBAPI怎么进行数据格式转换
  • Oracle JSON 函数详解与实战
  • C#面:请解释转发与跳转的区别
  • Java+IDEA+SpringBoot药物不良反应ADR智能监测系统源码 ADR智能化监测系统源码
  • linux系统模拟资源消耗的简单手段
  • 吉林大学软件工程简答题整理
  • 爬山算法介绍
  • 在linux中配置关于GFS创建各种卷以及卷组--配置实验
  • 安泰电子:使用高压放大器时有哪些需要注意的呢
  • 为什么大部分新手做抖音小店赚不到钱?
  • 跳跃游戏(2)
  • 11.Redis之zset类型
  • Python怎样将PDF拆分成多个文件
  • C语言-----前置++和后置++的不同
  • 685. 冗余连接 II
  • 自养号测评是什么?亚马逊、沃尔玛、Target卖家如何建立自己的护城河?
  • 计算机毕业设计 | SpringBoot招投标 任务发布网站(附源码)
  • element el-table表格表头某一列表头文字或者背景修改颜色
  • 移动云:连接未来的智慧之旅
  • 如何确保大模型 RAG 生成的信息是基于可靠的数据源?
  • Laravel(Lumen8) + Supervisor 实现多进程redis消息队列
  • 深度学习复盘与小实现
  • 算法刷题笔记 高精度加法(C++实现)
  • php祛除mqtt 返回数据中包含的特殊字符
  • 2024,java开发,已经炸了吗?
  • c++基础篇
  • 卫浴行业All in 智能化,国货品牌拿到了先手棋
  • 分享10个国内可以使用的GPT中文网站
  • golang实现mediasoup的tcp服务及channel通道
  • Spring:IoC容器(基于注解管理bean)