HADOOP-7328. When a serializer class is missing, return null, not throw an NPE. Contributed by Harsh J Chouraria.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1167363 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fbf0035ee6
commit
592aaa1f06
@ -573,6 +573,9 @@ Release 0.23.0 - Unreleased
|
|||||||
HADOOP-7598. Fix smart-apply-patch.sh to handle patching from a sub
|
HADOOP-7598. Fix smart-apply-patch.sh to handle patching from a sub
|
||||||
directory correctly. (Robert Evans via acmurthy)
|
directory correctly. (Robert Evans via acmurthy)
|
||||||
|
|
||||||
|
HADOOP-7328. When a serializer class is missing, return null, not throw
|
||||||
|
an NPE. (Harsh J Chouraria via todd)
|
||||||
|
|
||||||
Release 0.22.0 - Unreleased
|
Release 0.22.0 - Unreleased
|
||||||
|
|
||||||
INCOMPATIBLE CHANGES
|
INCOMPATIBLE CHANGES
|
||||||
|
@ -27,10 +27,10 @@
|
|||||||
import org.apache.hadoop.classification.InterfaceStability;
|
import org.apache.hadoop.classification.InterfaceStability;
|
||||||
import org.apache.hadoop.conf.Configuration;
|
import org.apache.hadoop.conf.Configuration;
|
||||||
import org.apache.hadoop.conf.Configured;
|
import org.apache.hadoop.conf.Configured;
|
||||||
|
import org.apache.hadoop.fs.CommonConfigurationKeys;
|
||||||
import org.apache.hadoop.io.serializer.avro.AvroReflectSerialization;
|
import org.apache.hadoop.io.serializer.avro.AvroReflectSerialization;
|
||||||
import org.apache.hadoop.io.serializer.avro.AvroSpecificSerialization;
|
import org.apache.hadoop.io.serializer.avro.AvroSpecificSerialization;
|
||||||
import org.apache.hadoop.util.ReflectionUtils;
|
import org.apache.hadoop.util.ReflectionUtils;
|
||||||
import org.apache.hadoop.util.StringUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@ -50,14 +50,15 @@ public class SerializationFactory extends Configured {
|
|||||||
* <p>
|
* <p>
|
||||||
* Serializations are found by reading the <code>io.serializations</code>
|
* Serializations are found by reading the <code>io.serializations</code>
|
||||||
* property from <code>conf</code>, which is a comma-delimited list of
|
* property from <code>conf</code>, which is a comma-delimited list of
|
||||||
* classnames.
|
* classnames.
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public SerializationFactory(Configuration conf) {
|
public SerializationFactory(Configuration conf) {
|
||||||
super(conf);
|
super(conf);
|
||||||
for (String serializerName : conf.getStrings("io.serializations",
|
for (String serializerName : conf.getStrings(
|
||||||
new String[]{WritableSerialization.class.getName(),
|
CommonConfigurationKeys.IO_SERIALIZATIONS_KEY,
|
||||||
AvroSpecificSerialization.class.getName(),
|
new String[]{WritableSerialization.class.getName(),
|
||||||
|
AvroSpecificSerialization.class.getName(),
|
||||||
AvroReflectSerialization.class.getName()})) {
|
AvroReflectSerialization.class.getName()})) {
|
||||||
add(conf, serializerName);
|
add(conf, serializerName);
|
||||||
}
|
}
|
||||||
@ -67,27 +68,35 @@ public SerializationFactory(Configuration conf) {
|
|||||||
private void add(Configuration conf, String serializationName) {
|
private void add(Configuration conf, String serializationName) {
|
||||||
try {
|
try {
|
||||||
Class<? extends Serialization> serializionClass =
|
Class<? extends Serialization> serializionClass =
|
||||||
(Class<? extends Serialization>) conf.getClassByName(serializationName);
|
(Class<? extends Serialization>) conf.getClassByName(serializationName);
|
||||||
serializations.add((Serialization)
|
serializations.add((Serialization)
|
||||||
ReflectionUtils.newInstance(serializionClass, getConf()));
|
ReflectionUtils.newInstance(serializionClass, getConf()));
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
LOG.warn("Serialization class not found: ", e);
|
LOG.warn("Serialization class not found: ", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> Serializer<T> getSerializer(Class<T> c) {
|
public <T> Serializer<T> getSerializer(Class<T> c) {
|
||||||
return getSerialization(c).getSerializer(c);
|
Serialization<T> serializer = getSerialization(c);
|
||||||
|
if (serializer != null) {
|
||||||
|
return serializer.getSerializer(c);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> Deserializer<T> getDeserializer(Class<T> c) {
|
public <T> Deserializer<T> getDeserializer(Class<T> c) {
|
||||||
return getSerialization(c).getDeserializer(c);
|
Serialization<T> serializer = getSerialization(c);
|
||||||
|
if (serializer != null) {
|
||||||
|
return serializer.getDeserializer(c);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> Serialization<T> getSerialization(Class<T> c) {
|
public <T> Serialization<T> getSerialization(Class<T> c) {
|
||||||
for (Serialization serialization : serializations) {
|
for (Serialization serialization : serializations) {
|
||||||
if (serialization.accept(c)) {
|
if (serialization.accept(c)) {
|
||||||
return (Serialization<T>) serialization;
|
return (Serialization<T>) serialization;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.hadoop.io.serializer;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.apache.hadoop.conf.Configuration;
|
||||||
|
import org.apache.hadoop.io.Writable;
|
||||||
|
|
||||||
|
public class TestSerializationFactory {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSerializerAvailability() {
|
||||||
|
Configuration conf = new Configuration();
|
||||||
|
SerializationFactory factory = new SerializationFactory(conf);
|
||||||
|
// Test that a valid serializer class is returned when its present
|
||||||
|
assertNotNull("A valid class must be returned for default Writable Serde",
|
||||||
|
factory.getSerializer(Writable.class));
|
||||||
|
assertNotNull("A valid class must be returned for default Writable serDe",
|
||||||
|
factory.getDeserializer(Writable.class));
|
||||||
|
// Test that a null is returned when none can be found.
|
||||||
|
assertNull("A null should be returned if there are no serializers found.",
|
||||||
|
factory.getSerializer(TestSerializationFactory.class));
|
||||||
|
assertNull("A null should be returned if there are no deserializers found",
|
||||||
|
factory.getDeserializer(TestSerializationFactory.class));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user