Search in sources :

Example 16 with Input

use of org.neo4j.unsafe.impl.batchimport.input.Input in project neo4j by neo4j.

the class CsvInputTest method shouldHandleMultipleInputGroups.

@Test
public void shouldHandleMultipleInputGroups() throws Exception {
    // GIVEN multiple input groups, each with their own, specific, header
    DataFactory<InputNode> group1 = data(":ID,name,kills:int,health:int\n" + "1,Jim,10,100\n" + "2,Abathur,0,200\n");
    DataFactory<InputNode> group2 = data(":ID,type\n" + "3,zergling\n" + "4,csv\n");
    Iterable<DataFactory<InputNode>> data = dataIterable(group1, group2);
    Input input = new CsvInput(data, defaultFormatNodeFileHeader(), null, null, IdType.STRING, config(COMMAS), silentBadCollector(0), getRuntime().availableProcessors());
    // WHEN iterating over them, THEN the expected data should come out
    try (InputIterator<InputNode> nodes = input.nodes().iterator()) {
        assertNode(nodes.next(), "1", properties("name", "Jim", "kills", 10, "health", 100), labels());
        assertNode(nodes.next(), "2", properties("name", "Abathur", "kills", 0, "health", 200), labels());
        assertNode(nodes.next(), "3", properties("type", "zergling"), labels());
        assertNode(nodes.next(), "4", properties("type", "csv"), labels());
        assertFalse(nodes.hasNext());
    }
}
Also used : InputNode(org.neo4j.unsafe.impl.batchimport.input.InputNode) Input(org.neo4j.unsafe.impl.batchimport.input.Input) Test(org.junit.Test)

Example 17 with Input

use of org.neo4j.unsafe.impl.batchimport.input.Input in project neo4j by neo4j.

the class CsvInputTest method shouldProvideDefaultRelationshipType.

@Test
public void shouldProvideDefaultRelationshipType() throws Exception {
    // GIVEN
    String defaultType = "DEFAULT";
    String customType = "CUSTOM";
    DataFactory<InputRelationship> data = data(":START_ID,:END_ID,:TYPE\n" + "0,1,\n" + "1,2," + customType + "\n" + "2,1," + defaultType, defaultRelationshipType(defaultType));
    Iterable<DataFactory<InputRelationship>> dataIterable = dataIterable(data);
    Input input = new CsvInput(null, null, dataIterable, defaultFormatRelationshipFileHeader(), IdType.ACTUAL, config(COMMAS), silentBadCollector(0), getRuntime().availableProcessors());
    // WHEN/THEN
    try (ResourceIterator<InputRelationship> relationships = input.relationships().iterator()) {
        assertRelationship(relationships.next(), 0L, 1L, defaultType, NO_PROPERTIES);
        assertRelationship(relationships.next(), 1L, 2L, customType, NO_PROPERTIES);
        assertRelationship(relationships.next(), 2L, 1L, defaultType, NO_PROPERTIES);
        assertFalse(relationships.hasNext());
    }
}
Also used : Input(org.neo4j.unsafe.impl.batchimport.input.Input) InputRelationship(org.neo4j.unsafe.impl.batchimport.input.InputRelationship) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 18 with Input

use of org.neo4j.unsafe.impl.batchimport.input.Input in project neo4j by neo4j.

the class CsvInputTest method shouldFailOnRelationshipWithMissingStartIdField.

@Test
public void shouldFailOnRelationshipWithMissingStartIdField() throws Exception {
    // GIVEN
    Iterable<DataFactory<InputRelationship>> data = relationshipData(CsvInputTest.<InputRelationship>data(":START_ID,:END_ID,:TYPE\n" + ",1,"));
    Input input = new CsvInput(null, null, data, defaultFormatRelationshipFileHeader(), IdType.INTEGER, config(COMMAS), silentBadCollector(0), getRuntime().availableProcessors());
    // WHEN
    try (InputIterator<InputRelationship> relationships = input.relationships().iterator()) {
        relationships.next();
        fail("Should have failed");
    } catch (InputException e) {
        // THEN good
        assertThat(e.getMessage(), containsString(Type.START_ID.name()));
    }
}
Also used : Input(org.neo4j.unsafe.impl.batchimport.input.Input) InputException(org.neo4j.unsafe.impl.batchimport.input.InputException) InputRelationship(org.neo4j.unsafe.impl.batchimport.input.InputRelationship) Test(org.junit.Test)

