001 /*
002 * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package de.kumpe.hadooptimizer.hadoop;
017
018 import java.io.IOException;
019 import java.io.Serializable;
020
021 import org.apache.hadoop.conf.Configuration;
022 import org.apache.hadoop.fs.FileSystem;
023 import org.apache.hadoop.fs.Path;
024 import org.apache.hadoop.io.IntWritable;
025 import org.apache.hadoop.io.SequenceFile;
026 import org.apache.hadoop.io.SequenceFile.Reader;
027 import org.apache.hadoop.io.serializer.JavaSerialization;
028 import org.apache.hadoop.io.serializer.WritableSerialization;
029
030 /**
031 * Example to demonstrate a bug in JavaSerialization.
032 * <p>
033 * <b>This class is not part of HadoOptimizer and will be removed.</b>
034 *
035 * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
036 */
037 @Deprecated
038 @SuppressWarnings("serial")
039 public class DemonstrationForPossibleBugInJavaSerialization {
040 public static class MyValueClass implements Serializable {
041 String value = "something";
042
043 @Override
044 public String toString() {
045 return value;
046 }
047 }
048
049 public static void main(final String[] args) throws IOException {
050 final Configuration conf = new Configuration();
051 final IntWritable key = new IntWritable(123);
052 final MyValueClass value = new MyValueClass();
053
054 conf.setStrings("io.serializations",
055 WritableSerialization.class.getName(),
056 JavaSerialization.class.getName());
057
058 System.out.println("My ClassLoader"
059 + DemonstrationForPossibleBugInJavaSerialization.class
060 .getClassLoader());
061 System.out.println("JavaSerialization's ClassLoader: "
062 + JavaSerialization.class.getClassLoader());
063
064 final FileSystem fs = FileSystem.get(conf);
065 final Path path = new Path("/tmp/sequencefile.tmp");
066
067 final SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
068 path, IntWritable.class, MyValueClass.class);
069 writer.append(key, value);
070 writer.close();
071
072 final SequenceFile.Reader reader = new Reader(fs, path, conf);
073 if (reader.next(key)) {
074 System.out.println(reader.getCurrentValue(value));
075 }
076 reader.close();
077 }
078 }