ArrayWritable实例

来源:互联网 发布:网络优化推广的宣传语 编辑:程序博客网 时间:2024/05/16 11:59

概述

  • 作用
  • Array类构造示例
  • Array类使用示例
作用

  • 类似于java的动态数据ArrayList,可以在不知有多少数据量的情况下,动态向里面加数据

Array类构造示例

  • 数组里添加很多歌Text,类似于ArrayList<string>
  • public class CookieArray extends ArrayWritable{public CookieArray(){super(Text.class);}public String toString() {String[] abc = toStrings();StringBuffer sBuffer = new StringBuffer();for (String string : abc) {sBuffer.append(string + ",");}if (sBuffer.length()==0) {return "";}sBuffer.deleteCharAt(sBuffer.lastIndexOf(","));return sBuffer.toString();}}

Array类构造示例
  • Map端用其读取数据对象
  • public class logMapper extends Mapper<Text, CookieArray, xxx, yyy> {public void map(Text key, CookieArray value, Context context)throws IOException, InterruptedException {Writable[] loglist=value.get();for (Writable writable : loglist) {    Text temp=(Text)writable;    // do something using this temp}context.write(xxx, yyy);}}
  • reduce端用其聚合
  • public class logReducer extends Reducer<Text, Text, Text, CookieArray> {public void reduce(Text key,Iterable<Text> values, Context context)throws IOException, InterruptedException {CookieArray cookieArray = new CookieArray();List<Text> littleCollection = new ArrayList<Text>();for (Text text : values) {littleCollection.add(text);}cookieArray.set(littleCollection.toArray(new Text[0]));context.write(key, cookieArray);}}