Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for YAML import specifications #9

Merged
merged 1 commit into from
Mar 4, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
This library provides a uniform configuration facade for tools running imports to Neo4j.
In particular, it offers:

- a user-friendly configuration surface, backed by a JSON schema
- a user-friendly configuration surface (in JSON or YAML), called import specification, backed by a JSON schema
- the Java equivalent of the import specification, a.k.a. `org.neo4j.importer.v1.ImportSpecification`
- validation plugins (soon)
- pre-processing plugins (soon)
Expand Down
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.16.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
Expand All @@ -54,6 +61,10 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
Expand Down Expand Up @@ -215,7 +226,6 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>licensing</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package org.neo4j.importer.v1;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import java.io.IOException;
import java.io.Reader;

public class ImportSpecificationDeserializer {

private static final JsonMapper MAPPER = JsonMapper.builder()
private static final YAMLMapper MAPPER = YAMLMapper.builder()
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.build();

Expand Down
26 changes: 17 additions & 9 deletions src/test/java/org/neo4j/importer/v1/e2e/AdminImportIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
import java.util.function.Function;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
Expand Down Expand Up @@ -75,7 +76,8 @@ public class AdminImportIT {

private static final String TARGET_DATABASE = "northwind";
private static final String JDBC_POSTGRES_URL =
"jdbc:tc:postgresql:15.5-alpine:///%s?TC_INITSCRIPT=e2e/northwind_pg_dump.sql".formatted(TARGET_DATABASE);
"jdbc:tc:postgresql:15.5-alpine:///%s?TC_INITSCRIPT=e2e/admin-import/northwind_pg_dump.sql"
.formatted(TARGET_DATABASE);

private Driver neo4jDriver;

Expand All @@ -95,9 +97,11 @@ void cleanUp() {
* This is **not** production-ready.
* This only serves as a proof that the spec format is descriptive enough to run neo4j-admin imports.
*/
@Test
void runs_import() throws Exception {
var importSpec = read("/e2e/spec.json", ImportSpecificationDeserializer::deserialize);
@ParameterizedTest
@ValueSource(strings = {"json", "yaml"})
void runs_import(String extension) throws Exception {
var importSpec =
read("/e2e/admin-import/spec.%s".formatted(extension), ImportSpecificationDeserializer::deserialize);
Map<String, Source> sources =
importSpec.getSources().stream().collect(toMap(Source::getName, Function.identity()));
File csvFolder = csvFolderPathFor("/e2e/admin-import");
Expand All @@ -118,26 +122,30 @@ void runs_import() throws Exception {
Neo4jAdmin.writeData(csvFolder, relationshipTarget, nodeTargets, SourceExecutor.read(source));
}
// note: custom query targets are ignored for now
var targetNeo4jDatabase = "%s-from-%s".formatted(TARGET_DATABASE, extension);

Neo4jAdmin.executeImport(NEO4J, neo4jDriver, importSpec, TARGET_DATABASE);
Neo4jAdmin.executeImport(NEO4J, neo4jDriver, importSpec, targetNeo4jDatabase);

var productCount = neo4jDriver
.executableQuery("MATCH (p:Product) RETURN count(p) AS count")
.withConfig(QueryConfig.builder().withDatabase(TARGET_DATABASE).build())
.withConfig(
QueryConfig.builder().withDatabase(targetNeo4jDatabase).build())
.execute()
.records();
assertThat(productCount).hasSize(1);
assertThat(productCount.getFirst().get("count").asLong()).isEqualTo(77L);
var categoryCount = neo4jDriver
.executableQuery("MATCH (c:Category) RETURN count(c) AS count")
.withConfig(QueryConfig.builder().withDatabase(TARGET_DATABASE).build())
.withConfig(
QueryConfig.builder().withDatabase(targetNeo4jDatabase).build())
.execute()
.records();
assertThat(categoryCount).hasSize(1);
assertThat(categoryCount.getFirst().get("count").asLong()).isEqualTo(8L);
var productInCategoryCount = neo4jDriver
.executableQuery("MATCH (:Product)-[btc:BELONGS_TO_CATEGORY]->(:Category) RETURN count(btc) AS count")
.withConfig(QueryConfig.builder().withDatabase(TARGET_DATABASE).build())
.withConfig(
QueryConfig.builder().withDatabase(targetNeo4jDatabase).build())
.execute()
.records();
assertThat(productInCategoryCount).hasSize(1);
Expand Down
Empty file.
51 changes: 51 additions & 0 deletions src/test/resources/e2e/admin-import/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
sources:
- name: "products"
type: "jdbc"
data_source: "northwind"
query: "SELECT productname, unitprice FROM products ORDER BY productname ASC"
- name: "categories"
type: "jdbc"
data_source: "northwind"
query: "SELECT categoryname, description FROM categories ORDER BY categoryname ASC"
- name: "products_in_categories"
type: "jdbc"
data_source: "northwind"
query: "SELECT p.productname AS productname, c.categoryname AS categoryname FROM products p NATURAL JOIN categories c ORDER BY p.productname ASC "
targets:
nodes:
- source: "products"
name: "product_nodes"
labels:
- "Product"
properties:
- source_field: "productname"
target_property: "name"
- source_field: "unitprice"
target_property: "unitPrice"
schema:
key_constraints:
- name: "name_key_constraint"
label: "Product"
properties:
- "name"
- source: "categories"
name: "category_nodes"
labels:
- "Category"
properties:
- source_field: "categoryname"
target_property: "name"
- source_field: "description"
target_property: "description"
schema:
key_constraints:
- name: "name_key_constraint"
label: "Category"
properties:
- "name"
relationships:
- source: "products_in_categories"
name: "product_in_category_relationships"
type: "BELONGS_TO_CATEGORY"
start_node_reference: "product_nodes"
end_node_reference: "category_nodes"