protected void verifySingleOperationIsSerializable(Operation operation) { // Checking that the operation implements Serializable is not enough since // it may contain a non-serializable non-transient field. Thus, we attempt // to serialize it and if there's an issue, we include the class name // of the offending operation in the error. Otherwise it can be time // consuming to track down which operation is causing problems. try { ObjectSerializer objectSerializer = HadoopUtil.instantiateSerializer(jobConf, operation.getClass()); try { objectSerializer.serialize(operation, true); } catch (ObjectStreamException e) { throw new RuntimeException("Could not serialize operation: " + operation.getClass().getCanonicalName(), e); } catch (IOException | NullPointerException e) { throw new RuntimeException("Error while trying to serialize: " + operation.getClass().getCanonicalName(), e); } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } }
public static <T> T deserializeBase64( String string, Configuration conf, Class<T> type, boolean decompress ) throws IOException { if( string == null || string.length() == 0 ) return null; ObjectSerializer objectSerializer; try { objectSerializer = instantiateSerializer( conf, type ); } catch( ClassNotFoundException exception ) { throw new IOException( exception ); } return objectSerializer.deserialize( decodeBytes( string ), type, decompress ); }
public static <T> ObjectSerializer instantiateSerializer( Configuration conf, Class<T> type ) throws ClassNotFoundException { Class<ObjectSerializer> flowSerializerClass; String serializerClassName = conf.get( ObjectSerializer.OBJECT_SERIALIZER_PROPERTY ); if( serializerClassName == null || serializerClassName.length() == 0 ) flowSerializerClass = (Class<ObjectSerializer>) DEFAULT_OBJECT_SERIALIZER; else flowSerializerClass = (Class<ObjectSerializer>) Class.forName( serializerClassName ); ObjectSerializer objectSerializer; try { objectSerializer = flowSerializerClass.newInstance(); if( objectSerializer instanceof Configurable ) ( (Configurable) objectSerializer ).setConf( conf ); } catch( Exception exception ) { exception.printStackTrace(); throw new IllegalArgumentException( "Unable to instantiate serializer \"" + flowSerializerClass.getName() + "\" for class: " + type.getName() ); } if( !objectSerializer.accepts( type ) ) throw new IllegalArgumentException( serializerClassName + " won't accept objects of class " + type.toString() ); return objectSerializer; }
public static <T> ObjectSerializer instantiateSerializer( Configuration conf, Class<T> type ) throws ClassNotFoundException { Class<ObjectSerializer> flowSerializerClass; String serializerClassName = conf.get( ObjectSerializer.OBJECT_SERIALIZER_PROPERTY ); if( serializerClassName == null || serializerClassName.length() == 0 ) flowSerializerClass = (Class<ObjectSerializer>) DEFAULT_OBJECT_SERIALIZER; else flowSerializerClass = (Class<ObjectSerializer>) Class.forName( serializerClassName ); ObjectSerializer objectSerializer; try { objectSerializer = flowSerializerClass.newInstance(); if( objectSerializer instanceof Configurable ) ( (Configurable) objectSerializer ).setConf( conf ); } catch( Exception exception ) { exception.printStackTrace(); throw new IllegalArgumentException( "Unable to instantiate serializer \"" + flowSerializerClass.getName() + "\" for class: " + type.getName() ); } if( !objectSerializer.accepts( type ) ) throw new IllegalArgumentException( serializerClassName + " won't accept objects of class " + type.toString() ); return objectSerializer; }
public static <T> String serializeBase64( T object, Configuration conf, boolean compress ) throws IOException { ObjectSerializer objectSerializer; try { objectSerializer = instantiateSerializer( conf, object.getClass() ); } catch( ClassNotFoundException exception ) { throw new IOException( exception ); } return encodeBytes( objectSerializer.serialize( object, compress ) ); }
public static <T> T deserializeBase64( String string, Configuration conf, Class<T> type, boolean decompress ) throws IOException { if( string == null || string.length() == 0 ) return null; ObjectSerializer objectSerializer; try { objectSerializer = instantiateSerializer( conf, type ); } catch( ClassNotFoundException exception ) { throw new IOException( exception ); } return objectSerializer.deserialize( decodeBytes( string ), type, decompress ); }
public static <T> String serializeBase64( T object, Configuration conf, boolean compress ) throws IOException { ObjectSerializer objectSerializer; try { objectSerializer = instantiateSerializer( conf, object.getClass() ); } catch( ClassNotFoundException exception ) { throw new IOException( exception ); } return encodeBytes( objectSerializer.serialize( object, compress ) ); }