hadoop SequenceFile示例
1、写入,SequenceFile的key和value不一定是Writable,只要能被Serialization序列化和反序列化就可以。
private static final String[] DATA = {
"One, two, buckle my shoe",
"Three, four, shut the door",
"Five, six, pick up sticks",
"Seven, eight, lay them straight",
"Nine, ten, a big fat hen"
};
private static void writeSequenceFile(String[] args) throws IOException{
String uri = args[];
Configuration conf = new Configuration();
Path path = new Path(uri);
IntWritable key = new IntWritable();
Text value = new Text();
SequenceFile.Writer writer = null;
SequenceFile.Writer.Option option1 = SequenceFile.Writer.file(path);
SequenceFile.Writer.Option option2 = SequenceFile.Writer.keyClass(key.getClass());
SequenceFile.Writer.Option option3 = SequenceFile.Writer.valueClass(value.getClass());
SequenceFile.Writer.Option option4 = SequenceFile.Writer.compression(SequenceFile.CompressionType.RECORD);
try {
writer = SequenceFile.createWriter(conf,option1,option2,option3,option4);
for (int i=;i<;i++){
key.set(i+);
value.set(DATA[i% DATA.length]);
System.out.printf("[%s]\t%s\t%s\n",writer.getLength(), key,value);
writer.append(key,value);
if(i%DATA.length==0)
writer.sync();//写入同步点
}
}finally {
IOUtils.closeStream(writer);
}
}
2、读取,如果key和value使用了Writable,使用next()方法就可以把数据读入到key和value中,对于非Writable的类型的序列化需要根据实际实现来判断。
private static void readSequenceFile(String[] args) throws IOException{
String uri = args[0];
Configuration conf = new Configuration();
Path path = new Path(uri);
SequenceFile.Reader reader = null;
SequenceFile.Reader.Option option1 = SequenceFile.Reader.file(path);
try {
reader = new SequenceFile.Reader(conf,option1);
Writable key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(),conf);
Writable value = (Writable)ReflectionUtils.newInstance(reader.getValueClass(),conf);
long position = reader.getPosition();
while (reader.next(key,value)){
String syncSeen = reader.syncSeen() ? "*":"";//同步位显示为*号
System.out.printf("[%s%s]\t%s\t%s\n",position,syncSeen,key,value);
position = reader.getPosition();
}
}finally {
IOUtils.closeStream(reader);
}
}
hadoop SequenceFile示例的更多相关文章
- Hadoop SequenceFile数据结构介绍及读写
在一些应用中,我们需要一种特殊的数据结构来存储数据,并进行读取,这里就分析下为什么用SequenceFile格式文件. Hadoop SequenceFile Hadoop提供的SequenceFil ...
- 基于Hadoop Sequencefile的小文件解决方案
一.概述 小文件是指文件size小于HDFS上block大小的文件.这样的文件会给hadoop的扩展性和性能带来严重问题.首先,在HDFS中,任何block,文件或者目录在内存中均以对象的形式存储,每 ...
- <Hadoop><SequenceFile><Hadoop小文件>
Origin 我们首先理解一下SequenceFile试图解决什么问题,然后看SeqFile怎么解决这些问题. In HDFS 序列文件是解决Hadoop小文件问题的一个方法: 小文件是显著小于HDF ...
- Hadoop SequenceFile
SequenceFile格式: 每一个SequenceFile都包含一个“头”(header).Header包含了以下几部分. 1.SEQ三个字母的byte数组 2.Version number的by ...
- 运行Hadoop的示例程序WordCount-Running Hadoop Example
In the last post we've installed Hadoop 2.2.0 on Ubuntu. Now we'll see how to launch an example ma ...
- hadoop FileSystem类和SequenceFile类实例
Hadoop的FileSystem类是与Hadoop的某一文件系统进行交互的API,虽然我们主要聚焦于HDFS实例,但还是应该集成FileSystem抽象类,并编写代码,使其在不同的文件系统中可移植, ...
- Hadoop 写SequenceFile文件 源代码
package com.tdxx.hadoop.sequencefile; import java.io.IOException; import org.apache.hadoop.conf.Conf ...
- Hadoop Map/Reduce 示例程序WordCount
#进入hadoop安装目录 cd /usr/local/hadoop #创建示例文件:input #在里面输入以下内容: #Hello world, Bye world! vim input #在hd ...
- Hadoop小文件存储方案
原文地址:https://www.cnblogs.com/ballwql/p/8944025.html HDFS总体架构 在介绍文件存储方案之前,我觉得有必要先介绍下关于HDFS存储架构方面的一些知识 ...
随机推荐
- 2 引用 深copy 浅copy
1. is == 总结 is 是比较两个引用是否指向了同一个对象(引用比较). == 是比较两个对象是否相等. In [1]: a = [11,22,33] In [2]: b = [11,22,33 ...
- HDU 4418 Time travel
Time travel http://acm.hdu.edu.cn/showproblem.php?pid=4418 分析: 因为走到最后在折返,可以将区间复制一份,就变成了只往右走,01234321 ...
- FreeRTOS的信号量和互斥量
1. 理解如下,言简意赅的说,信号量解决同步,互斥量解决竞争. 信号量用于同步,主要任务间和中断间同步:互斥量用于互锁,用于保护同时只能有一个任务访问的资源,为资源上一把锁. 互斥量具有优先级继承,信 ...
- Manual install on Windows 7 with Apache and MySQL
These are instructions for installing on Windows 7 desktop (they may also be useful for a server ins ...
- 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)
题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...
- Qt-QML-Popup,弹层界面编写
随着接触Qt的时间的增加,也逐渐的发现了Qt 的一些不人信话的一些地方,不由的想起一句话,也不知道是在哪里看到的了“一切变成语言都是垃圾,就C++还可以凑合用”大致意思是这样.最近项目的祝界面框架都基 ...
- 使用python+selenium控制手工已打开的浏览器
我们可以利用Chrome DevTools协议.它允许客户检查和调试Chrome浏览器. 打开cmd,在命令行中输入命令: chrome.exe --remote-debugging-port=922 ...
- TPO-12 C2 A problem of the TA's payroll
TPO-12 C2 A problem of the TA's payroll payroll n. 工资单:在册职工人数:工资名单: paycheck n. 付薪水的支票,薪水 paperwork ...
- 第3章 TCP协议详解
第3章 TCP协议详解 3.1 TCP服务的特点 传输协议主要有两个:TCP协议和UDP协议,TCP协议相对于UDP协议的特点是 面向连接使用TCP协议通信的双方必须先建立连接,完成数据交换后,通信双 ...
- 关于maven项目中修改的JS不生效的解决方案
1. 问题描述 昨天下午博主在开发学习的过程中,碰到一个修改了JS无法生效的问题,折腾我不少的时间,现将百度到的解决方案总结一下,以便下次碰到类似问题能够最快的找到解决方案 2 解决方案 2.1 方案 ...