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 ...
随机推荐
- 7.11 数据注解特性--InverseProperty
我们已经知道了,Code--First默认的约定,如果你没有包含外键属性在父类中,那么他会为我们创建{Class Name}_{primary Key}外键.这个InverseProperty特性用在 ...
- LocalDB在IIS中的运行失败
Using LocalDB with Full IIS, Part 1: User Profile http://blogs.msdn.com/b/sqlexpress/archive/2011/12 ...
- cmd执行SQL语句
首先新建txt文本,复制粘帖以下内容,保存为cmd执行SQL.bat.注意是bat文件 osql -S 服务器名称 -d 数据库 -U 登录名 -P 密码 -i 盘符:\文件夹\文件.sql
- 各种类型转换为字符串类型(ToString())
更详细请参考:http://blog.csdn.net/wanzhuan2010/article/details/8478904 // C 货币 2.5.ToString("C") ...
- java for循环冒泡排序
int[]a =new int[]{7,19,66,21,88,40}; for(int i=1;i<=a.length-1;i++){ //比总长度减1趟就排好序了.因为每一趟出一个值. fo ...
- B/S结构的流程简单概述
在介绍appl ication 对象之前,先简单介绍一些Web 服务器的实现原理. 对于大部分浏览器而言,它通常负责完成三件事情: (1)向远程服务器发送请求. (2)读取远程服务器返 ...
- 发现两个有趣的CSS3效果
一.CSS3画机器猫 http://keleyi.com/keleyi/phtml/html5/3.htm 哆啦A梦效果图: 可用于浏览器对CSS3支持情况的测试 但最近有人对这个测试表示怀疑,指该测 ...
- 各大互联网公司前端面试题(HTML/CSS)
Html篇: 1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? IE: trident内核 Firefox:gecko内核 Safari:webkit内核 Opera:以前是presto ...
- Android中GridView通过自定义适配器(未优化)实现图文视图排列
Android中GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要显示时,可以使用该组件.下面我们通过代码实现如下图例(为了方便截图,将事件处理(土 ...
- plist的读取和写入
// 从plist中读取数组数据 let arrPath = Bundle.main.path(forResource: "ArrayPList", ofType: "p ...