java操作hdfs实例
环境: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包
- <dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-hdfs</artifactId>
- <version>2.6.</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-common</artifactId>
- <version>2.6.</version>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.2</version>
- </dependency>
2、创建普通java类,编写代码,贴出部分代码,如下:
- public class OperaHDFS {
- public static void main(String args[]) throws IOException {
- //测试 创建新文件
- //byte[] contents = "hello world 世界你好\n--created by eclipse\n".getBytes();
- //createFile("/eclipse/first.txt", contents); //或 createFile("hdfs://192.168.137.56:9000/eclipse/first.txt", contents);
- //测试 上传本地文件
- //uploadFile("D:\\c.txt", "/eclipse/");
- //测试重命名
- //rename("/eclipse/c.txt", "/eclipse/cc.txt");
- //测试删除文件
- //delete("/eclipse/cc.txt"); //使用相对路径
- //delete("/eclipse2"); //删除目录
- //测试新建目录
- //mkdir("/eclipse2/");
- //测试读取文件
- //readFile("/eclipse/first.txt");
- //测试文件是否存在
- //fileIsExists("/eclipse/first.txt");
- getNodeMsgHdfs();
- }
- //1、创建新文件(直接生成指定路径下的first.txt,即:/eclipse/first.txt)
- public static void createFile(String dst, byte[] contents) throws IOException {
- Configuration conf = new Configuration();
- System.out.println("-----------:"+conf);
- conf.set("fs.defaultFS", "hdfs://192.168.137.56:9000"); //master
- FileSystem fs = FileSystem.get(conf);
- Path dstPath = new Path(dst); // 目标路径
- // 打开一个输出流
- FSDataOutputStream outputStream = fs.create(dstPath);
- outputStream.write(contents);
- outputStream.close();
- fs.close();
- System.out.println("文件创建成功!");
- }
- //2、上传本地文件
- public static void uploadFile(String src, String dst) throws IOException {
- Configuration conf = new Configuration();
- conf.set("fs.defaultFS", "hdfs://192.168.137.56:9000"); //master
- FileSystem fs = FileSystem.get(conf);
- Path srcPath = new Path(src); // 源路径
- Path dstPath = new Path(dst); // 目标路径
- // 调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
- fs.copyFromLocalFile(false, srcPath, dstPath);
- // 打印文件路径
- System.out.println("Upload to " + conf.get("fs.default.name"));
- //列出指定路径下的所有文件
- System.out.println("------------list files------------" + "\n");
- FileStatus[] fileStatus = fs.listStatus(dstPath);
- for (FileStatus file : fileStatus) {
- System.out.println(file.getPath()+"--"+file.getGroup()+"--"+file.getBlockSize()+"--"+file.getLen()+"--");
- }
- fs.close();
- }
}
3、完整代码,请参考: http://pan.baidu.com/s/1eRsXp6M 密码: 9tg9,里面还有一些关于压缩文件的例子。
java操作hdfs实例的更多相关文章
- memcached—Java操作Memcached实例
前面博客介绍了如何在Windows操作系统中安装Memcached,总结一下如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; imp ...
- java操作Hbase实例
所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...
- hadoop集群配置和在windows系统上运用java操作hdfs
安装 配置 概念 hadoop常用shell命令 使用java操作hadoop 本文介绍hadoop集群配置和在windows系统上运用java操作hdfs 安装 http://mirror.bit. ...
- hadoop学习(三)HDFS常用命令以及java操作HDFS
一.HDFS的常用命令 1.查看根目录下的信息:./hadoop dfs -ls 2.查看根目录下的in目录中的内容:./hadoop dfs -ls in或者./hadoop dfs -ls ./i ...
- 使用java操作HDFS
新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...
- java操作hdfs到数据库或者缓存
使用hadoop工具将数据分析出来以后,须要做入库处理或者存到缓存中.不然就没了意义 一下是使用javaAPI操作hdfs存入缓存的代码: <span style="font-fami ...
- java 操作hdfs(连接HDFS)
FileSystem fs = null; Configuration conf = null; @Before public void init() throws Exception{ conf = ...
- hadoop3自学入门笔记(3)-java 操作hdfs
1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> <val ...
- Java操作HDFS代码样例
代码在GitHub上. 包括如下几种样例代码: 新建文件夹 删除文件/文件夹 重命名文件/文件夹 查看指定路径下的所有文件 新建文件 读文件 写文件 下载文件至本地 上传本地文件 https://gi ...
随机推荐
- 我的微软MVP申请历程
10月10日晚更新: 今天看到这篇博客好多朋友点了推荐上了博客园首页最多推荐,很开心,感谢大家的鼓励! 张善友大哥也写过一篇文章: 10年微软MVP路(如何成为一个MVP?) 写的更为详细,大家也可以 ...
- 数据库排序sql,order by
一开始我认为 SELECT * FROM dbo.T_User ORDER BY CreateTime ,IsDel DESC 的执行顺序是先按创建时间倒序排序,再按isdel倒序排序,所以我就没再S ...
- jQuery网页版五子棋小游戏源码下载
体验效果:http://hovertree.com/texiao/game/4/ 网页五子棋源代码: <!DOCTYPE html> <html> <head> & ...
- 登陆后设置cookie的方法
public void SetCookie(string userName, string role,string cookieValueName) {FormsAuthentication.Form ...
- 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
[源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...
- form表单提交和阻止
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- thinkphp配置文件路径
thinkphp配置文件路径在入口文件index.php中配置. 如果Public目录在应用程序目录同等级位置: 2.如果Public在app内部则: 3.如果使用Public在app外部,但定义为: ...
- [转]看部电影,透透彻彻理解IoC(你没有理由再迷惑!)
之前对依赖注入的概念一直感到模糊,直到看了这篇文章:http://www.iteye.com/topic/1122835 引述: IoC(控制反转:Inverse of Control)是Spring ...
- Java基础知识回顾
Java回顾之I/O Java回顾之网络通信 Java回顾之多线程 Java回顾之多线程同步 Java回顾之集合 Java回顾之序列化 Java回顾之反射 Java回顾之一些基础概念 Java回顾之J ...
- CSS3 float深入理解浮动资料整理
CSS浮动(float,clear)通俗讲解 CSS 浮动 CSS float浮动的深入研究.详解及拓展(一) CSS float浮动的深入研究.详解及拓展(二) 1.浮动实现图文环绕(理解难点) 浮 ...