Search in sources :

Example 1 with VertexLabelEntity

use of com.baidu.hugegraph.entity.schema.VertexLabelEntity in project incubator-hugegraph-toolchain by apache.

the class LoadTaskService method buildVertexMappings.

private List<com.baidu.hugegraph.loader.mapping.VertexMapping> buildVertexMappings(GraphConnection connection, FileMapping fileMapping) {
    int connId = connection.getId();
    List<com.baidu.hugegraph.loader.mapping.VertexMapping> vMappings = new ArrayList<>();
    for (VertexMapping mapping : fileMapping.getVertexMappings()) {
        VertexLabelEntity vl = this.vlService.get(mapping.getLabel(), connId);
        List<String> idFields = mapping.getIdFields();
        Map<String, String> fieldMappings = mapping.fieldMappingToMap();
        com.baidu.hugegraph.loader.mapping.VertexMapping vMapping;
        if (vl.getIdStrategy().isCustomize()) {
            Ex.check(idFields.size() == 1, "When the ID strategy is CUSTOMIZED, you must " + "select a column in the file as the id");
            vMapping = new com.baidu.hugegraph.loader.mapping.VertexMapping(idFields.get(0), true);
        } else {
            assert vl.getIdStrategy().isPrimaryKey();
            List<String> primaryKeys = vl.getPrimaryKeys();
            Ex.check(idFields.size() >= 1 && idFields.size() == primaryKeys.size(), "When the ID strategy is PRIMARY_KEY, you must " + "select at least one column in the file as the " + "primary keys");
            /*
                 * The id column can be unfold into multi sub-ids only
                 * when primarykeys contains just one field
                 */
            boolean unfold = idFields.size() == 1;
            vMapping = new com.baidu.hugegraph.loader.mapping.VertexMapping(null, unfold);
            for (int i = 0; i < primaryKeys.size(); i++) {
                fieldMappings.put(idFields.get(i), primaryKeys.get(i));
            }
        }
        // set label
        vMapping.label(mapping.getLabel());
        // set field_mapping
        vMapping.mappingFields(fieldMappings);
        // set value_mapping
        vMapping.mappingValues(mapping.valueMappingToMap());
        // set selected
        vMapping.selectedFields().addAll(idFields);
        vMapping.selectedFields().addAll(fieldMappings.keySet());
        // set null_values
        Set<Object> nullValues = new HashSet<>();
        nullValues.addAll(mapping.getNullValues().getChecked());
        nullValues.addAll(mapping.getNullValues().getCustomized());
        vMapping.nullValues(nullValues);
        // TODO: Update strategies
        vMappings.add(vMapping);
    }
    return vMappings;
}
Also used : VertexMapping(com.baidu.hugegraph.entity.load.VertexMapping) ArrayList(java.util.ArrayList) VertexLabelEntity(com.baidu.hugegraph.entity.schema.VertexLabelEntity) HashSet(java.util.HashSet)

Example 2 with VertexLabelEntity

use of com.baidu.hugegraph.entity.schema.VertexLabelEntity 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 3 with VertexLabelEntity

use of com.baidu.hugegraph.entity.schema.VertexLabelEntity in project incubator-hugegraph-toolchain by apache.

the class VertexLabelService method checkConflict.

public void checkConflict(List<VertexLabelEntity> entities, ConflictDetail detail, int connId, boolean compareEachOther) {
    if (CollectionUtils.isEmpty(entities)) {
        return;
    }
    Map<String, VertexLabelEntity> originEntities = new HashMap<>();
    for (VertexLabelEntity entity : this.list(connId)) {
        originEntities.put(entity.getName(), entity);
    }
    for (VertexLabelEntity entity : entities) {
        if (detail.anyPropertyKeyConflict(entity.getPropNames())) {
            detail.add(entity, ConflictStatus.DEP_CONFLICT);
            continue;
        }
        if (detail.anyPropertyIndexConflict(entity.getIndexProps())) {
            detail.add(entity, ConflictStatus.DEP_CONFLICT);
            continue;
        }
        VertexLabelEntity originEntity = originEntities.get(entity.getName());
        ConflictStatus status = SchemaEntity.compare(entity, originEntity);
        detail.add(entity, status);
    }
    // Compare resued entities with each other
    if (compareEachOther) {
        compareWithEachOther(detail, SchemaType.VERTEX_LABEL);
    }
}
Also used : ConflictStatus(com.baidu.hugegraph.entity.schema.ConflictStatus) HashMap(java.util.HashMap) VertexLabelEntity(com.baidu.hugegraph.entity.schema.VertexLabelEntity)

Example 4 with VertexLabelEntity

use of com.baidu.hugegraph.entity.schema.VertexLabelEntity in project incubator-hugegraph-toolchain by apache.

