环境:window7+eclipse+vmware虚拟机+搭建好的hadoop环境(master、slave01、slave02)

内容:主要是在windows环境下,利用eclipse如何来操作hdfs,如上传文件、删除文件、创建文件夹、查看节点信息等。

eclipse开发环境的搭建,请参考:http://www.cnblogs.com/bookwed/p/4816521.html

1、新建maven项目,(主要是因为要引入一些jar包,除非是特别清楚要引入哪些jar包可以不用建maven项目)

  创建web项目的细节不作说明了,下面把关键的pom依赖信息贴出来,这里主要是hadoop的基础包和hdfs包  

  1. <dependency>
  2. <groupId>org.apache.hadoop</groupId>
  3. <artifactId>hadoop-hdfs</artifactId>
  4. <version>2.6.</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.hadoop</groupId>
  8. <artifactId>hadoop-common</artifactId>
  9. <version>2.6.</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>commons-logging</groupId>
  13. <artifactId>commons-logging</artifactId>
  14. <version>1.2</version>
  15. </dependency>

2、创建普通java类,编写代码,贴出部分代码,如下:

  1. public class OperaHDFS {
  2. public static void main(String args[]) throws IOException {
  3. //测试 创建新文件
  4. //byte[] contents = "hello world 世界你好\n--created by eclipse\n".getBytes();
  5. //createFile("/eclipse/first.txt", contents); //或 createFile("hdfs://192.168.137.56:9000/eclipse/first.txt", contents);
  6.  
  7. //测试 上传本地文件
  8. //uploadFile("D:\\c.txt", "/eclipse/");
  9.  
  10. //测试重命名
  11. //rename("/eclipse/c.txt", "/eclipse/cc.txt");
  12.  
  13. //测试删除文件
  14. //delete("/eclipse/cc.txt"); //使用相对路径
  15. //delete("/eclipse2"); //删除目录
  16.  
  17. //测试新建目录
  18. //mkdir("/eclipse2/");
  19.  
  20. //测试读取文件
  21. //readFile("/eclipse/first.txt");
  22.  
  23. //测试文件是否存在
  24. //fileIsExists("/eclipse/first.txt");
  25.  
  26. getNodeMsgHdfs();
  27.  
  28. }
  29.  
  30. //1、创建新文件(直接生成指定路径下的first.txt,即:/eclipse/first.txt)
  31. public static void createFile(String dst, byte[] contents) throws IOException {
  32. Configuration conf = new Configuration();
  33. System.out.println("-----------:"+conf);
  34. conf.set("fs.defaultFS", "hdfs://192.168.137.56:9000"); //master
  35. FileSystem fs = FileSystem.get(conf);
  36. Path dstPath = new Path(dst); // 目标路径
  37. // 打开一个输出流
  38. FSDataOutputStream outputStream = fs.create(dstPath);
  39. outputStream.write(contents);
  40. outputStream.close();
  41. fs.close();
  42. System.out.println("文件创建成功!");
  43. }
  44.  
  45. //2、上传本地文件
  46. public static void uploadFile(String src, String dst) throws IOException {
  47. Configuration conf = new Configuration();
  48. conf.set("fs.defaultFS", "hdfs://192.168.137.56:9000"); //master
  49. FileSystem fs = FileSystem.get(conf);
  50. Path srcPath = new Path(src); // 源路径
  51. Path dstPath = new Path(dst); // 目标路径
  52. // 调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
  53. fs.copyFromLocalFile(false, srcPath, dstPath);
  54.  
  55. // 打印文件路径
  56. System.out.println("Upload to " + conf.get("fs.default.name"));
  57. //列出指定路径下的所有文件
  58. System.out.println("------------list files------------" + "\n");
  59. FileStatus[] fileStatus = fs.listStatus(dstPath);
  60. for (FileStatus file : fileStatus) {
  61. System.out.println(file.getPath()+"--"+file.getGroup()+"--"+file.getBlockSize()+"--"+file.getLen()+"--");
  62. }
  63. fs.close();
  64. }
      }

