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);
}
} }

最新文章

  1. C#开发中常用方法1------日期计算
  2. Atitit jsr规范有多少个 &#160;407个。Jsr规范大全
  3. Selenium学习笔记之外部化相关测试数据---xml
  4. 在MySQL中存储大文件
  5. 旋转V字俄罗斯方块
  6. 深度优先搜索 codevs 1031 质数环
  7. 【心得&amp;&amp;体会】
  8. Linux 环境下开机自启动Oracle服务
  9. Fuzzy test
  10. Android Training精要(五)讀取Bitmap對象實際的尺寸和類型
  11. c++相关知识回顾
  12. swift 实现漂亮的粒子效果CAEmitterLayer
  13. python socket理论知识
  14. 基于visual Studio2013解决C语言竞赛题之1036递归求值
  15. temp--达州银行
  16. 微信小程序AES加密解密
  17. 2、Libgdx配置你的开发环境(Eclipse,Intellij IDEA,NetBeans)
  18. Servlet中文件上传下载
  19. harbor pull 失败
  20. JODA-TIME获取本月的第一天及最后一天

热门文章

  1. hdu4055 dp
  2. [Angular 2] Event in deep
  3. [RxJS] Getting Input Text with Map
  4. log4net使用具体解释
  5. Java学习笔记——JDBC之与数据库MySQL的连接以及增删改查等操作
  6. 原生 javascript 学习之 js变量
  7. css布局详解(二)——标准流布局(Nomal flow)
  8. (转)SQL利用Case When Then多条件判断
  9. Visual Studio在页面按F7不能跳转至cs代码页的解决方法
  10. SQL Server 向临时表插入数据