the class VertexLabelService method list.

public List<VertexLabelEntity> list(Collection<String> names, int connId, boolean emptyAsAll) {
    HugeClient client = this.client(connId);
    List<VertexLabel> vertexLabels;
    if (CollectionUtils.isEmpty(names)) {
        if (emptyAsAll) {
            vertexLabels = client.schema().getVertexLabels();
        } else {
            vertexLabels = new ArrayList<>();
        }
    } else {
        vertexLabels = client.schema().getVertexLabels(new ArrayList<>(names));
    }
    List<IndexLabel> indexLabels = client.schema().getIndexLabels();
    List<VertexLabelEntity> results = new ArrayList<>(vertexLabels.size());
    vertexLabels.forEach(vertexLabel -> {
        results.add(join(vertexLabel, indexLabels));
    });
    return results;
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) IndexLabel(com.baidu.hugegraph.structure.schema.IndexLabel) ArrayList(java.util.ArrayList) VertexLabelEntity(com.baidu.hugegraph.entity.schema.VertexLabelEntity)

Example 5 with VertexLabelEntity

use of com.baidu.hugegraph.entity.schema.VertexLabelEntity in project incubator-hugegraph-toolchain by apache.

the class FileMappingController method checkEdgeMappingValid.

private void checkEdgeMappingValid(int connId, EdgeMapping edgeMapping, FileMapping fileMapping) {
    EdgeLabelEntity el = this.elService.get(edgeMapping.getLabel(), connId);
    VertexLabelEntity source = this.vlService.get(el.getSourceLabel(), connId);
    VertexLabelEntity target = this.vlService.get(el.getTargetLabel(), connId);
    Ex.check(!source.getIdStrategy().isAutomatic(), "load.file-mapping.vertex.automatic-id-unsupported");
    Ex.check(!target.getIdStrategy().isAutomatic(), "load.file-mapping.vertex.automatic-id-unsupported");
    Ex.check(!CollectionUtils.isEmpty(edgeMapping.getSourceFields()), "load.file-mapping.edge.source-fields-cannot-be-empty");
    Ex.check(!CollectionUtils.isEmpty(edgeMapping.getTargetFields()), "load.file-mapping.edge.target-fields-cannot-be-empty");
    FileSetting fileSetting = fileMapping.getFileSetting();
    List<String> columnNames = fileSetting.getColumnNames();
    Ex.check(columnNames.containsAll(edgeMapping.getSourceFields()), "load.file-mapping.edge.source-fields-should-in-column-names", edgeMapping.getSourceFields(), columnNames);
    Ex.check(columnNames.containsAll(edgeMapping.getTargetFields()), "load.file-mapping.edge.target-fields-should-in-column-names", edgeMapping.getTargetFields(), columnNames);
    Ex.check(CollectionUtil.allUnique(edgeMapping.fieldMappingToMap().values()), "load.file-mapping.mapping-fields-should-no-duplicate");
    this.checkMappingValid(edgeMapping, fileMapping);
}
Also used : EdgeLabelEntity(com.baidu.hugegraph.entity.schema.EdgeLabelEntity) FileSetting(com.baidu.hugegraph.entity.load.FileSetting) VertexLabelEntity(com.baidu.hugegraph.entity.schema.VertexLabelEntity)

Aggregations

VertexLabelEntity (com.baidu.hugegraph.entity.schema.VertexLabelEntity)13 EdgeLabelEntity (com.baidu.hugegraph.entity.schema.EdgeLabelEntity)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 HugeClient (com.baidu.hugegraph.driver.HugeClient)2 FileSetting (com.baidu.hugegraph.entity.load.FileSetting)2 IdStrategy (com.baidu.hugegraph.structure.constant.IdStrategy)2 Vertex (com.baidu.hugegraph.structure.graph.Vertex)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 GraphManager (com.baidu.hugegraph.driver.GraphManager)1 EdgeMapping (com.baidu.hugegraph.entity.load.EdgeMapping)1 VertexMapping (com.baidu.hugegraph.entity.load.VertexMapping)1 ConflictStatus (com.baidu.hugegraph.entity.schema.ConflictStatus)1 PropertyKeyEntity (com.baidu.hugegraph.entity.schema.PropertyKeyEntity)1 ExternalException (com.baidu.hugegraph.exception.ExternalException)1 IllegalGremlinException (com.baidu.hugegraph.exception.IllegalGremlinException)1 InternalException (com.baidu.hugegraph.exception.InternalException)1 ServerException (com.baidu.hugegraph.exception.ServerException)1 ClientException (com.baidu.hugegraph.rest.ClientException)1 Edge (com.baidu.hugegraph.structure.graph.Edge)1