ArrayList 和数组 在mapreduce编程中序列化

Home » hadoop-0.14.4 » org.apache » hadoop » io » [javadoc | source]
    看看这个源码,应该就很明白了
1   /** 2   * Licensed to the Apache Software Foundation (ASF) under one 3   * or more contributor license agreements. See the NOTICE file 4   * distributed with this work for additional information 5   * regarding copyright ownership. The ASF licenses this file 6   * to you under the Apache License, Version 2.0 (the 7   * "License"); you may not use this file except in compliance 8   * with the License. You may obtain a copy of the License at 9   * 10   * http://www.apache.org/licenses/LICENSE-2.0 11   * 12   * Unless required by applicable law or agreed to in writing, software 13   * distributed under the License is distributed on an "AS IS" BASIS, 14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15   * See the License for the specific language governing permissions and 16   * limitations under the License. 17   */ 18   19   package org.apache.hadoop.io; 20   21   import java.io; 22   import java.lang.reflect.Array; 23   24   /** A Writable for arrays containing instances of a class. */ 25   public class ArrayWritable implements Writable { 26   private Class valueClass; 27   private Writable[] values; 28   29   public ArrayWritable() { 30   this.valueClass = null; 31   } 32   33   public ArrayWritable(Class valueClass) { 34   this.valueClass = valueClass; 35   } 36   37   public ArrayWritable(Class valueClass, Writable[] values) { 38   this(valueClass); 39   this.values = values; 40   } 41   42   public ArrayWritable(String[] strings) { 43   this(UTF8.class, new Writable[strings.length]); 44   for (int i = 0; i < strings.length; i++) { 45   values[i] = new UTF8(strings[i]); 46   } 47   } 48   49   public void setValueClass(Class valueClass) { 50   if (valueClass != this.valueClass) { 51   this.valueClass = valueClass; 52   this.values = null; 53   } 54   } 55   56   public Class getValueClass() { 57   return valueClass; 58   } 59   60   public String[] toStrings() { 61   String[] strings = new String[values.length]; 62   for (int i = 0; i < values.length; i++) { 63   strings[i] = values[i].toString(); 64   } 65   return strings; 66   } 67   68   public Object toArray() { 69   Object result = Array.newInstance(valueClass, values.length); 70   for (int i = 0; i < values.length; i++) { 71   Array.set(result, i, values[i]); 72   } 73   return result; 74   } 75   76   public void set(Writable[] values) { this.values = values; } 77   78   public Writable[] get() { return values; } 79   80   public void readFields(DataInput in) throws IOException { 81   values = new Writable[in.readInt()]; // construct values 82   for (int i = 0; i < values.length; i++) { 83   Writable value = WritableFactories.newInstance(valueClass); 84   value.readFields(in); // read a value 85   values[i] = value; // store it in values 86   } 87   } 88   89   public void write(DataOutput out) throws IOException { 90   out.writeInt(values.length); // write values 91   for (int i = 0; i < values.length; i++) { 92   values[i].write(out); 93   } 94   } 95   96   } 97  
Home » hadoop-0.14.4 » org.apache » hadoop » io » [javadoc | source]
posted @ 2012-04-29 09:54  glose  阅读(977)  评论(0编辑  收藏  举报