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

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

Introduction

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

Prototype

public static int writeString(DataOutput out, String s) throws IOException 

Source Link

Document

Write a UTF8 encoded string to out

Usage

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

License:Apache License

public void write(DataOutput out) throws IOException {
    // super./*from w w w .j  a  v a2  s .  com*/
    super.write(out);

    if (opposites == null) {
        out.writeInt(0);
        return;
    }
    // Size of the container.
    out.writeInt(opposites.size());
    if (opposites.size() > 0) {
        // All incidental edges should have the same type.
        Text.writeString(out, opposites.toArray()[0].getClass().getName());
        for (AdjVertexEdge sibling : opposites) {
            sibling.write(out);
        }
    }
}

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

License:Apache License

public void write(DataOutput out) throws IOException {
    //System.out.println("Invoking write() for Serialization, node = "+this.id);
    // super./*from  w  w  w.j ava  2s .c  o m*/
    if (this.id == null) {
        Text.writeString(out, "");
    } else {
        super.write(out);
        // serialize neighbors
        if (neighbors == null) {
            out.writeInt(0);
            return;
        }
        //System.out.println("neighbors.size() = "+neighbors.size());
        out.writeInt(neighbors.size());// Size of the container.
        if (neighbors.size() > 0) {
            // All incidental edges should have the same type.
            Text.writeString(out, neighbors.toArray()[0].getClass().getName());
            for (AdjVertexEdgeWithLabel sibling : neighbors) {
                //System.out.println("wirte neighbor: " + sibling.getOpposite());
                sibling.write(out);
            }
        }
        // serialize allTwoHopNeighbors
        //format:
        // |----------------------------------------------------------------|
        // | map size,                                          |
        // | key1, neighbor size, neighbor class, neigbor 1, neighbor 2, ...|
        // | key2, neighbor size, neighbor class, neigbor 3, neighbor 4, ...|
        // |----------------------------------------------------------------|
        if (allTwoHopNeighbors == null) {
            //System.out.println("allTwoHopNeighbors=0, out.write(0)");
            out.writeInt(0);
            return;
        }
        //System.out.println("allTwoHopNeighbors.size()="+allTwoHopNeighbors.size());
        out.writeInt(allTwoHopNeighbors.size());
        if (allTwoHopNeighbors.size() > 0) {
            for (String key : allTwoHopNeighbors.keySet()) {
                Text.writeString(out, key);
                Set<AdjVertexEdgeWithLabel> twoHopNeighbors = allTwoHopNeighbors.get(key);
                if (twoHopNeighbors == null) {
                    out.writeInt(0);
                    return;
                }
                out.writeInt(twoHopNeighbors.size());
                if (twoHopNeighbors.size() > 0) {
                    Text.writeString(out, twoHopNeighbors.toArray()[0].getClass().getName());
                    for (AdjVertexEdgeWithLabel sibling : twoHopNeighbors) {
                        sibling.write(out);
                    }
                }
            }
        }
    }

}

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

License:Apache License

public void write(DataOutput out) throws IOException {
    // super./*from  w  w w.  jav a 2  s  .  com*/
    super.write(out);

    if (edges == null) {
        out.writeInt(0);
        return;
    }
    // number of incidental edges.
    out.writeInt(edges.size());
    if (edges.size() > 0) {
        // All incidental edges should have the same type.
        Text.writeString(out, edges.get(0).getClass().getName());
        for (Edge edge : edges) {
            edge.write(out);
        }
    }
}

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

License:Apache License

@Override
public void write(DataOutput out) throws IOException {
    if (_the_set == null || _the_set.size() == 0) {
        out.writeInt(0);/* www.  ja  v a2s .c o m*/
    } else {
        // Write the number of vertex sets in this set.
        out.writeInt(_the_set.size());
        // All the vertex sets should have the same type.
        Text.writeString(out, _the_set.toArray()[0].getClass().getName());
        for (VertexSet vertex_set : _the_set) {
            vertex_set.write(out);
        }
    }
}

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

License:Apache License

public void write(DataOutput out) throws IOException {
    Text.writeString(out, id);
}

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

License:Apache License

@Override
public void write(DataOutput out) throws IOException {
    if (_vertexes == null || _vertexes.size() == 0) {
        out.writeInt(0);//  ww  w .ja  v  a2s .  co m
    } else {
        // Write the number of vertexes in this set.
        out.writeInt(_vertexes.size());
        // All the vertexes should have the same type.
        Text.writeString(out, _vertexes.toArray()[0].getClass().getName());
        for (Vertex vertex : _vertexes) {
            vertex.write(out);
        }
    }
}

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

