source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/io/GenericWritable.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.6 KB
Line 
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
19package org.apache.hadoop.io;
20
21import java.io.DataInput;
22import java.io.DataOutput;
23import java.io.IOException;
24
25import org.apache.hadoop.conf.Configurable;
26import org.apache.hadoop.conf.Configuration;
27import org.apache.hadoop.util.ReflectionUtils;
28
29/**
30 * A wrapper for Writable instances.
31 * <p>
32 * When two sequence files, which have same Key type but different Value
33 * types, are mapped out to reduce, multiple Value types is not allowed.
34 * In this case, this class can help you wrap instances with different types.
35 * </p>
36 *
37 * <p>
38 * Compared with <code>ObjectWritable</code>, this class is much more effective,
39 * because <code>ObjectWritable</code> will append the class declaration as a String
40 * into the output file in every Key-Value pair.
41 * </p>
42 *
43 * <p>
44 * Generic Writable implements {@link Configurable} interface, so that it will be
45 * configured by the framework. The configuration is passed to the wrapped objects
46 * implementing {@link Configurable} interface <i>before deserialization</i>.
47 * </p>
48 *
49 * how to use it: <br>
50 * 1. Write your own class, such as GenericObject, which extends GenericWritable.<br>
51 * 2. Implements the abstract method <code>getTypes()</code>, defines
52 *    the classes which will be wrapped in GenericObject in application.
53 *    Attention: this classes defined in <code>getTypes()</code> method, must
54 *    implement <code>Writable</code> interface.
55 * <br><br>
56 *
57 * The code looks like this:
58 * <blockquote><pre>
59 * public class GenericObject extends GenericWritable {
60 *
61 *   private static Class[] CLASSES = {
62 *               ClassType1.class,
63 *               ClassType2.class,
64 *               ClassType3.class,
65 *               };
66 *
67 *   protected Class[] getTypes() {
68 *       return CLASSES;
69 *   }
70 *
71 * }
72 * </pre></blockquote>
73 *
74 * @since Nov 8, 2006
75 */
76public abstract class GenericWritable implements Writable, Configurable {
77
78  private static final byte NOT_SET = -1;
79
80  private byte type = NOT_SET;
81
82  private Writable instance;
83
84  private Configuration conf = null;
85 
86  /**
87   * Set the instance that is wrapped.
88   *
89   * @param obj
90   */
91  public void set(Writable obj) {
92    instance = obj;
93    Class<? extends Writable> instanceClazz = instance.getClass();
94    Class<? extends Writable>[] clazzes = getTypes();
95    for (int i = 0; i < clazzes.length; i++) {
96      Class<? extends Writable> clazz = clazzes[i];
97      if (clazz.equals(instanceClazz)) {
98        type = (byte) i;
99        return;
100      }
101    }
102    throw new RuntimeException("The type of instance is: "
103                               + instance.getClass() + ", which is NOT registered.");
104  }
105
106  /**
107   * Return the wrapped instance.
108   */
109  public Writable get() {
110    return instance;
111  }
112 
113  public String toString() {
114    return "GW[" + (instance != null ? ("class=" + instance.getClass().getName() +
115        ",value=" + instance.toString()) : "(null)") + "]";
116  }
117
118  public void readFields(DataInput in) throws IOException {
119    type = in.readByte();
120    Class<? extends Writable> clazz = getTypes()[type & 0xff];
121    try {
122      instance = ReflectionUtils.newInstance(clazz, conf);
123    } catch (Exception e) {
124      e.printStackTrace();
125      throw new IOException("Cannot initialize the class: " + clazz);
126    }
127    instance.readFields(in);
128  }
129
130  public void write(DataOutput out) throws IOException {
131    if (type == NOT_SET || instance == null)
132      throw new IOException("The GenericWritable has NOT been set correctly. type="
133                            + type + ", instance=" + instance);
134    out.writeByte(type);
135    instance.write(out);
136  }
137
138  /**
139   * Return all classes that may be wrapped.  Subclasses should implement this
140   * to return a constant array of classes.
141   */
142  abstract protected Class<? extends Writable>[] getTypes();
143
144  public Configuration getConf() {
145    return conf;
146  }
147
148  public void setConf(Configuration conf) {
149    this.conf = conf;
150  }
151 
152}
Note: See TracBrowser for help on using the repository browser.