hadoop学习5
来源:互联网 发布:cf外设软件 编辑:程序博客网 时间:2024/05/09 11:17
利用SequenceFile对文件进行读写
1.文件的写操作
import java.io.IOException;import java.net.URI;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.IntWritable;import org.apache.hadoop.io.SequenceFile;import org.apache.hadoop.io.Text;import org.apache.hadoop.io.SequenceFile.CompressionType;public class SequenceFileWriteDemo { private static String[] myValue = {"hello world","bye world","hello hadoop","bye world"}; public static void main(String[] args) throws IOException{ String uri = "hdfs://node1:9000/afan/output"; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri),conf); Path path = new Path(uri); IntWritable key = new IntWritable(); Text value = new Text(); SequenceFile.Writer writer = null; try{ //对文件不进行压缩// writer = SequenceFile.createWriter(fs,conf,path,key.getClass(),value.getClass());// //对文件进行压缩 writer = SequenceFile.createWriter(fs,conf,path,key.getClass(),value.getClass() ,CompressionType.BLOCK); for(int i = 0;i<50;i++){ key.set(50-i); value.set(myValue[i%myValue.length]); writer.append(key,value); } }finally{ IOUtils.closeStream(writer); } }}
2.文件的读操作
import java.io.IOException;import java.net.URI;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.SequenceFile;import org.apache.hadoop.io.Writable;import org.apache.hadoop.util.ReflectionUtils;public class SequenceFileReadFile { public static void main(String[] args) throws IOException{ String uri = "hdfs://node1:9000/afan/output"; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri),conf); Path path = new Path(uri); SequenceFile.Reader reader = null; try{ reader = new SequenceFile.Reader(fs,path, conf); Writable key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(), conf); Writable value =(Writable) ReflectionUtils.newInstance(reader.getValueClass(),conf); while(reader.next(key, value)){ System.out.println(key + "---"+ value); } }finally{ IOUtils.closeStream(reader); } }}
阅读全文
0 0
- Hadoop学习5
- Hadoop学习笔记(5)
- Hadoop学习笔记 5
- hadoop学习5
- Hadoop学习(5)-----hadoop 2.6的安装
- 【hadoop学习】Hadoop配置
- Hadoop学习-hadoop常用命令
- hadoop正式学习---hadoop
- hadoop学习5-HDFS API学习
- hadoop 学习
- Hadoop学习
- hadoop学习
- Hadoop学习
- hadoop学习
- Hadoop学习
- hadoop学习
- hadoop学习
- hadoop学习
- java注解
- 开门人和关门人
- stl版的各种模板
- 推荐五款在国内好评率比较好的扫地机人品牌旗舰店教你怎么选扫地机器人
- 纯前端实现人脸识别-提取-合成
- hadoop学习5
- 机器学习算法-DBSCAN聚类
- java基础语法难点(day01-day05)
- 文件操作指令笔记
- [RRM]RRM Common 理解
- 《JAVA编程思想》自动包装机制在泛型中的应用
- 这样的SQL不想改第二遍更不想再写第二遍
- 【前端知识点】前端事件监听、处理等解决方案详解
- 腾讯前端笔试题