Search in sources :

Example 36 with Vertex

use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.

the class StreamGraphOutputInputTest method testWriteReadEdgesWithSinglePerLabelFrequency.

@Test
public void testWriteReadEdgesWithSinglePerLabelFrequency() throws Exception {
    UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "SINGLE_PER_LABEL");
    ComputerContext context = ComputerContext.instance();
    GraphFactory graphFactory = context.graphFactory();
    Id longId = BytesId.of(100L);
    LongValue longValue = new LongValue(999L);
    Vertex vertex = graphFactory().createVertex(longId, longValue);
    vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
    vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
    vertex.addEdge(graphFactory.createEdge("watch", BytesId.of(3L)));
    vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
    vertex.addEdge(graphFactory.createEdge("watch", "2222", BytesId.of(4L)));
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        StreamGraphOutput output = newStreamGraphOutput(bao);
        output.writeEdges(vertex);
        bytes = bao.toByteArray();
        bytes = reweaveBytes(bytes);
    }
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        StreamGraphInput input = newStreamGraphInput(bai);
        assertEdgesEqual(vertex, input.readEdges(), EdgeFrequency.SINGLE_PER_LABEL);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Example 37 with Vertex

use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.

the class StreamGraphOutputInputTest method testWriteReadEdgesWithMultipleFrequency.

@Test
public void testWriteReadEdgesWithMultipleFrequency() throws Exception {
    UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "MULTIPLE");
    ComputerContext context = ComputerContext.instance();
    GraphFactory graphFactory = context.graphFactory();
    Id longId = BytesId.of(100L);
    LongValue longValue = new LongValue(999L);
    Vertex vertex = graphFactory().createVertex(longId, longValue);
    vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
    vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
    vertex.addEdge(graphFactory.createEdge("watch", BytesId.of(3L)));
    vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
    vertex.addEdge(graphFactory.createEdge("watch", "2222", BytesId.of(4L)));
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        StreamGraphOutput output = newStreamGraphOutput(bao);
        output.writeEdges(vertex);
        bytes = bao.toByteArray();
        bytes = reweaveBytes(bytes);
    }
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        StreamGraphInput input = newStreamGraphInput(bai);
        assertEdgesEqual(vertex, input.readEdges(), EdgeFrequency.MULTIPLE);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Example 38 with Vertex

use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.

the class CsvStructGraphOutputTest method testWriteReadVertexOnlyIdAndValue.

@Test
public void testWriteReadVertexOnlyIdAndValue() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "false", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "false", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false");
    ComputerContext context = context();
    GraphFactory factory = context.graphFactory();
    Id longId = BytesId.of(100L);
    Value value = BytesId.of(999L);
    Vertex vertex = factory.createVertex(longId, value);
    String fileName = "output.csv";
    File file = new File(fileName);
    try {
        BufferedFileOutput dos = new BufferedFileOutput(file);
        StructGraphOutput output = (StructGraphOutput) IOFactory.createGraphOutput(context, OutputFormat.CSV, dos);
        output.writeVertex(vertex);
        dos.close();
        @SuppressWarnings("deprecation") String text = FileUtils.readFileToString(file);
        Assert.assertEquals("100,999" + System.lineSeparator(), text);
    } finally {
        FileUtils.deleteQuietly(file);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) FloatValue(com.baidu.hugegraph.computer.core.graph.value.FloatValue) Value(com.baidu.hugegraph.computer.core.graph.value.Value) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) BooleanValue(com.baidu.hugegraph.computer.core.graph.value.BooleanValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) File(java.io.File) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Example 39 with Vertex

use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.

the class StreamGraphInput method readEdges.

@Override
public Vertex readEdges() throws IOException {
    Vertex vertex = this.graphFactory.createVertex();
    KvEntryReader reader = this.in.readEntry(in -> {
        // Read id
        vertex.id(readId(in));
    });
    if (this.frequency == EdgeFrequency.SINGLE) {
        while (reader.hasRemaining()) {
            Edge edge = this.graphFactory.createEdge();
            // Only use targetId as subKey, use properties as subValue
            reader.readSubKv(in -> {
                edge.targetId(readId(in));
            }, in -> {
                edge.properties(readProperties(in));
            });
            vertex.addEdge(edge);
        }
    } else if (this.frequency == EdgeFrequency.SINGLE_PER_LABEL) {
        while (reader.hasRemaining()) {
            Edge edge = this.graphFactory.createEdge();
            // Use label + targetId as subKey, use properties as subValue
            reader.readSubKv(in -> {
                edge.label(readLabel(in));
                edge.targetId(readId(in));
            }, in -> {
                edge.properties(readProperties(in));
            });
            vertex.addEdge(edge);
        }
    } else {
        assert this.frequency == EdgeFrequency.MULTIPLE;
        while (reader.hasRemaining()) {
            Edge edge = this.graphFactory.createEdge();
            /*
                 * Use label + sortValues + targetId as subKey,
                 * use properties as subValue
                 */
            reader.readSubKv(in -> {
                edge.label(readLabel(in));
                edge.name(readLabel(in));
                edge.targetId(readId(in));
            }, in -> {
                edge.properties(this.readProperties(in));
            });
            vertex.addEdge(edge);
        }
    }
    return vertex;
}
Also used : ComputerOptions(com.baidu.hugegraph.computer.core.config.ComputerOptions) Id(com.baidu.hugegraph.computer.core.graph.id.Id) EntryInput(com.baidu.hugegraph.computer.core.store.entry.EntryInput) EdgeFrequency(com.baidu.hugegraph.computer.core.config.EdgeFrequency) Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) IOException(java.io.IOException) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) Config(com.baidu.hugegraph.computer.core.config.Config) Value(com.baidu.hugegraph.computer.core.graph.value.Value) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) KvEntryReader(com.baidu.hugegraph.computer.core.store.entry.KvEntryReader) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) KvEntryReader(com.baidu.hugegraph.computer.core.store.entry.KvEntryReader) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge)

Example 40 with Vertex

use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.

the class StreamGraphInput method readVertex.

@Override
public Vertex readVertex() throws IOException {
    Vertex vertex = this.graphFactory.createVertex();
    this.in.readEntry(in -> {
        vertex.id(readId(in));
    }, in -> {
        vertex.label(readLabel(in));
        vertex.properties(readProperties(in));
    });
    return vertex;
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex)

Aggregations

Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)46 Test (org.junit.Test)21 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)18 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)16 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)15 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)15 Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)13 Id (com.baidu.hugegraph.computer.core.graph.id.Id)12 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)11 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)11 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)10 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)7 Value (com.baidu.hugegraph.computer.core.graph.value.Value)7 IOException (java.io.IOException)7 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)6 File (java.io.File)6 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)4 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)4 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)4 ComputerOptions (com.baidu.hugegraph.computer.core.config.ComputerOptions)2