首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

hdfs例证之读写MapFile

2014-01-03 
hdfs例子之读写MapFilepackage mytestimport java.io.IOExceptionimport java.net.URIimport org.apach

hdfs例子之读写MapFile

package mytest;import java.io.IOException;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.io.IOUtils;import org.apache.hadoop.io.MapFile;import org.apache.hadoop.io.Text;public class MyMapFileTest {private static void write() throws IOException {Configuration conf = new Configuration();URI uri = URI.create("file:///home/yunpeng/mapFile.map");FileSystem fs = FileSystem.get(uri, conf);MapFile.Writer writer = null;writer = new MapFile.Writer(conf, fs, uri.getPath(), Text.class, Text.class);//通过writer向文档中写入记录  writer.append(new Text("key"), new Text("value"));IOUtils.closeStream(writer);//关闭write流  }private static void read() throws IOException {Configuration conf = new Configuration();URI uri = URI.create("file:///home/yunpeng/mapFile.map");FileSystem fs = FileSystem.get(uri, conf);MapFile.Reader reader = null;reader = new MapFile.Reader(fs, uri.getPath(), conf);//通过writer向文档中写入记录  Text key = new Text();Text value = new Text();while (reader.next(key, value)) {System.out.println(key);System.out.println(value);}IOUtils.closeStream(reader);//关闭write流  }public static void main(String[] args) throws IOException {read();}}

?

热点排行