Skip to content

Commit

Permalink
Add an option to specify the report file.
Browse files Browse the repository at this point in the history
  • Loading branch information
srbaker committed Oct 14, 2016
1 parent 83615a2 commit 28c1de3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
Expand Up @@ -64,10 +64,14 @@ class CsvImporter implements Importer
private final Config config;
private final Args args;
private final OutsideWorld outsideWorld;
private final String reportFileName;

public static String description()
{
return "--mode=csv Import a database from a collection of CSV files.\n" +
"--report-file <filename>\n" +
" File name in which to store the report of the import.\n" +
" Defaults to import.report in the current directory.\n" +
"--nodes[:Label1:Label2]=\"<file1>,<file2>,...\"\n" +
" Node CSV header and data. Multiple files will be logically seen as\n" +
" one big file from the perspective of the importer. The first line\n" +
Expand All @@ -94,7 +98,7 @@ public static String description()

public static String arguments()
{
return "[--nodes[:Label1:Label2]=\"<file1>,<file2>,...\"] " +
return "[--report-file=<filename>] [--nodes[:Label1:Label2]=\"<file1>,<file2>,...\"] " +
"[--relationships[:RELATIONSHIP_TYPE]=\"<file1>,<file2>,...\"] " +
"[--input-encoding=<character-set>] " + "[--id-type=<id-type>] ";
}
Expand All @@ -105,6 +109,7 @@ public static String arguments()
this.outsideWorld = outsideWorld;
nodesFiles = extractInputFiles( args, "nodes", outsideWorld.errorStream() );
relationshipsFiles = extractInputFiles( args, "relationships", outsideWorld.errorStream() );
reportFileName = args.interpretOption( "report-file", withDefault( "import.report" ), s -> s );
try
{
validateInputFiles( nodesFiles, relationshipsFiles );
Expand All @@ -128,8 +133,9 @@ public void doImport() throws IOException
FileSystemAbstraction fs = outsideWorld.fileSystem();
File storeDir = config.get( DatabaseManagementSystemSettings.database_path );
File logsDir = config.get( GraphDatabaseSettings.logs_directory );
File badFile = new File( logsDir, BAD_FILE_NAME );
OutputStream badOutput = new BufferedOutputStream( fs.openAsOutputStream( badFile, false ) );
File reportFile = new File( reportFileName );

OutputStream badOutput = new BufferedOutputStream( fs.openAsOutputStream( reportFile, false ) );
Collector badCollector = badCollector( badOutput, 1000, collect( true, false, false ) );

Configuration configuration = importConfiguration( null, false, config, pageSize );
Expand All @@ -138,7 +144,7 @@ public void doImport() throws IOException
csvConfiguration( args, false ), badCollector, configuration.maxNumberOfProcessors() );

ImportTool.doImport( outsideWorld.errorStream(), outsideWorld.errorStream(),
storeDir, logsDir, badFile, fs, nodesFiles, relationshipsFiles, false,
storeDir, logsDir, reportFile, fs, nodesFiles, relationshipsFiles, false,
input, config, badOutput, configuration );
}
}
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.commandline.dbms;

import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

import org.neo4j.commandline.admin.RealOutsideWorld;
import org.neo4j.helpers.Args;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.test.rule.TestDirectory;

import static org.junit.Assert.assertTrue;

public class CsvImporterTest
{
@Rule
public final TestDirectory testDir = TestDirectory.testDirectory();

@Test
public void writesReportToSpecifiedReportFile() throws Exception
{
File reportLocation = testDir.file( "the_report" );

File inputFile = testDir.file( "foobar.csv" );
List<String> lines = Arrays.asList( "foo,bar,baz" );
Files.write( inputFile.toPath(), lines, Charset.defaultCharset() );

CsvImporter csvImporter = new CsvImporter(
Args.parse( String.format( "--report-file=%s", reportLocation.getAbsolutePath() ),
String.format( "--nodes=%s", inputFile.getAbsolutePath() ) ), Config.defaults(),
new RealOutsideWorld() );

csvImporter.doImport();

assertTrue( reportLocation.exists() );
}
}

0 comments on commit 28c1de3

Please sign in to comment.