Hadoop 写SequenceFile文件 源代码
2024-10-18 11:00:34
package com.tdxx.hadoop.sequencefile; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.Writer; public class SequenceFileStudy{ /**
* 写入到sequence file
*
* @param filePath
* @param conf
* @param datas
*/
public static void write2SequenceFile(String filePath, Configuration conf,
LongWritable key,LongWritable val) {
FileSystem fs = null;
Writer writer = null;
Path path = null;
//LongWritable idKey = new LongWritable(0); try {
fs = FileSystem.get(conf);
path = new Path(filePath);
writer = SequenceFile.createWriter(fs, conf, path,
LongWritable.class, LongWritable.class);
writer.append(key, val); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
IOUtils.closeStream(writer);
}
} private static Configuration getDefaultConf() {
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "local");
conf.set("fs.default.name", "file:///");
return conf;
} /**
* @param args
*/
public static void main(String[] args) {
String filePath = "data/longValue.sequence"; // 文件路径 // 生成数据
for (int i = 1; i <= 20; i++) {
// 写入到sequence file
LongWritable key = new LongWritable(i);
LongWritable value = new LongWritable(i+1);
write2SequenceFile(filePath, getDefaultConf(), key,value);
}
} }
最新文章
- C#开发中常用方法1------日期计算
- Atitit jsr规范有多少个 &#160;407个。Jsr规范大全
- Selenium学习笔记之外部化相关测试数据---xml
- 在MySQL中存储大文件
- 旋转V字俄罗斯方块
- 深度优先搜索 codevs 1031 质数环
- 【心得&;&;体会】
- Linux 环境下开机自启动Oracle服务
- Fuzzy test
- Android Training精要(五)讀取Bitmap對象實際的尺寸和類型
- c++相关知识回顾
- swift 实现漂亮的粒子效果CAEmitterLayer
- python socket理论知识
- 基于visual Studio2013解决C语言竞赛题之1036递归求值
- temp--达州银行
- 微信小程序AES加密解密
- 2、Libgdx配置你的开发环境(Eclipse,Intellij IDEA,NetBeans)
- Servlet中文件上传下载
- harbor pull 失败
- JODA-TIME获取本月的第一天及最后一天