3、完整代码,请参考: http://pan.baidu.com/s/1eRsXp6M 密码: 9tg9,里面还有一些关于压缩文件的例子。

  

  

java操作hdfs实例的更多相关文章

  1. memcached—Java操作Memcached实例

    前面博客介绍了如何在Windows操作系统中安装Memcached,总结一下如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; imp ...

  2. java操作Hbase实例

    所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...

  3. hadoop集群配置和在windows系统上运用java操作hdfs

    安装 配置 概念 hadoop常用shell命令 使用java操作hadoop 本文介绍hadoop集群配置和在windows系统上运用java操作hdfs 安装 http://mirror.bit. ...

  4. hadoop学习(三)HDFS常用命令以及java操作HDFS

    一.HDFS的常用命令 1.查看根目录下的信息:./hadoop dfs -ls 2.查看根目录下的in目录中的内容:./hadoop dfs -ls in或者./hadoop dfs -ls ./i ...

  5. 使用java操作HDFS

    新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...

  6. java操作hdfs到数据库或者缓存

    使用hadoop工具将数据分析出来以后,须要做入库处理或者存到缓存中.不然就没了意义 一下是使用javaAPI操作hdfs存入缓存的代码: <span style="font-fami ...

  7. java 操作hdfs(连接HDFS)

    FileSystem fs = null; Configuration conf = null; @Before public void init() throws Exception{ conf = ...

  8. hadoop3自学入门笔记(3)-java 操作hdfs

    1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> <val ...

  9. Java操作HDFS代码样例

    代码在GitHub上. 包括如下几种样例代码: 新建文件夹 删除文件/文件夹 重命名文件/文件夹 查看指定路径下的所有文件 新建文件 读文件 写文件 下载文件至本地 上传本地文件 https://gi ...

随机推荐

  1. 我的微软MVP申请历程

    10月10日晚更新: 今天看到这篇博客好多朋友点了推荐上了博客园首页最多推荐,很开心,感谢大家的鼓励! 张善友大哥也写过一篇文章: 10年微软MVP路(如何成为一个MVP?) 写的更为详细,大家也可以 ...

  2. 数据库排序sql,order by

    一开始我认为 SELECT * FROM dbo.T_User ORDER BY CreateTime ,IsDel DESC 的执行顺序是先按创建时间倒序排序,再按isdel倒序排序,所以我就没再S ...

  3. jQuery网页版五子棋小游戏源码下载

    体验效果:http://hovertree.com/texiao/game/4/ 网页五子棋源代码: <!DOCTYPE html> <html> <head> & ...

  4. 登陆后设置cookie的方法

    public void SetCookie(string userName, string role,string cookieValueName) {FormsAuthentication.Form ...

  5. 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧

    [源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...

  6. form表单提交和阻止

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. thinkphp配置文件路径

    thinkphp配置文件路径在入口文件index.php中配置. 如果Public目录在应用程序目录同等级位置: 2.如果Public在app内部则: 3.如果使用Public在app外部,但定义为: ...

  8. [转]看部电影,透透彻彻理解IoC(你没有理由再迷惑!)

    之前对依赖注入的概念一直感到模糊,直到看了这篇文章:http://www.iteye.com/topic/1122835 引述: IoC(控制反转:Inverse of Control)是Spring ...

  9. Java基础知识回顾

    Java回顾之I/O Java回顾之网络通信 Java回顾之多线程 Java回顾之多线程同步 Java回顾之集合 Java回顾之序列化 Java回顾之反射 Java回顾之一些基础概念 Java回顾之J ...

  10. CSS3 float深入理解浮动资料整理

    CSS浮动(float,clear)通俗讲解 CSS 浮动 CSS float浮动的深入研究.详解及拓展(一) CSS float浮动的深入研究.详解及拓展(二) 1.浮动实现图文环绕(理解难点) 浮 ...