License:Apache License

/**
 * Run the {@link SequenceReader#readRecord() read} method test for
 * {@link SequenceReader sequence reader}.
 * //from  www  .j  a va  2 s  .  co m
 * @throws Exception
 *             if the test fails for some reason.
 */
@Test
public void testRead() throws Exception {
    /*
     * Writes data.
     */
    FileSystem fs = IOUtils.getLocalFileSystem();
    try (FSDataOutputStream out = fs.create(super.file)) {
        out.writeUTF("org.apache.hadoop.io.Text");
        out.writeUTF("org.apache.hadoop.io.Text");

        for (int i = 0; i < N; i++) {
            Text.writeString(out, "key-" + i);
            Text.writeString(out, "line-" + i);
        }
    }

    /*
     * Reads data.
     */
    try (SequenceReader reader = new SequenceReader(this.job.getConfiguration());) {
        assertTrue(reader.getProgress() > 0f);

        float progress = 0f;
        long index = 0;
        Record<Writable, Writable> record = null;
        while ((record = reader.readRecord()) != null) {
            assertEquals("key-" + index, ((Text) record.getKey()).toString());
            assertEquals("line-" + index, ((Text) record.getValue()).toString());

            assertTrue(reader.getProgress() > progress);
            progress = reader.getProgress();

            ++index;
        }

        assertEquals(1f, reader.getProgress(), 0.001);
    }
}

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

License:Apache License

/**
 * Writes an {@link Object} to the {@link DataOutput}.
 * //from  w  w  w . j  a  v  a  2  s . c o  m
 * @param obj
 *            the object to write.
 * @param out
 *            the data output stream.
 * @throws IOException
 *             if I/O error occurs.
 */
public static final void writeObject(Object obj, DataOutput out) throws IOException {
    try {
        if (obj == null) {
            throw new IOException("Writing object is not defined: null.");
        } else if (ClassUtils.isBoolean(obj)) {
            (new BooleanWritable((boolean) obj)).write(out);
        } else if (ClassUtils.isByte(obj)) {
            (new ByteWritable((byte) obj)).write(out);
        } else if (ClassUtils.isShort(obj)) {
            (new ShortWritable((short) obj)).write(out);
        } else if (ClassUtils.isInteger(obj)) {
            (new IntWritable((int) obj)).write(out);
        } else if (ClassUtils.isLong(obj)) {
            (new LongWritable((long) obj)).write(out);
        } else if (ClassUtils.isFloat(obj)) {
            (new FloatWritable((float) obj)).write(out);
        } else if (ClassUtils.isDouble(obj)) {
            (new DoubleWritable((double) obj)).write(out);
        } else if (ClassUtils.isString(obj)) {
            Text.writeString(out, (String) obj);
        } else if (ClassUtils.isEnum(obj)) {
            (new IntWritable(((Enum<?>) obj).ordinal())).write(out);
        } else if (ClassUtils.isArray(obj)) {
            int length = Array.getLength(obj);
            writeObject(length, out);
            for (int j = 0; j < length; j++) {
                writeObject(Array.get(obj, j), out);
            }
        } else {
            ((Writable) obj).write(out);
        }
    } catch (IllegalArgumentException exc) {
        throw new IOException(exc);
    }
}

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

License:Apache License

/**
 * Test reading of {@code String} value.
 *//* w  w  w.  jav  a  2s  .  co m*/
@Test
public void testReadString() {
    byte[] buf = null;

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

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
            DataInputStream in = new DataInputStream(bais);) {
        assertEquals(value, (String) IOUtils.readObject(String.class, in));
    } catch (IOException exc) {
        fail(exc.getMessage());
    }
}

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

License:Apache License

/**
 * Test reading of {@code Array} value./*from   www  . ja v a  2 s . c om*/
 */
@Test
public void testReadArray() {
    byte[] buf = null;

    String[] array = { "h", "e", "l", "l", "o" };
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(baos);) {
        IntWritable len = new IntWritable(array.length);
        len.write(out);

        for (String value : array) {
            Text.writeString(out, value);
        }

        buf = baos.toByteArray();
    } catch (IOException exc) {
        fail(exc.getMessage());
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
            DataInputStream in = new DataInputStream(bais);) {
        assertArrayEquals(array, (String[]) IOUtils.readObject(String[].class, in));
    } catch (IOException exc) {
        fail(exc.getMessage());
    }
}