Search in sources :

Example 1 with ComputerContext

use of com.baidu.hugegraph.computer.core.common.ComputerContext in project hugegraph-computer by hugegraph.

the class HugeGraphComputer method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    E.checkArgument(ArrayUtils.getLength(args) == 3, "Argument count must be three, " + "the first is conf path;" + "the second is role type;" + "the third is drive type.");
    String role = args[1];
    E.checkArgument(!StringUtils.isEmpty(role), "The role can't be null or emtpy, " + "it must be either '%s' or '%s'", ROLE_MASTER, ROLE_WORKER);
    setUncaughtExceptionHandler();
    loadClass();
    registerOptions();
    ComputerContext context = parseContext(args[0]);
    switch(role) {
        case ROLE_MASTER:
            executeMasterService(context);
            break;
        case ROLE_WORKER:
            executeWorkerService(context);
            break;
        default:
            throw new IllegalArgumentException(String.format("Unexpected role '%s'", role));
    }
}
Also used : ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext)

Example 2 with ComputerContext

use of com.baidu.hugegraph.computer.core.common.ComputerContext in project hugegraph-computer by hugegraph.

the class JsonStructGraphOutputTest 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", ComputerOptions.OUTPUT_RESULT_NAME, "rank");
    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.json";
    File file = new File(fileName);
    try {
        BufferedFileOutput dos = new BufferedFileOutput(file);
        StructGraphOutput output = (StructGraphOutput) IOFactory.createGraphOutput(context, OutputFormat.JSON, dos);
        output.writeVertex(vertex);
        dos.close();
        @SuppressWarnings("deprecation") String json = FileUtils.readFileToString(file);
        Assert.assertEquals("{\"id\":100,\"rank\":999}" + System.lineSeparator(), json);
    } 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 3 with ComputerContext

use of com.baidu.hugegraph.computer.core.common.ComputerContext in project hugegraph-computer by hugegraph.

the class JsonStructGraphOutputTest method testWriteReadVertexWithProperties.

@Test
public void testWriteReadVertexWithProperties() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "false", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "true", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false", ComputerOptions.OUTPUT_RESULT_NAME, "rank");
    ComputerContext context = context();
    GraphFactory factory = context.graphFactory();
    Id longId = BytesId.of(100L);
    IdListList idListList = new IdListList();
    IdList idList1 = new IdList();
    idList1.add(BytesId.of(66L));
    IdList idList2 = new IdList();
    idList2.add(BytesId.of(998L));
    idList2.add(BytesId.of(999L));
    idListList.add(idList1);
    idListList.add(idList2);
    Vertex vertex = factory.createVertex(longId, idListList);
    vertex.properties().put("boolean", new BooleanValue(true));
    vertex.properties().put("byte", new IntValue(127));
    vertex.properties().put("short", new IntValue(16383));
    vertex.properties().put("int", new IntValue(1000000));
    vertex.properties().put("long", new LongValue(10000000000L));
    vertex.properties().put("float", new FloatValue(0.1F));
    vertex.properties().put("double", new DoubleValue(-0.01D));
    vertex.properties().put("idvalue", longId);
    String fileName = "output.json";
    File file = new File(fileName);
    try {
        BufferedFileOutput dos = new BufferedFileOutput(file);
        StructGraphOutput output = (StructGraphOutput) IOFactory.createGraphOutput(context, OutputFormat.JSON, dos);
        output.writeVertex(vertex);
        dos.close();
        @SuppressWarnings("deprecation") String json = FileUtils.readFileToString(file);
        Assert.assertEquals("{\"id\":100,\"rank\":[[66],[998,999]]," + "\"properties\":{\"boolean\":true," + "\"byte\":127,\"double\":-0.01," + "\"short\":16383,\"idvalue\":100," + "\"float\":0.1,\"int\":1000000," + "\"long\":10000000000}}" + System.lineSeparator(), json);
    } finally {
        FileUtils.deleteQuietly(file);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) IdListList(com.baidu.hugegraph.computer.core.graph.value.IdListList) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) BooleanValue(com.baidu.hugegraph.computer.core.graph.value.BooleanValue) 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) FloatValue(com.baidu.hugegraph.computer.core.graph.value.FloatValue) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) File(java.io.File) Test(org.junit.Test)

Example 4 with ComputerContext

use of com.baidu.hugegraph.computer.core.common.ComputerContext in project hugegraph-computer by hugegraph.

the class StreamGraphOutputInputTest method testWriteReadEdgesWithSingleFrequency.

@Test
public void testWriteReadEdgesWithSingleFrequency() throws Exception {
    UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "SINGLE");
    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);
    }
}
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 5 with ComputerContext

use of com.baidu.hugegraph.computer.core.common.ComputerContext in project hugegraph-computer by hugegraph.

the class CsvStructGraphOutputTest method testWriteReadVertexWithEdges.

@Test
public void testWriteReadVertexWithEdges() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "true", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "false", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false");
    ComputerContext context = context();
    GraphFactory factory = context.graphFactory();
    Id longId = BytesId.of(100L);
    IdList idList = new IdList();
    idList.add(BytesId.of(998L));
    idList.add(BytesId.of(999L));
    Vertex vertex = factory.createVertex(longId, idList);
    vertex.addEdge(factory.createEdge("knows", BytesId.of(200)));
    vertex.addEdge(factory.createEdge("watch", "1111", BytesId.of(300)));
    String fileName = "output2.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,[998,999],[{200,\"knows\",\"\"}," + "{300,\"watch\",\"1111\"}]" + 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) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) File(java.io.File) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Aggregations

ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)10 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)9 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)9 Id (com.baidu.hugegraph.computer.core.graph.id.Id)9 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)9 Test (org.junit.Test)9 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)7 File (java.io.File)6 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)4 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)4 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)4 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)4 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)4 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)2 Value (com.baidu.hugegraph.computer.core.graph.value.Value)2