Search in sources :

Example 1 with GraphElement

use of com.baidu.hugegraph.structure.GraphElement in project incubator-hugegraph-toolchain by apache.

the class ParseTaskBuilder method buildTask.

private ParseTask buildTask(ElementBuilder builder, List<Line> lines) {
    final LoadMetrics metrics = this.context.summary().metrics(this.struct);
    final int batchSize = this.context.options().batchSize;
    final ElementMapping mapping = builder.mapping();
    final boolean needRemoveId = builder instanceof VertexBuilder && ((VertexLabel) builder.schemaLabel()).idStrategy().isPrimaryKey();
    return new ParseTask(mapping, () -> {
        List<List<Record>> batches = new ArrayList<>();
        // One batch record
        List<Record> records = new ArrayList<>(batchSize);
        int count = 0;
        for (Line line : lines) {
            try {
                // NOTE: don't remove entry in keyValues
                @SuppressWarnings("unchecked") List<GraphElement> elements = builder.build(line.names(), line.values());
                E.checkState(elements.size() <= batchSize, "The number of columns in a line cannot " + "exceed the size of a batch, but got %s > %s", elements.size(), batchSize);
                // Prevent batch size from exceeding limit
                if (records.size() + elements.size() > batchSize) {
                    LOG.debug("Create a new batch for {}", mapping);
                    // Add current batch and create a new batch
                    batches.add(records);
                    records = new ArrayList<>(batchSize);
                }
                for (GraphElement element : elements) {
                    if (needRemoveId) {
                        ((Vertex) element).id(null);
                    }
                    records.add(new Record(line.rawLine(), element));
                    count++;
                }
            } catch (IllegalArgumentException e) {
                metrics.increaseParseFailure(mapping);
                ParseException pe = new ParseException(line.rawLine(), e);
                this.handleParseFailure(mapping, pe);
            }
        }
        if (!records.isEmpty()) {
            batches.add(records);
        }
        metrics.plusParseSuccess(mapping, count);
        return batches;
    });
}
Also used : ElementMapping(com.baidu.hugegraph.loader.mapping.ElementMapping) Vertex(com.baidu.hugegraph.structure.graph.Vertex) ArrayList(java.util.ArrayList) LoadMetrics(com.baidu.hugegraph.loader.metrics.LoadMetrics) Line(com.baidu.hugegraph.loader.reader.line.Line) VertexBuilder(com.baidu.hugegraph.loader.builder.VertexBuilder) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) GraphElement(com.baidu.hugegraph.structure.GraphElement) ArrayList(java.util.ArrayList) List(java.util.List) Record(com.baidu.hugegraph.loader.builder.Record) ParseException(com.baidu.hugegraph.loader.exception.ParseException)

Example 2 with GraphElement

use of com.baidu.hugegraph.structure.GraphElement in project incubator-hugegraph-toolchain by apache.

the class InsertTask method insertBatch.

@SuppressWarnings("unchecked")
protected void insertBatch(List<Record> batch, boolean checkVertex) {
    HugeClient client = this.context.client();
    List<GraphElement> elements = new ArrayList<>(batch.size());
    batch.forEach(r -> elements.add(r.element()));
    if (this.type().isVertex()) {
        client.graph().addVertices((List<Vertex>) (Object) elements);
    } else {
        client.graph().addEdges((List<Edge>) (Object) elements, checkVertex);
    }
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) GraphElement(com.baidu.hugegraph.structure.GraphElement) ArrayList(java.util.ArrayList) Edge(com.baidu.hugegraph.structure.graph.Edge)

Example 3 with GraphElement

use of com.baidu.hugegraph.structure.GraphElement in project incubator-hugegraph-toolchain by apache.

the class InsertTask method updateBatch.

@SuppressWarnings("unchecked")
protected void updateBatch(List<Record> batch, boolean checkVertex) {
    HugeClient client = this.context.client();
    List<GraphElement> elements = new ArrayList<>(batch.size());
    batch.forEach(r -> elements.add(r.element()));
    // CreateIfNotExist dose not support false now
    if (this.type().isVertex()) {
        BatchVertexRequest.Builder req = new BatchVertexRequest.Builder();
        req.vertices((List<Vertex>) (Object) elements).updatingStrategies(this.mapping.updateStrategies()).createIfNotExist(true);
        client.graph().updateVertices(req.build());
    } else {
        BatchEdgeRequest.Builder req = new BatchEdgeRequest.Builder();
        req.edges((List<Edge>) (Object) elements).updatingStrategies(this.mapping.updateStrategies()).checkVertex(checkVertex).createIfNotExist(true);
        client.graph().updateEdges(req.build());
    }
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) BatchVertexRequest(com.baidu.hugegraph.structure.graph.BatchVertexRequest) BatchEdgeRequest(com.baidu.hugegraph.structure.graph.BatchEdgeRequest) GraphElement(com.baidu.hugegraph.structure.GraphElement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with GraphElement

use of com.baidu.hugegraph.structure.GraphElement in project incubator-hugegraph-toolchain by apache.

the class BackupRestoreBaseManager method writeText.

protected long writeText(String path, HugeType type, List<?> list, boolean compress, String label, boolean allProperties, List<String> properties) {
    OutputStream os = this.outputStream(path, compress);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(LBUF_SIZE);
    StringBuilder builder = new StringBuilder(LBUF_SIZE);
    long count = 0L;
    try {
        for (Object e : list) {
            GraphElement element = (GraphElement) e;
            if (label != null && !label.equals(element.label())) {
                continue;
            }
            count++;
            if (type == HugeType.VERTEX) {
                builder.append(element.id()).append("\t");
            } else {
                Edge edge = (Edge) e;
                builder.append(edge.sourceId()).append("\t").append(edge.targetId()).append("\t");
            }
            if (allProperties) {
                for (Object value : element.properties().values()) {
                    builder.append(value).append(",");
                }
            } else {
                for (String property : properties) {
                    builder.append(element.property(property)).append(",");
                }
            }
            builder.setCharAt(builder.length() - 1, '\n');
        }
        baos.write(builder.toString().getBytes(API.CHARSET));
        os.write(baos.toByteArray());
    } catch (Throwable e) {
        throw new ToolsException("Failed to serialize %s to %s", e, type, path);
    }
    return count;
}
Also used : ToolsException(com.baidu.hugegraph.exception.ToolsException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GraphElement(com.baidu.hugegraph.structure.GraphElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Edge(com.baidu.hugegraph.structure.graph.Edge)

Aggregations

GraphElement (com.baidu.hugegraph.structure.GraphElement)4 Vertex (com.baidu.hugegraph.structure.graph.Vertex)3 ArrayList (java.util.ArrayList)3 HugeClient (com.baidu.hugegraph.driver.HugeClient)2 Edge (com.baidu.hugegraph.structure.graph.Edge)2 List (java.util.List)2 ToolsException (com.baidu.hugegraph.exception.ToolsException)1 Record (com.baidu.hugegraph.loader.builder.Record)1 VertexBuilder (com.baidu.hugegraph.loader.builder.VertexBuilder)1 ParseException (com.baidu.hugegraph.loader.exception.ParseException)1 ElementMapping (com.baidu.hugegraph.loader.mapping.ElementMapping)1 LoadMetrics (com.baidu.hugegraph.loader.metrics.LoadMetrics)1 Line (com.baidu.hugegraph.loader.reader.line.Line)1 BatchEdgeRequest (com.baidu.hugegraph.structure.graph.BatchEdgeRequest)1 BatchVertexRequest (com.baidu.hugegraph.structure.graph.BatchVertexRequest)1 VertexLabel (com.baidu.hugegraph.structure.schema.VertexLabel)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1