Search in sources :

Example 1 with ListFormat

use of com.baidu.hugegraph.loader.source.file.ListFormat in project incubator-hugegraph-toolchain by apache.

the class GraphService method fillProperties.

private void fillProperties(int connId, SchemaLabelEntity schema, GraphElement element, Map<String, Object> properties) {
    HugeClient client = this.client(connId);
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        String key = entry.getKey();
        Object rawValue = entry.getValue();
        // Skip nullable property
        if (schema.getNullableProps().contains(key)) {
            if (rawValue instanceof String && StringUtils.isEmpty((String) rawValue)) {
                continue;
            }
        }
        PropertyKeyEntity pkEntity = this.pkService.get(key, connId);
        PropertyKey propertyKey = PropertyKeyService.convert(pkEntity, client);
        assert propertyKey != null;
        Object value;
        try {
            // DataTypeUtil.convert in loader need param InputSource
            FileSource source = new FileSource();
            ListFormat listFormat = new ListFormat("", "", ",");
            source.listFormat(listFormat);
            value = DataTypeUtil.convert(rawValue, propertyKey, source);
        } catch (IllegalArgumentException e) {
            throw new ExternalException("graph.property.convert.failed", e, key, rawValue);
        }
        element.property(key, value);
    }
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) FileSource(com.baidu.hugegraph.loader.source.file.FileSource) ListFormat(com.baidu.hugegraph.loader.source.file.ListFormat) ExternalException(com.baidu.hugegraph.exception.ExternalException) Map(java.util.Map) PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) PropertyKeyEntity(com.baidu.hugegraph.entity.schema.PropertyKeyEntity)

Example 2 with ListFormat

use of com.baidu.hugegraph.loader.source.file.ListFormat in project incubator-hugegraph-toolchain by apache.

the class DataTypeUtil method split.

private static List<Object> split(String key, String rawValue, InputSource source) {
    List<Object> valueColl = new ArrayList<>();
    if (rawValue.isEmpty()) {
        return valueColl;
    }
    E.checkState(AbstractSource.class.isAssignableFrom(source.getClass()), "Only accept AbstractSource when parse multi values, " + "but got '%s'", source.getClass().getName());
    ListFormat listFormat = ((AbstractSource) source).listFormat();
    E.checkArgumentNotNull(listFormat, "The list_format must be set when " + "parse list or set values");
    String startSymbol = listFormat.startSymbol();
    String endSymbol = listFormat.endSymbol();
    E.checkArgument(rawValue.length() >= startSymbol.length() + endSymbol.length(), "The value(key='%s') '%s' length(%s) must be >= " + "start symbol '%s' + end symbol '%s' length", key, rawValue, rawValue.length(), startSymbol, endSymbol);
    E.checkArgument(rawValue.startsWith(startSymbol) && rawValue.endsWith(endSymbol), "The value(key='%s') must start with '%s' and " + "end with '%s', but got '%s'", key, startSymbol, endSymbol, rawValue);
    rawValue = rawValue.substring(startSymbol.length(), rawValue.length() - endSymbol.length());
    String elemDelimiter = listFormat.elemDelimiter();
    Splitter.on(elemDelimiter).split(rawValue).forEach(value -> {
        if (!listFormat.ignoredElems().contains(value)) {
            valueColl.add(value);
        }
    });
    return valueColl;
}
Also used : ArrayList(java.util.ArrayList) AbstractSource(com.baidu.hugegraph.loader.source.AbstractSource) ListFormat(com.baidu.hugegraph.loader.source.file.ListFormat)

Aggregations

ListFormat (com.baidu.hugegraph.loader.source.file.ListFormat)2 HugeClient (com.baidu.hugegraph.driver.HugeClient)1 PropertyKeyEntity (com.baidu.hugegraph.entity.schema.PropertyKeyEntity)1 ExternalException (com.baidu.hugegraph.exception.ExternalException)1 AbstractSource (com.baidu.hugegraph.loader.source.AbstractSource)1 FileSource (com.baidu.hugegraph.loader.source.file.FileSource)1 PropertyKey (com.baidu.hugegraph.structure.schema.PropertyKey)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1