1.上传本地文件到HDFS

  1. //上传本地文件到HDFS
  2. public class CopyFile {
  3. public static void main(String[] args) {
  4. try {
  5. Configuration conf = new Configuration();
  6. String str_src = "/usr/local/myjar/mongo/地图数据/Zhengye_Drive_Testing_Data/solu"
  7. + "/solu_Yanming_DriveTesting_09-04.16-17.16-27_True_TA.json";
  8. String str_dst = "hdfs://node4:9000/user/hadoop/TestFile.json";
  9.  
  10. Path src = new Path(str_src); //本地地址
  11. Path dst = new Path(str_dst); //hdfs地址
  12.  
  13. FileSystem hdfs = dst.getFileSystem(conf);
  14. //FileSystem hdfs = FileSystem.get(URI.create(str_dst),conf); //这样也可以
  15. //伪分布式上面两种都可以,如果直接FileSystem.get(conf),可能出现错误
  16.  
  17. hdfs.copyFromLocalFile(src, dst);
  18. System.out.println("Upload to "+conf.get("fs.default.name"));
  19.  
  20. FileStatus files[] = hdfs.listStatus(dst);
  21. for(FileStatus file:files){
  22. System.out.println(file.getPath());
  23. }
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }

可能出现的错误 Wrong FS解决方法:
http://blog.csdn.net/kurama_sai/article/details/8604640
http://blog.itpub.net/22846396/viewspace-1119945

2. 在hdfs中创建文件,并写入一行文字

  1. //创建文件,并向文件中写入一行文字
  2. public class CreateFile {
  3. public static void main(String[] args) {
  4. try {
  5. Configuration conf = new Configuration();
  6. byte[] buff = "This is a test line.".getBytes();
  7. String dsf = "hdfs://node4:9000/user/hadoop/Test";
  8. Path pathdsf = new Path(dsf);
  9. FileSystem hdfs = pathdsf.getFileSystem(conf);
  10. FSDataOutputStream outputStream = hdfs.create(pathdsf);
  11. outputStream.write(buff,0,buff.length);
  12. System.out.println("Finish write!");
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }

3.删除文件

  1. Configuration conf = new Configuration();
  2. Path path_del = new Path("hdfs://node4:9000/user/hadoop/Test2");
  3. FileSystem hdfs = path_del.getFileSystem(conf);
  4. boolean isDeleted = hdfs.delete(path_del,false);
  5. //hdfs.delete(path_del,true); //递归删除,如果path_del是一个文件夹,将文件夹以及下面的子文件全删除
  6. System.out.println("delete? " +isDeleted);

4.重命名文件

  1. Configuration conf = new Configuration();
  2. Path path_fr = new Path("hdfs://node4:9000/user/hadoop/Test");
  3. Path path_to = new Path("hdfs://node4:9000/user/hadoop/Test2");
  4. FileSystem hdfs = path_fr.getFileSystem(conf);
  5. boolean isRename = hdfs.rename(path_fr, path_to); //对文件进行重命名
  6. System.out.println("is rename? "+isRename);

5.查看文件以及文件系统的各项信息

  1. Configuration conf = new Configuration();
  2. Path findf = new Path("hdfs://node4:9000/user/hadoop/hadoop.txt");
  3. FileSystem hdfs = findf.getFileSystem(conf);
  4.  
  5. //查看某个HDFS文件是否存在
  6. boolean isExists = hdfs.exists(findf); //查看文件或文件夹是否存在
  7. System.out.println("exists? " + isExists);
  8.  
  9. //查看HDFS文件的属性
  10. FileStatus filestatus = hdfs.getFileStatus(findf);
  11. long modificationTime = filestatus.getModificationTime(); //最后修改时间
  12. System.out.println("Modification time is: "+modificationTime);
  13. long blocksize = filestatus.getBlockSize(); //块大小
  14. System.out.println("Block size is: "+blocksize);
  15.  
  16. //查看某个文件在HDFS集群的位置
  17. BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus, 0, filestatus.getLen());
  18. int blockLen = blkLocations.length;
  19. for(int i = 0 ; i < blockLen ; i++){
  20. String[] hosts = blkLocations[i].getHosts();
  21. System.out.println("block "+i+" location: "+hosts[i]);
  22. }
  23.  
  24. //查看hdfs文件系统的的各项信息
  25. System.out.println("scheme: "+hdfs.getScheme());
  26. System.out.println("used: "+hdfs.getUsed());
  27. System.out.println("canonical service name: "+hdfs.getCanonicalServiceName());
  28. System.out.println("default block size: "+hdfs.getDefaultBlockSize(findf));

输出结果:

exists? true
Modification time is: 1430225267896
Block size is: 134217728
block 0 location: node4
scheme: hdfs
used: 0
canonical service name: 192.168.1.160:9000
default block size: 134217728

6.读取HDFS中的文件内容

下面代码的效果就是Test文件的内容输出

  1. String dsf = "hdfs://node4:9000/user/hadoop/Test";
  2. Configuration conf = new Configuration();
  3.  
  4. Path pathdsf = new Path(dsf);
  5.  
  6. FileSystem fs = FileSystem.get(URI.create(dsf), conf);
  7. //FileSystem fs = pathdsf.getFileSystem(conf); //这样也可以
  8.  
  9. FSDataInputStream hdfsInStream = fs.open(pathdsf);
  10.  
  11. byte[] ioBuffer = new byte[1024];
  12. int readLen = hdfsInStream.read(ioBuffer);
  13. while (readLen != -1) {
  14. System.out.write(ioBuffer, 0, readLen);
  15. readLen = hdfsInStream.read(ioBuffer);
  16. }
  17. hdfsInStream.close();
  18. fs.close();

7.获取集群上所有节点的名称

  1. Configuration conf = new Configuration();
  2. Path path = new Path("hdfs://node4:9000/user/hadoop");
  3. FileSystem fs = path.getFileSystem(conf);
  4. DistributedFileSystem dfs = (DistributedFileSystem) fs;
  5. DatanodeInfo[] dataNodeStats = dfs.getDataNodeStats();
  6.  
  7. String[] names = new String[dataNodeStats.length];
  8. for(int i = 0 ; i < dataNodeStats.length ; i++){
  9. names[i] = dataNodeStats[i].getHostName();
  10. System.out.println("no."+i+", name:"+names[i]);
  11. }

输出的就是节点名称
no.0, name:node4
no.1, name:node3

HDFS操作--文件上传/创建/删除/查询文件信息的更多相关文章

  1. HTTP文件上传服务器-支持超大文件HTTP断点续传的实现办法

    最近由于笔者所在的研发集团产品需要,需要支持高性能的大文件http上传,并且要求支持http断点续传.笔者在以前的博客如何实现支持大文件的高性能HTTP文件上传服务器已经介绍了实现大文件上传的一些基本 ...

  2. PHP实现单文件、多文件上传 封装 面向对象实现文件上传

    文件上传配置 客户端配置 1.表单页面 2.表单的发送方式为post 3.添加enctype = "multipart/form-data" <form action=&qu ...

  3. SpringBoot - 实现文件上传2(多文件上传、常用上传参数配置)

    在前文中我介绍了 Spring Boot 项目如何实现单文件上传,而多文件上传逻辑和单文件上传基本一致,下面通过样例进行演示. 多文件上传 1,代码编写 1)首先在 static 目录中创建一个 up ...

  4. php 文件上传后缀名与文件类型对照表(几乎涵盖所有文件)

    网上有很多php文件上传的类,文件上传处理是php的一个特色(至少手册上是将此作为php特点来展示的,个人认为php在数组方面的优异功能更有特 色),学php的人都知道文件上传怎么做,但很多人在编程中 ...

  5. django设置并获取cookie/session,文件上传,ajax接收文件,post/get请求及跨域请求等的方法

    django设置并获取cookie/session,文件上传,ajax接收文件等的方法: views.py文件: from django.shortcuts import render,HttpRes ...

  6. SpringMVC ajax技术无刷新文件上传下载删除示例

    参考 Spring MVC中上传文件实例 SpringMVC结合ajaxfileupload.js实现ajax无刷新文件上传 Spring MVC 文件上传下载 (FileOperateUtil.ja ...

  7. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

  8. c# txt 文件上传、写入TXT文件、创建图形验证码

    asp.net mvc 图片上传 html 在使用包含文件上传控件的表单时,必须使用 enctype="multipart/form-data" 属性 <form encty ...

  9. 利用Rsync同步工具上传、删除目标文件

    Rsync是文件备份工具,当然也可以当做传输工具,管理远程服务器的文件 上传 rsync -avzP --progress --port 9106 /path/.../指定文件 root@192.16 ...

随机推荐

  1. .net performance

    http://msdn.microsoft.com/en-us/library/ms173196.aspx http://www.zhihu.com/question/20314377 http:// ...

  2. Windows 中默认安装的.Net 版本

    Windows contains a version of .NET by default. Here's a listing of them. XP .NET v1.0 -- Service pac ...

  3. Android 获取imageview的图,在另一个imageview里显示。

    当我点击默认头像里的其中一个然后在点确定就在最上面的那个imageview里显示选择的头像.求大神. img1和img2都是ImageView,要把img1中的图片显示到img2中 前景(对应src属 ...

  4. BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 4 ...

  5. 安装与使用smarty

    1.安装 下载最新的smarty.下载地址:http://www.smarty.net/download 下载成功后,解压压缩包后的文件如图所示: 将解压后的文件存放在web文档根目录外的某个位置.w ...

  6. for

    1,cout在显示bool值之前将他们转换为int,但cout.setf(ios::boolalpha)函数调用设置了一个标记,标记命令cout显示true 和 false 而不是 1 和0;

  7. java中文乱码解决之道(六)—–javaWeb中的编码解码

    在上篇博客中LZ介绍了前面两种场景(IO.内存)中的java编码解码操作,其实在这两种场景中我们只需要在编码解码过程中设置正确的编码解码方式一般而言是不会出现乱码的.对于我们从事java开发的人而言, ...

  8. socket实现局域网通信

    今天实现了一个局域网通信的小例子,上来记录一下,代码不成熟,勿拍. 这是我本机客户端: 这是我虚拟机的客户端. 我为他们分配了静态IP,这样就可以实现局域网通信了.注意代码中必须把监视线程的IsBac ...

  9. Android应用程序之间共享文字和图片(二)

    MainActivity如下: package cn.testshare1; import java.io.File; import java.util.ArrayList; import andro ...

  10. JAVA基础1

    阶段0:拟出一个计划 阶段1:要制作什么? 阶段2:如何构建? 阶段3:开始创建 阶段4:校订 阶段5:计划的回报   一.程序运行时,数据保存位置 1.寄存器.这是最快的保存区域,因为它位于和其他所 ...