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

做物流网站电话东莞市建筑业协会

做物流网站电话,东莞市建筑业协会,个性flash网站,网站建设的一般流程是什么意思HDFS的API操作 1 HDFS 核心类简介 Configuration类:处理HDFS配置的核心类。 FileSystem类:处理HDFS文件相关操作的核心类,包括对文件夹或文件的创建,删除,查看状态,复制,从本地挪动到HDFS文件系统中等。…

HDFS的API操作

1 HDFS 核心类简介

Configuration类:处理HDFS配置的核心类。

FileSystem类:处理HDFS文件相关操作的核心类,包括对文件夹或文件的创建,删除,查看状态,复制,从本地挪动到HDFS文件系统中等。

Path类:处理HDFS文件路径。

IOUtils类:处理HDFS文件读写的工具类。

2 HDFS文件处理类FileSystem的核心方法介绍:

1. FileSystem get(URI uri, Configuration conf)根据HDFSURI和配置,创建FileSystem实例2. public boolean mkdirs(Path f) throws IOException根据路径创建HDFS文件夹3. FSDataOutput Stream create(Path f, boolean overwrite)根据具体的路径创建文件,并且知名是否以重写的方式4. abstract boolean delete(Path f, boolean recursive)根据路径删除文件5. abstract FileStatus[] listStatus(Path f)根据路径,返回该路径下所有文件夹或文件的状态。6. Void moveFromLocalFile(Path src, Path dst)将本地路径下的文件,挪动到HDFS的指定路径下7. FSDataInputStream open(Path f)打开指定路径下的文件内容

3 执行流程

maven依赖

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>RELEASE</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.8.2</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.3.2</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.3.2</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>3.3.2</version></dependency></dependencies>

hdfs 创建文件夹

   public static void main(String[] args) throws IOException, Exception, URISyntaxException {Configuration conf = new Configuration();
//		conf.set("fs.defaultFS", "hdfs://hadoop102:9000");// 1 获取hdfs客户端对象
//		FileSystem fs = FileSystem.get(conf );FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf, "root");// 2 在hdfs上创建路径fs.mkdirs(new Path("/dir01/"));// 3 关闭资源fs.close();System.out.println("over");}

1 HDFS文件上传(测试参数优先级)

// 1 文件上传@Testpublic void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException{// 1 获取fs对象Configuration conf = new Configuration();conf.set("dfs.replication", "2");FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 执行上传APIfs.copyFromLocalFile(new Path("e:/info.txt"), new Path("/file1.txt"));// 3 关闭资源fs.close();}

2 HDFS文件下载

  // 2 文件下载@Testpublic void testCopyToLocalFile() throws URISyntaxException, IOException, InterruptedException {// 1 获取对象Configuration conf = new Configuration();// conf.set("dfs.replication", "2");FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 执行下载操作
//		fs.copyToLocalFile(new Path("/banhua.txt"), new Path("e:/banhua.txt"));fs.copyToLocalFile(false, new Path("/file1.txt"), new Path("e:/file2.txt"), true);// 3 关闭资源fs.close();}

3 HDFS文件夹删除

// 3 文件删除@Testpublic void testDelete() throws IOException, InterruptedException, URISyntaxException{// 1 获取对象Configuration conf = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 文件删除fs.delete(new Path("/dir01"), true);// 3 关闭资源fs.close();}

4 HDFS文件名更改

// 4 文件更名@Testpublic void testRename() throws IOException, InterruptedException, URISyntaxException{// 1 获取对象Configuration conf = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 执行更名操作fs.rename(new Path("/file1.txt"), new Path("/file111.txt"));// 3 关闭资源fs.close();}

5 HDFS文件详情查看

查看文件名称、权限、长度、块信息

// 5 文件详情查看@Testpublic void testListFiles() throws IOException, InterruptedException, URISyntaxException{// 1 获取对象Configuration conf = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 查看文件详情RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);while(listFiles.hasNext()){LocatedFileStatus fileStatus = listFiles.next();// 查看文件名称、权限、长度、块信息System.out.println(fileStatus.getPath().getName());// 文件名称System.out.println(fileStatus.getPermission());// 文件权限System.out.println(fileStatus.getLen());// 文件长度BlockLocation[] blockLocations = fileStatus.getBlockLocations();for (BlockLocation blockLocation : blockLocations) {String[] hosts = blockLocation.getHosts();for (String host : hosts) {System.out.println(host);}}System.out.println("------ok分割线--------");}// 3 关闭资源fs.close();}

6 HDFS文件和文件夹判断

// 6 判断是文件还是文件夹@Testpublic void testListStatus() throws IOException, InterruptedException, URISyntaxException{// 1 获取对象Configuration conf = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");// 2 判断操作FileStatus[] listStatus = fs.listStatus(new Path("/"));for (FileStatus fileStatus : listStatus) {if (fileStatus.isFile()) {// 文件System.out.println("f:"+fileStatus.getPath().getName());}else{// 文件夹System.out.println("d:"+fileStatus.getPath().getName());}}// 3 关闭资源fs.close();}

4 HDFS的I/O流操作

上面我们学的API操作HDFS系统都是框架封装好的。那么如果我们想自己实现上述API的操作该怎么实现呢?
我们可以采用IO流的方式实现数据的上传和下载。

1 HDFS文件上传

1.需求:把本地e盘上的banhua.txt文件上传到HDFS根目录
2.编写代码

@Testpublic void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), configuration, "root");// 2 创建输入流FileInputStream fis = new FileInputStream(new File("e:/hahaha.txt"));// 3 获取输出流FSDataOutputStream fos = fs.create(new Path("/hahaha.txt"));// 4 流对拷IOUtils.copyBytes(fis, fos, configuration);// 5 关闭资源IOUtils.closeStream(fos);IOUtils.closeStream(fis);fs.close();}

