Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ private void assignIdIfNeed(Vertex vertex, Map<String, Object> keyValues) {
"The value of id field '%s' can't be null",
this.source.idField());

String id = String.valueOf(idValue);
if (this.vertexLabel.idStrategy() == IdStrategy.CUSTOMIZE_STRING) {
String id = String.valueOf(idValue);
this.checkVertexIdLength(id);
vertex.id(id);
} else {
Long id = parseNumberId(idValue);
vertex.id(id);
}
} else {
assert isPrimaryKey(this.vertexLabel.idStrategy());
Expand Down Expand Up @@ -113,4 +116,16 @@ private void checkIdField() {
"Unsupported AUTOMATIC id strategy for hugegraph-loader");
}
}

private static long parseNumberId(Object idValue) {
if (idValue instanceof Number) {
return ((Number) idValue).longValue();
} else if (idValue instanceof String) {
return Long.parseLong((String) idValue);
} else {
throw new IllegalArgumentException(String.format(
"The id value must can be casted to Long, " +
"but got %s(%s)", idValue, idValue.getClass().getName()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,36 @@ public void testLoadWithValueSetPorpertyInJsonFile() {
FileUtil.delete(path("edge_use.json"));
}

@Test
public void testLoadVerticesWithCustomizedNumberId() {
String line = FileUtil.newCSVLine(1, "marko", 29, "Beijing");
FileUtil.append(path("vertex_person_number_id.csv"), line);

String[] args = new String[]{"-f", path("struct_number_id.json"),
"-g", "hugegraph",
"-s", path("schema_number_id.groovy"),
"--test-mode", "true"};

try {
HugeGraphLoader.main(args);
} catch (Exception e) {
FileUtil.delete(path("vertex_person_number_id.csv"));
Assert.fail("Should not throw exception, but throw " + e);
}

List<Vertex> vertices = client.graph().listVertices();
Assert.assertEquals(1, vertices.size());
Vertex vertex = vertices.get(0);

Assert.assertEquals(1, vertex.id());
Assert.assertEquals("person", vertex.label());
Assert.assertEquals("marko", vertex.property("name"));
Assert.assertEquals(29, vertex.property("age"));
Assert.assertEquals("Beijing", vertex.property("city"));

FileUtil.delete(path("vertex_person_number_id.csv"));
}

private static String path(String fileName) {
return Paths.get(PATH_PREFIX, fileName).toString();
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/schema_number_id.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Define schema
schema.propertyKey("name").asText().ifNotExist().create();
schema.propertyKey("age").asInt().ifNotExist().create();
schema.propertyKey("city").asText().ifNotExist().create();

schema.vertexLabel("person").properties("name", "age", "city").useCustomizeNumberId().ifNotExist().create();

schema.indexLabel("personByName").onV("person").by("name").secondary().ifNotExist().create();
schema.indexLabel("personByAge").onV("person").by("age").range().ifNotExist().create();
schema.indexLabel("personByCity").onV("person").by("city").secondary().ifNotExist().create();
schema.indexLabel("personByAgeAndCity").onV("person").by("age", "city").secondary().ifNotExist().create();
20 changes: 20 additions & 0 deletions src/test/resources/struct_number_id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"vertices": [
{
"label": "person",
"input": {
"type": "file",
"path": "src/test/resources/vertex_person_number_id.csv",
"format": "CSV",
"header": [
"id",
"name",
"age",
"city"
],
"charset": "UTF-8"
},
"id": "id"
}
]
}