本文主要是介绍MapFile 读例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.peidw;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.*;import java.io.IOException;
import java.net.URI;/*** mapfile 例子* Created by peidw on 2018-05-15.*/
public class MapFileWriteDemo {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"};public static void main(String[] args) throws IOException {String uri="hdfs://localhost:9000/mapfile";Configuration conf = new Configuration();FileSystem fs = FileSystem.get(URI.create(uri), conf);IntWritable key=new IntWritable();Text value=new Text();MapFile.Writer write=null;try{write =new MapFile.Writer( conf,fs,uri,key.getClass(),value.getClass());for(int i=0;i<1024;i++){key.set(i+1);value.set(DATA[i%DATA.length]);write.append(key,value);}}catch (Exception e){}finally {IOUtils.closeStream(write);}}}
运行成功后,查看hdfs的记录
这篇关于MapFile 读例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!