2 HDFS文件下载

1.需求:从HDFS上下载banhua.txt文件到本地e盘上
2.编写代码

@Testpublic void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{// 1 获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), configuration, "root");// 2 获取输入流FSDataInputStream fis = fs.open(new Path("/jinan/info/lenovo/hello.txt"));// 3 获取输出流FileOutputStream fos = new FileOutputStream(new File("e:/hello.txt"));// 4 流的对拷IOUtils.copyBytes(fis, fos, configuration);// 5 关闭资源IOUtils.closeStream(fos);IOUtils.closeStream(fis);fs.close();}
http://www.yayakq.cn/news/79545/

相关文章:

  • 共享ip服务器做网站网站开发毕业答辩问题
  • 动画视频制作手机端网站外部链接如何去优化
  • 图片无版权网站状态管理名词解释网站开发
  • 提升访问境外网站速度免费申请网站 免备案
  • 网站开发技术的雏形 cgi长沙网站营销
  • 传奇背景图网站怎么做岳阳seo招聘
  • 工商联网站建设方案wordpress如何更换域名
  • 网站制作公司获取客户做外单网站有哪些内容
  • .net做网站之前设置惠州做棋牌网站建设哪家便宜
  • wordpress配置多站点广州专业做继承案件律师
  • 网站制作 系统定制网络上市场推广
  • 网站建设技能考试试题三房地产新闻时事热点
  • 建设网站的公司广州泰来县城乡建设局网站
  • 什么网站做啤酒重庆建设集团官方网站
  • cms网站模板 数据采集网站建设中采用的技术
  • 免费做企业网站好牌子商城网
  • 百度资源站长平台网站建设一般流程
  • 共享备案网站教学网站制作
  • 做网站买什么笔记本好台州市建设局网站
  • 网站恶意镜像 301推广普通话主题班会ppt
  • 网站主题类型怎么在网站做推广不要钱
  • 导购网站怎么做有特色自己制作简易网页
  • 给网站做友情链接个人网页设计的主要内容和要求
  • 商铺免费做的网站是普通网站地图好还是rss地图好一点
  • 网站开发都需要什么软件租车公司哪家好
  • 太原seo网站排名优化推广普通话的宣传语
  • 做淘宝客网站挣钱微商城网站策划
  • 网站游戏网站建设漯河百度做网站电话
  • 像淘客基地这样的网站如何做北京综合网站建设报价
  • 烟台住房和城乡建设厅网站南宁小程序制作