List of usage examples for org.apache.hadoop.io WritableFactories newInstance
public static Writable newInstance(Class<? extends Writable> c, Configuration conf)
From source file:com.nebulousnews.io.ObjectSerializableWritable.java
License:Apache License
/** Read a {@link Writable}, {@link String}, primitive type, or an array of * the preceding. *///from www . j a va 2 s .co m @SuppressWarnings("unchecked") public static Object readObject(DataInput in, ObjectSerializableWritable objectWritable, Configuration conf) throws IOException { String className = UTF8.readString(in); Class<?> declaredClass = PRIMITIVE_NAMES.get(className); if (declaredClass == null) { try { declaredClass = conf.getClassByName(className); } catch (ClassNotFoundException e) { throw new RuntimeException("readObject can't find class " + className, e); } } Object instance; if (declaredClass.isPrimitive()) { // primitive types if (declaredClass == Boolean.TYPE) { // boolean instance = Boolean.valueOf(in.readBoolean()); } else if (declaredClass == Character.TYPE) { // char instance = Character.valueOf(in.readChar()); } else if (declaredClass == Byte.TYPE) { // byte instance = Byte.valueOf(in.readByte()); } else if (declaredClass == Short.TYPE) { // short instance = Short.valueOf(in.readShort()); } else if (declaredClass == Integer.TYPE) { // int instance = Integer.valueOf(in.readInt()); } else if (declaredClass == Long.TYPE) { // long instance = Long.valueOf(in.readLong()); } else if (declaredClass == Float.TYPE) { // float instance = Float.valueOf(in.readFloat()); } else if (declaredClass == Double.TYPE) { // double instance = Double.valueOf(in.readDouble()); } else if (declaredClass == Void.TYPE) { // void instance = null; } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isArray()) { // array int length = in.readInt(); instance = Array.newInstance(declaredClass.getComponentType(), length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } else if (declaredClass == String.class) { // String instance = UTF8.readString(in); } else if (declaredClass.isEnum()) { // enum instance = Enum.valueOf((Class<? extends Enum>) declaredClass, UTF8.readString(in)); } else if (Serializable.class.isAssignableFrom(declaredClass)) { //Serializable ObjectInputStream input = new ObjectInputStream((InputStream) in); try { instance = input.readObject(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block throw new IllegalArgumentException("ClassNotFound: " + declaredClass); } } else { // Writable (or fail, this is dangerous) Class instanceClass = null; String str = ""; try { str = UTF8.readString(in); instanceClass = conf.getClassByName(str); } catch (ClassNotFoundException e) { throw new RuntimeException("readObject can't find class " + str, e); } Writable writable = WritableFactories.newInstance(instanceClass, conf); writable.readFields(in); instance = writable; if (instanceClass == NullInstance.class) { // null declaredClass = ((NullInstance) instance).declaredClass; instance = null; } } if (objectWritable != null) { // store values objectWritable.declaredClass = declaredClass; objectWritable.instance = instance; } return instance; }
From source file:org.sf.xrime.model.edge.EdgeSet.java
License:Apache License
@SuppressWarnings("unchecked") @Override// w w w . j a va 2s.com public void readFields(DataInput in) throws IOException { // Clear the container. _edges.clear(); // Determine the size. int size = in.readInt(); if (size > 0) { // All edges in the set should have the same type. String className = Text.readString(in); try { Class instanceClass; instanceClass = Class.forName(className); for (int i = 0; i < size; i++) { Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); if (writable instanceof Edge) { addEdge((Edge) writable); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
From source file:org.sf.xrime.model.label.Labels.java
License:Apache License
@Override @SuppressWarnings("unchecked") public void readFields(DataInput in) throws IOException { // Clear the container. labels.clear();/* ww w .j a va2 s . c om*/ // Determine the size of container. int size = in.readInt(); while (size-- > 0) { // Each key is a string, but the label value may be of different types. String key = Text.readString(in); String valueClassName = Text.readString(in); try { Class instanceClass; instanceClass = Class.forName(valueClassName); Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); labels.put(key, writable); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new IOException(e.getMessage()); } } }
From source file:org.sf.xrime.model.path.PathAsVertexesList.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w w w . ja v a2 s . c o m public void readFields(DataInput in) throws IOException { // Clear the container. _vertexes.clear(); // Determine the size. int size = in.readInt(); if (size > 0) { // All vertexes in the set should have the same type. String className = Text.readString(in); try { Class instanceClass; instanceClass = Class.forName(className); for (int i = 0; i < size; i++) { Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); if (writable instanceof Vertex) { addVertex((Vertex) writable); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
From source file:org.sf.xrime.model.vertex.AdjBiSetVertex.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from www .java 2 s. c o m*/ public void readFields(DataInput in) throws IOException { // Call super first. super.readFields(in); // Clean containers. _forward_vertexes.clear(); _backward_vertexes.clear(); // Deal with forward vertex set. int size = in.readInt(); if (size > 0) { // Determine the type of elements. String className = Text.readString(in); try { Class instanceClass = Class.forName(className); for (int ii = 0; ii < size; ii++) { Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); if (writable instanceof AdjVertexEdge) { addForwardVertex((AdjVertexEdge) writable); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } // Deal with backward vertex set. size = in.readInt(); if (size > 0) { // Determine the type of elements. String className = Text.readString(in); Class instanceClass; try { instanceClass = Class.forName(className); for (int i = 0; i < size; i++) { Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); if (writable instanceof AdjVertexEdge) { addBackwardVertex((AdjVertexEdge) writable); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
From source file:org.sf.xrime.model.vertex.AdjSetVertex.java
License:Apache License
@SuppressWarnings("unchecked") public void readFields(DataInput in) throws IOException { // super./*from w w w . ja v a 2 s .co m*/ super.readFields(in); // Clear the container. opposites.clear(); // Determine container size. int size = in.readInt(); if (size > 0) { // Determine the element type. String className = Text.readString(in); try { for (int ii = 0; ii < size; ii++) { Class instanceClass = Class.forName(className); Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); if (writable instanceof AdjVertexEdge) { addOpposite((AdjVertexEdge) writable); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
From source file:org.sf.xrime.model.vertex.AdjSetVertexWithTwoHopLabel.java
License:Apache License
@SuppressWarnings("unchecked") public void readFields(DataInput in) throws IOException { // super.// w w w .j a v a 2s.co m super.readFields(in); //System.out.println("Invoking readFields() for De-serialization, node = "+this.id); // Clear the container. neighbors.clear(); allTwoHopNeighbors.clear(); if (this.id.equals("")) { this.id = null; } else { // Determine container size. int size = in.readInt(); //System.out.println("neighbors.size() = "+size); if (size > 0) { //Determine the element type, read neighbors String className = Text.readString(in); //System.out.println("ClassName = "+className); try { for (int ii = 0; ii < size; ii++) { Class instanceClass = Class.forName(className); Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); if (writable instanceof AdjVertexEdgeWithLabel) { this.addNeighbor((AdjVertexEdgeWithLabel) writable); //System.out.println("addNeighbor(): "+((AdjVertexEdgeWithLabel) writable).getOpposite()); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } // read two hop neighbors int mapSize = in.readInt(); //System.out.println("twoHopNeihgbor map size = " + mapSize); while (mapSize > 0) { String currentKey = Text.readString(in); int neighborSize = in.readInt(); if (neighborSize > 0) { String currentClassName = Text.readString(in); Set<AdjVertexEdgeWithLabel> currentTwoHopNeighbors = new TreeSet<AdjVertexEdgeWithLabel>( new AdjVertexEdgeWithLabelComparator()); try { for (int jj = 0; jj < neighborSize; jj++) { Class currentInstanceClass = Class.forName(currentClassName); Writable currentWritable = WritableFactories.newInstance(currentInstanceClass, null); currentWritable.readFields(in); if (currentWritable instanceof AdjVertexEdgeWithLabel) { currentTwoHopNeighbors.add((AdjVertexEdgeWithLabel) currentWritable); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } allTwoHopNeighbors.put(currentKey, currentTwoHopNeighbors); mapSize--; } } } } }
From source file:org.sf.xrime.model.vertex.AdjVertex.java
License:Apache License
@SuppressWarnings("unchecked") public void readFields(DataInput in) throws IOException { super.readFields(in); // Clear the container. edges.clear();//from w w w .j a va2 s . c o m // Determine the size. int size = in.readInt(); if (size > 0) { // Determine the element type. String className = Text.readString(in); try { for (int ii = 0; ii < size; ii++) { Class instanceClass = Class.forName(className); Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); if (writable instanceof Edge) { addEdge((Edge) writable); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
From source file:org.sf.xrime.model.vertex.SetOfVertexSets.java
License:Apache License
@SuppressWarnings("unchecked") @Override// w w w . j a va 2s. c o m public void readFields(DataInput in) throws IOException { // Clear the container. _the_set.clear(); // Determine the size. int size = in.readInt(); if (size > 0) { // All vertex sets in the set should have the same type. String className = Text.readString(in); try { Class instanceClass; instanceClass = Class.forName(className); for (int i = 0; i < size; i++) { Writable writable = WritableFactories.newInstance(instanceClass, null); writable.readFields(in); if (writable instanceof VertexSet) { addVertexSet((VertexSet) writable); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }