Search in sources :

Example 1 with EdgeMapping

use of com.baidu.hugegraph.entity.load.EdgeMapping in project incubator-hugegraph-toolchain by apache.

the class LoadTaskService method buildEdgeMappings.

private List<com.baidu.hugegraph.loader.mapping.EdgeMapping> buildEdgeMappings(GraphConnection connection, FileMapping fileMapping) {
    int connId = connection.getId();
    List<com.baidu.hugegraph.loader.mapping.EdgeMapping> eMappings = new ArrayList<>();
    for (EdgeMapping mapping : fileMapping.getEdgeMappings()) {
        List<String> sourceFields = mapping.getSourceFields();
        List<String> targetFields = mapping.getTargetFields();
        EdgeLabelEntity el = this.elService.get(mapping.getLabel(), connId);
        VertexLabelEntity svl = this.vlService.get(el.getSourceLabel(), connId);
        VertexLabelEntity tvl = this.vlService.get(el.getTargetLabel(), connId);
        Map<String, String> fieldMappings = mapping.fieldMappingToMap();
        /*
             * When id strategy is customize or primaryKeys contains
             * just one field, the param 'unfold' can be true
             */
        boolean unfoldSource = true;
        if (svl.getIdStrategy().isPrimaryKey()) {
            List<String> primaryKeys = svl.getPrimaryKeys();
            Ex.check(sourceFields.size() >= 1 && sourceFields.size() == primaryKeys.size(), "When the source vertex ID strategy is CUSTOMIZED, " + "you must select at least one column in the file " + "as the id");
            for (int i = 0; i < primaryKeys.size(); i++) {
                fieldMappings.put(sourceFields.get(i), primaryKeys.get(i));
            }
            if (sourceFields.size() > 1) {
                unfoldSource = false;
            }
        }
        boolean unfoldTarget = true;
        if (tvl.getIdStrategy().isPrimaryKey()) {
            List<String> primaryKeys = tvl.getPrimaryKeys();
            Ex.check(targetFields.size() >= 1 && targetFields.size() == primaryKeys.size(), "When the target vertex ID strategy is CUSTOMIZED, " + "you must select at least one column in the file " + "as the id");
            for (int i = 0; i < primaryKeys.size(); i++) {
                fieldMappings.put(targetFields.get(i), primaryKeys.get(i));
            }
            if (targetFields.size() > 1) {
                unfoldTarget = false;
            }
        }
        com.baidu.hugegraph.loader.mapping.EdgeMapping eMapping;
        eMapping = new com.baidu.hugegraph.loader.mapping.EdgeMapping(sourceFields, unfoldSource, targetFields, unfoldTarget);
        // set label
        eMapping.label(mapping.getLabel());
        // set field_mapping
        eMapping.mappingFields(fieldMappings);
        // set value_mapping
        eMapping.mappingValues(mapping.valueMappingToMap());
        // set selected
        eMapping.selectedFields().addAll(sourceFields);
        eMapping.selectedFields().addAll(targetFields);
        eMapping.selectedFields().addAll(fieldMappings.keySet());
        // set null_values
        Set<Object> nullValues = new HashSet<>();
        nullValues.addAll(mapping.getNullValues().getChecked());
        nullValues.addAll(mapping.getNullValues().getCustomized());
        eMapping.nullValues(nullValues);
        eMappings.add(eMapping);
    }
    return eMappings;
}
Also used : EdgeLabelEntity(com.baidu.hugegraph.entity.schema.EdgeLabelEntity) ArrayList(java.util.ArrayList) EdgeMapping(com.baidu.hugegraph.entity.load.EdgeMapping) VertexLabelEntity(com.baidu.hugegraph.entity.schema.VertexLabelEntity) HashSet(java.util.HashSet)

Example 2 with EdgeMapping

use of com.baidu.hugegraph.entity.load.EdgeMapping in project incubator-hugegraph-toolchain by apache.

the class FileMappingController method updateEdgeMapping.

@PutMapping("{id}/edge-mappings/{emid}")
public FileMapping updateEdgeMapping(@PathVariable("connId") int connId, @PathVariable("id") int id, @PathVariable("emid") String emId, @RequestBody EdgeMapping newEntity) {
    FileMapping mapping = this.service.get(id);
    if (mapping == null) {
        throw new ExternalException("load.file-mapping.not-exist.id", id);
    }
    this.checkEdgeMappingValid(connId, newEntity, mapping);
    EdgeMapping edgeMapping = mapping.getEdgeMapping(emId);
    Ex.check(edgeMapping != null, "load.file-mapping.edge-mapping.not-exist.id", emId);
    newEntity.setId(emId);
    Set<EdgeMapping> edgeMappings = mapping.getEdgeMappings();
    edgeMappings.remove(edgeMapping);
    edgeMappings.add(newEntity);
    this.service.update(mapping);
    return mapping;
}
Also used : FileMapping(com.baidu.hugegraph.entity.load.FileMapping) EdgeMapping(com.baidu.hugegraph.entity.load.EdgeMapping) ExternalException(com.baidu.hugegraph.exception.ExternalException) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 3 with EdgeMapping

use of com.baidu.hugegraph.entity.load.EdgeMapping in project incubator-hugegraph-toolchain by apache.

the class FileMappingController method deleteEdgeMapping.

@DeleteMapping("{id}/edge-mappings/{emid}")
public FileMapping deleteEdgeMapping(@PathVariable("id") int id, @PathVariable("emid") String emid) {
    FileMapping mapping = this.service.get(id);
    if (mapping == null) {
        throw new ExternalException("load.file-mapping.not-exist.id", id);
    }
    EdgeMapping edgeMapping = mapping.getEdgeMapping(emid);
    boolean removed = mapping.getEdgeMappings().remove(edgeMapping);
    if (!removed) {
        throw new ExternalException("load.file-mapping.edge-mapping.not-exist.id", emid);
    }
    this.service.update(mapping);
    return mapping;
}
Also used : FileMapping(com.baidu.hugegraph.entity.load.FileMapping) EdgeMapping(com.baidu.hugegraph.entity.load.EdgeMapping) ExternalException(com.baidu.hugegraph.exception.ExternalException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Aggregations

EdgeMapping (com.baidu.hugegraph.entity.load.EdgeMapping)3 FileMapping (com.baidu.hugegraph.entity.load.FileMapping)2 ExternalException (com.baidu.hugegraph.exception.ExternalException)2 EdgeLabelEntity (com.baidu.hugegraph.entity.schema.EdgeLabelEntity)1 VertexLabelEntity (com.baidu.hugegraph.entity.schema.VertexLabelEntity)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1