Example 19 with Input

use of org.neo4j.unsafe.impl.batchimport.input.Input in project neo4j by neo4j.

the class CsvInputTest method shouldHaveIdSetAsPropertyIfIdHeaderEntryIsNamed.

@Test
public void shouldHaveIdSetAsPropertyIfIdHeaderEntryIsNamed() throws Exception {
    // GIVEN
    DataFactory<InputNode> data = data("myId:ID,name:string,level:int\n" + "abc,Mattias,1\n" + // this node is anonymous
    "def,Johan,2\n");
    Iterable<DataFactory<InputNode>> dataIterable = dataIterable(data);
    Input input = new CsvInput(dataIterable, defaultFormatNodeFileHeader(), null, null, IdType.STRING, config(COMMAS), silentBadCollector(0), getRuntime().availableProcessors());
    // WHEN
    try (ResourceIterator<InputNode> nodes = input.nodes().iterator()) {
        // THEN
        assertNode(nodes.next(), "abc", new Object[] { "myId", "abc", "name", "Mattias", "level", 1 }, labels());
        assertNode(nodes.next(), "def", new Object[] { "myId", "def", "name", "Johan", "level", 2 }, labels());
        assertFalse(nodes.hasNext());
    }
}
Also used : InputNode(org.neo4j.unsafe.impl.batchimport.input.InputNode) Input(org.neo4j.unsafe.impl.batchimport.input.Input) Test(org.junit.Test)

Example 20 with Input

use of org.neo4j.unsafe.impl.batchimport.input.Input in project neo4j by neo4j.

the class CsvInputTest method shouldTreatEmptyQuotedStringsAsNullIfConfiguredTo.

@Test
public void shouldTreatEmptyQuotedStringsAsNullIfConfiguredTo() throws Exception {
    // GIVEN
    Iterable<DataFactory<InputNode>> data = DataFactories.nodeData(CsvInputTest.<InputNode>data(":ID,one,two,three\n" + "1,\"\",,value"));
    Configuration config = config(new Configuration.Overridden(COMMAS) {

        @Override
        public boolean emptyQuotedStringsAsNull() {
            return true;
        }
    });
    Input input = new CsvInput(data, defaultFormatNodeFileHeader(), null, null, IdType.INTEGER, config, silentBadCollector(0), getRuntime().availableProcessors());
    // WHEN
    try (InputIterator<InputNode> nodes = input.nodes().iterator()) {
        InputNode node = nodes.next();
        // THEN
        assertNode(node, 1L, properties("three", "value"), labels());
        assertFalse(nodes.hasNext());
    }
}
Also used : InputNode(org.neo4j.unsafe.impl.batchimport.input.InputNode) Input(org.neo4j.unsafe.impl.batchimport.input.Input) Test(org.junit.Test)

Aggregations

Input (org.neo4j.unsafe.impl.batchimport.input.Input)31 Test (org.junit.Test)30 InputNode (org.neo4j.unsafe.impl.batchimport.input.InputNode)21 InputRelationship (org.neo4j.unsafe.impl.batchimport.input.InputRelationship)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 Matchers.anyString (org.mockito.Matchers.anyString)6 InputException (org.neo4j.unsafe.impl.batchimport.input.InputException)4 Collector (org.neo4j.unsafe.impl.batchimport.input.Collector)3 BadCollector (org.neo4j.unsafe.impl.batchimport.input.BadCollector)2 Group (org.neo4j.unsafe.impl.batchimport.input.Group)2 Groups (org.neo4j.unsafe.impl.batchimport.input.Groups)2 CsvInput (org.neo4j.unsafe.impl.batchimport.input.csv.CsvInput)2 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Charset (java.nio.charset.Charset)1 Charset.defaultCharset (java.nio.charset.Charset.defaultCharset)1 CharReadable (org.neo4j.csv.reader.CharReadable)1