Example usage for org.apache.hadoop.io Text readString

List of usage examples for org.apache.hadoop.io Text readString

Introduction

In this page you can find the example usage for org.apache.hadoop.io Text readString.

Prototype

public static String readString(DataInput in) throws IOException 

Source Link

Document

Read a UTF8 encoded string from in

Usage

From source file:org.sf.xrime.model.path.PathAsVertexesList.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/* w  w  w . j  a  va2s .co  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/*  ww  w.j  a va  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./* w ww . ja va 2 s .c o 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 2  s. c  o  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();// w  w  w  . j a v a  2 s .  co 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//from   w w  w . j a v  a2  s .  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();
        }
    }
}

From source file:org.sf.xrime.model.vertex.Vertex.java

License:Apache License

public void readFields(DataInput in) throws IOException {
    this.id = Text.readString(in);
}

From source file:org.shaf.core.io.emulator.SequenceWriterTest.java

License:Apache License

/**
 * Run the {@link SequenceWriter#writeRecord(Record) write} method test for
 * {@link SequenceWriter sequence writer}.
 * /*from   w  w  w.j a v a 2s  .  c o m*/
 * @throws Exception
 *             if the test fails for some reason.
 */
@Test
public void testWrite() throws Exception {
    /*
     * Writes data.
     */
    FileOutputFormat.setOutputPath(this.job, this.dir);
    this.job.setOutputKeyClass(Text.class);
    this.job.setOutputValueClass(Text.class);

    try (SequenceWriter writer = new SequenceWriter(this.job.getConfiguration());) {
        for (int i = 0; i < N; i++) {
            writer.writeRecord(new Record<Writable, Writable>(new Text("key-" + i), new Text("value-" + i)));
        }
    }

    /*
     * Reads data.
     */
    FileSystem fs = IOUtils.getLocalFileSystem();
    try (FSDataInputStream in = fs.open(this.file);) {

        String keyClassName = in.readUTF();
        assertEquals("org.apache.hadoop.io.Text", keyClassName);

        String valueClassName = in.readUTF();
        assertEquals("org.apache.hadoop.io.Text", valueClassName);

        int index = 0;
        for (int i = 0; i < N; i++) {
            String key = Text.readString(in);
            assertEquals("key-" + index, key);

            String value = Text.readString(in);
            assertEquals("value-" + index, value);

            ++index;
        }
    }
}

From source file:org.shaf.core.util.IOUtils.java

License:Apache License

/**
 * Reads an {@link Object} of the specified type from the {@link DataInput}.
 * /*from   w  ww.java2  s .  co m*/
 * @param cls
 *            the type of the reading object.
 * @param in
 *            the data input stream.
 * @return the read object.
 * @throws IOException
 *             if I/O error occurs.
 */
public static final Object readObject(Class<?> cls, DataInput in) throws IOException {
    try {
        if (cls == null) {
            throw new IOException("Reading class is not defined: null.");
        } else if (ClassUtils.isBoolean(cls)) {
            BooleanWritable obj = new BooleanWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isByte(cls)) {
            ByteWritable obj = new ByteWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isShort(cls)) {
            ShortWritable obj = new ShortWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isInteger(cls)) {
            IntWritable obj = new IntWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isLong(cls)) {
            LongWritable obj = new LongWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isFloat(cls)) {
            FloatWritable obj = new FloatWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isDouble(cls)) {
            DoubleWritable obj = new DoubleWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isString(cls)) {
            return Text.readString(in);
        } else if (ClassUtils.isEnum(cls)) {
            IntWritable obj = new IntWritable();
            obj.readFields(in);
            return cls.getEnumConstants()[obj.get()];
        } else if (ClassUtils.isArray(cls)) {
            int length = (int) readObject(int.class, in);
            Object array = Array.newInstance(cls.getComponentType(), length);
            for (int j = 0; j < length; j++) {
                Object a = readObject(cls.getComponentType(), in);
                Array.set(array, j, a);
            }
            return array;
        } else {
            Object obj = cls.newInstance();
            ((Writable) obj).readFields(in);
            return obj;
        }
    } catch (IllegalArgumentException | InstantiationException | IllegalAccessException exc) {
        throw new IOException(exc);
    }
}

From source file:org.shaf.core.util.IOUtilsTest.java

License:Apache License

/**
 * Test writing of {@code String} value.
 *///from w ww . j  a v a  2s  . c om
@Test
public void testWriteString() {
    byte[] buf = null;

    String value = "some text";
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(baos);) {
        IOUtils.writeObject(value, out);
        buf = baos.toByteArray();
    } catch (IOException exc) {
        fail(exc.getMessage());
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
            DataInputStream in = new DataInputStream(bais);) {
        assertEquals(value, Text.readString(in));
    } catch (IOException exc) {
        fail(exc.getMessage());
    }
}