|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with this |
| 4 | + * work for additional information regarding copyright ownership. The ASF |
| 5 | + * licenses this file to You under the Apache License, Version 2.0 (the |
| 6 | + * "License"); you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hugegraph.computer.core.output.hg; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.apache.hugegraph.computer.core.config.ComputerOptions; |
| 24 | +import org.apache.hugegraph.computer.core.config.Config; |
| 25 | +import org.apache.hugegraph.computer.core.graph.vertex.Vertex; |
| 26 | +import org.apache.hugegraph.computer.core.output.AbstractComputerOutput; |
| 27 | +import org.apache.hugegraph.computer.core.output.hg.task.TaskManager; |
| 28 | +import org.apache.hugegraph.driver.HugeClient; |
| 29 | +import org.apache.hugegraph.structure.constant.WriteType; |
| 30 | +import org.apache.hugegraph.util.Log; |
| 31 | +import org.slf4j.Logger; |
| 32 | + |
| 33 | +public abstract class HugeGraphOutput<T> extends AbstractComputerOutput { |
| 34 | + |
| 35 | + private static final Logger LOG = Log.logger(HugeGraphOutput.class); |
| 36 | + |
| 37 | + private TaskManager taskManager; |
| 38 | + private List<org.apache.hugegraph.structure.graph.Vertex> localVertices; |
| 39 | + private int batchSize; |
| 40 | + private WriteType writeType; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void init(Config config, int partition) { |
| 44 | + super.init(config, partition); |
| 45 | + |
| 46 | + this.taskManager = new TaskManager(config); |
| 47 | + this.batchSize = config.get(ComputerOptions.OUTPUT_BATCH_SIZE); |
| 48 | + this.localVertices = new ArrayList<>(this.batchSize); |
| 49 | + this.writeType = WriteType.valueOf( |
| 50 | + config.get(ComputerOptions.OUTPUT_RESULT_WRITE_TYPE)); |
| 51 | + |
| 52 | + this.prepareSchema(); |
| 53 | + } |
| 54 | + |
| 55 | + public HugeClient client() { |
| 56 | + return this.taskManager.client(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void write(Vertex vertex) { |
| 61 | + this.localVertices.add(this.constructHugeVertex(vertex)); |
| 62 | + if (this.localVertices.size() >= this.batchSize) { |
| 63 | + this.commit(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void close() { |
| 69 | + if (!this.localVertices.isEmpty()) { |
| 70 | + this.commit(); |
| 71 | + } |
| 72 | + this.taskManager.waitFinished(); |
| 73 | + this.taskManager.shutdown(); |
| 74 | + LOG.info("End write back partition {}", this.partition()); |
| 75 | + } |
| 76 | + |
| 77 | + protected void commit() { |
| 78 | + this.taskManager.submitBatch(this.localVertices); |
| 79 | + LOG.info("Write back {} vertices", this.localVertices.size()); |
| 80 | + |
| 81 | + this.localVertices = new ArrayList<>(this.batchSize); |
| 82 | + } |
| 83 | + |
| 84 | + protected org.apache.hugegraph.structure.graph.Vertex constructHugeVertex( |
| 85 | + Vertex vertex) { |
| 86 | + org.apache.hugegraph.structure.graph.Vertex hugeVertex = |
| 87 | + new org.apache.hugegraph.structure.graph.Vertex(null); |
| 88 | + hugeVertex.id(vertex.id().asObject()); |
| 89 | + hugeVertex.property(this.name(), this.value(vertex)); |
| 90 | + return hugeVertex; |
| 91 | + } |
| 92 | + |
| 93 | + protected T value(Vertex vertex) { |
| 94 | + @SuppressWarnings("unchecked") |
| 95 | + T value = (T) vertex.value().value(); |
| 96 | + return value; |
| 97 | + } |
| 98 | + |
| 99 | + protected WriteType writeType() { |
| 100 | + return this.writeType; |
| 101 | + } |
| 102 | + |
| 103 | + protected abstract void prepareSchema(); |
| 104 | +} |
0 commit comments