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.mzph.cn/news/841663.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(十四)- 函数式编程(3)

目录 Optional 创建对象 安全消费值 安全获取值 ofElseGet ofElseThrow 过滤 判断 数据转换 函数式接口 只有一个抽象方法的接口称为函数接口 常用默认方法 基本数据类型优化 Optional 可以避免空指针异常<br/> 创建对象 public static void main(String[…

Leetcode.560.和为k的子数组

原题链接&#xff1a;链接 代码&#xff1a; class Solution { public:int subarraySum(vector<int>& nums, int k) {int n nums.size();vector<int> vec(n1);for(int i 1;i<n;i){vec[i] vec[i-1]nums[i-1];}unordered_map<int,int> hash;hash[0…

Linux-应用编程学习笔记(三、文件属性和目录)

一、文件类型 1、普通文件&#xff08;ls -l 文件&#xff0c;权限前边第一个"-"代表普通文件&#xff1b;stat 文件&#xff09; 文本文件&#xff1a;ASCII字符 二进制文件&#xff1a;数字0/1 2、目录文件&#xff08;‘’d&#xff09;&#xff1a;文件夹 3…

利用微服务SpringCloud如何实现熔断?

熔断是一种保护机制&#xff0c;用于处理由于服务故障或负载过重引起的服务请求失败问题。在分布式系统中&#xff0c;如果一个服务发生故障或负载过重&#xff0c;它可能会导致其他依赖于它的服务也出现故障&#xff0c;最终导致整个系统崩溃。熔断器就是为了解决这个问题而设…

c语言IO

前言 老是忘记c语言IO操作&#xff0c;故写个文章记录一下 打开文件 fopen FILE *fopen(const char *path, const char *mode);mode 返回值 如果文件成功打开&#xff0c;fopen 返回一个指向 FILE 结构的指针。如果文件打开失败&#xff08;例如&#xff0c;因为文件不存…

Flutter 中的 RotationTransition 小部件:全面指南

Flutter 中的 RotationTransition 小部件&#xff1a;全面指南 在 Flutter 中&#xff0c;动画是增强用户界面和提供流畅用户体验的强大工具。RotationTransition 是 Flutter 提供的一种动画组件&#xff0c;用于在父组件大小变化时旋转子组件。本文将详细介绍 RotationTransi…

AWS数据库之Neptune

Amazon Neptune是一项快速、可靠且完全托管的图形数据库服务&#xff0c;可帮助我们轻松构建和运行使用高度互连数据集的应用程序。Amazon Neptune的核心是专门构建的高性能图形数据库引擎&#xff0c;它进行了优化以存储数十亿个关系并将图形查询延迟降低到毫秒级。 Amazon N…

跨平台之用VisualStudio开发APK嵌入OpenCV(三)

本篇将包含以下内容&#xff1a; 1.使用 Visual Studio 2019 开发一个 Android 的 App 2.导入前篇 C 编译好的 so 动态库 3.一些入门必须的其它设置 作为入门&#xff0c;我们直接使用真机进行调试&#xff0c;一方面运行速度远高于模拟器&#xff0c;另一方面模拟器使用的…

java 拦截器-用户无操作超时退出利用Redis

1、授权过滤&#xff0c;只要实现AuthConfigAdapter接口 2、利用Redis token超时时间&#xff0c;用户访问后台续时 效果 Component public class AuthFilter implements Filter {private static Logger logger LoggerFactory.getLogger(AuthFilter.class);Autowiredprivat…

OWASP top10--SQL注入(二)

目录 06&#xff1a;SQL注入提交方式 6.1、get提交 6.2、post提交 6.3、cookie提交 6.4、HTTP Header头提交 07&#xff1a;注入攻击支持类型 7.1、union注入&#xff1a; 7.1.1、union操作符一般与order by语句配合使用 7.1.2、information_schema注入 7.2、基于函数…

RAG技术中的文本分块与重排序策略:提升大型语言模型应用效果的关键

在探讨RAG&#xff08;Retrieval-Augmented Generation&#xff09;技术中的文本分块&#xff08;Chunking&#xff09;方法时&#xff0c;我们需关注其对大型语言模型&#xff08;LLM&#xff09;应用效果的直接影响。文本分块是处理和优化信息检索过程的关键步骤&#xff0c;…

在gitlab CICD中 小试 hooks:pre_get_sources_script 功能

参考链接&#xff1a; hooks:pre_get_sources_script 功能简介 hooks:pre_get_sources_script 是gitlab CICD中的一个功能&#xff0c;该功能可以指定在克隆 Git 仓库和任何子模块之前要在执行器上执行的某些命令。例如&#xff1a; 调整 Git 配置导出跟踪变量 下来简单给…

(已开源-ICRA2023) High Resolution Point Clouds from mmWave Radar

本文提出了一种用于生成高分辨率毫米波雷达点云的方法&#xff1a;RadarHD&#xff0c;端到端的神经网络&#xff0c;用于从低分辨率雷达构建类似激光雷达的点云。本文通过在大量原始雷达数据上训练 RadarHD 模型&#xff0c;同时这些雷达数据有对应配对的激光雷达点云数据。本…

<Python实际应用>用yolov9实现垃圾检测

公司一个项目需要在无人机巡检的画面中识别垃圾和汽车&#xff0c;正好听闻yolov9最新出炉&#xff0c;于是试了一下采用yolov9来搭建该项目 1.下载和部署 下载yolov9:GitHub地址&#xff1a;GitHub代码下载地址 配置环境可以参考之前关于yolov5的文章 Yolov5自学笔记之一-…

vcpkg环境配置

vcpkg 使用linux相关库&#xff0c;设置环境变量VCPKG_ROOT&#xff0c;设置cmake工具链$VCPKG_ROOT/scripts\buildsystems\vcpkg.cmake set VCPKG_DEFAULT_TRIPLETx64-windows .\vcpkg.exe install fftw3 freetype gettext glibmm gtkmm libjpeg-turbo libpng libxmlpp libs…

驱动开发之新字符设备驱动开发

1.前言 register_chrdev 和 unregister_chrdev 这两个函数是老版本驱动使用的函数&#xff0c;现在新的 字符设备驱动已经不再使用这两个函数&#xff0c;而是使用 Linux 内核推荐的新字符设备驱动 API 函数。 旧版本的接口使用&#xff0c;感兴趣可以看下面这个博客&#…

免费图片文字转换成文本,ocr文字识别软件免费版,真的太实用了!

截屏短视频上一段扎心文字&#xff0c;想把它发到朋友圈怎么办&#xff1f;这时候就需要一个OCR识别软件。 它就像一个聪明的小助手&#xff0c;它可以帮助电脑“看懂”书本上或者图片里的字。就像我们用眼睛看字一样&#xff0c;OCR软件用它的“眼睛”扫描图片&#xff0c;识…

百亿级流量红包系统,如何做架构?(字节面试真题)

尼恩说在前面 在40岁老架构师 尼恩的读者交流群(50)中&#xff0c;最近有小伙伴拿到了一线互联网企业如得物、阿里、滴滴、极兔、有赞、希音、百度、网易、美团的面试资格&#xff0c;遇到很多很重要的架构类/设计类的场景题&#xff1a; 1.如何设计高并发红包系统 &#xff0…

【移动云】云端赋能——数字化时代游戏与工作的新境界

前言 在当今这个信息化、数字化的时代&#xff0c;云计算、大数据和人工智能等前沿技术已经深入到我们生活的方方面面。作为我国通信行业的领军企业&#xff0c;中国移动凭借其在5G技术领域的领先优势&#xff0c;推出了基于移动云计算技术的云业务品牌——移动云。移动云以云操…

猫抓(cat-catch)插件的常规用法

目录 1.1、前言1.2、抓取图片资源1.3、抓取音频资源1.4、抓取视频资源 1.1、前言 本文将介绍利用猫抓&#xff08;cat-catch&#xff09;插件如下抓取网页上的图片、音频、视频等资源&#xff0c;猫抓&#xff08;cat-catch&#xff09;插件的安装及设置请参考推荐一款媒体影音…