-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ImportTool.java
835 lines (761 loc) · 36.4 KB
/
ImportTool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/*
* 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.tooling;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.function.Function;
import org.neo4j.csv.reader.IllegalMultilineFieldException;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Args;
import org.neo4j.helpers.Args.Option;
import org.neo4j.helpers.ArrayUtil;
import org.neo4j.helpers.Exceptions;
import org.neo4j.helpers.Format;
import org.neo4j.helpers.Strings;
import org.neo4j.helpers.collection.IterableWrapper;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.logging.StoreLogService;
import org.neo4j.kernel.impl.storemigration.ExistingTargetStrategy;
import org.neo4j.kernel.impl.storemigration.FileOperation;
import org.neo4j.kernel.impl.storemigration.StoreFile;
import org.neo4j.kernel.impl.storemigration.StoreFileType;
import org.neo4j.kernel.impl.util.Converters;
import org.neo4j.kernel.impl.util.OsBeanUtil;
import org.neo4j.kernel.impl.util.Validator;
import org.neo4j.kernel.impl.util.Validators;
import org.neo4j.kernel.internal.Version;
import org.neo4j.kernel.lifecycle.LifeSupport;
import org.neo4j.unsafe.impl.batchimport.BatchImporter;
import org.neo4j.unsafe.impl.batchimport.ParallelBatchImporter;
import org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.DuplicateInputIdException;
import org.neo4j.unsafe.impl.batchimport.input.Collector;
import org.neo4j.unsafe.impl.batchimport.input.Input;
import org.neo4j.unsafe.impl.batchimport.input.InputException;
import org.neo4j.unsafe.impl.batchimport.input.InputNode;
import org.neo4j.unsafe.impl.batchimport.input.InputRelationship;
import org.neo4j.unsafe.impl.batchimport.input.MissingRelationshipDataException;
import org.neo4j.unsafe.impl.batchimport.input.csv.Configuration;
import org.neo4j.unsafe.impl.batchimport.input.csv.CsvInput;
import org.neo4j.unsafe.impl.batchimport.input.csv.DataFactory;
import org.neo4j.unsafe.impl.batchimport.input.csv.Decorator;
import org.neo4j.unsafe.impl.batchimport.input.csv.IdType;
import org.neo4j.unsafe.impl.batchimport.staging.ExecutionMonitors;
import static java.lang.Math.toIntExact;
import static java.nio.charset.Charset.defaultCharset;
import static org.neo4j.helpers.Exceptions.launderedException;
import static org.neo4j.helpers.Format.bytes;
import static org.neo4j.helpers.Strings.TAB;
import static org.neo4j.io.ByteUnit.mebiBytes;
import static org.neo4j.kernel.configuration.Settings.parseLongWithUnit;
import static org.neo4j.kernel.impl.util.Converters.withDefault;
import static org.neo4j.unsafe.impl.batchimport.Configuration.BAD_FILE_NAME;
import static org.neo4j.unsafe.impl.batchimport.input.Collectors.badCollector;
import static org.neo4j.unsafe.impl.batchimport.input.Collectors.collect;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.NO_NODE_DECORATOR;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.additiveLabels;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.defaultRelationshipType;
import static org.neo4j.unsafe.impl.batchimport.input.csv.Configuration.COMMAS;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DataFactories.data;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DataFactories.defaultFormatNodeFileHeader;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DataFactories.defaultFormatRelationshipFileHeader;
/**
* User-facing command line tool around a {@link BatchImporter}.
*/
public class ImportTool
{
private static final int UNSPECIFIED = -1;
enum Options
{
STORE_DIR( "into", null,
"<store-dir>",
"Database directory to import into. " + "Must not contain existing database." ),
NODE_DATA( "nodes", null,
"[:Label1:Label2] \"<file1>" + MULTI_FILE_DELIMITER + "<file2>" + MULTI_FILE_DELIMITER + "...\"",
"Node CSV header and data. Multiple files will be logically seen as one big file "
+ "from the perspective of the importer. "
+ "The first line must contain the header. "
+ "Multiple data sources like these can be specified in one import, "
+ "where each data source has its own header. "
+ "Note that file groups must be enclosed in quotation marks.",
true ),
RELATIONSHIP_DATA( "relationships", null,
"[:RELATIONSHIP_TYPE] \"<file1>" + MULTI_FILE_DELIMITER + "<file2>" +
MULTI_FILE_DELIMITER + "...\"",
"Relationship CSV header and data. Multiple files will be logically seen as one big file "
+ "from the perspective of the importer. "
+ "The first line must contain the header. "
+ "Multiple data sources like these can be specified in one import, "
+ "where each data source has its own header. "
+ "Note that file groups must be enclosed in quotation marks.",
true ),
DELIMITER( "delimiter", null,
"<delimiter-character>",
"Delimiter character, or 'TAB', between values in CSV data. The default option is `" + COMMAS.delimiter() + "`." ),
ARRAY_DELIMITER( "array-delimiter", null,
"<array-delimiter-character>",
"Delimiter character, or 'TAB', between array elements within a value in CSV data. The default option is `" + COMMAS.arrayDelimiter() + "`." ),
QUOTE( "quote", null,
"<quotation-character>",
"Character to treat as quotation character for values in CSV data. "
+ "The default option is `" + COMMAS.quotationCharacter() + "`. "
+ "Quotes inside quotes escaped like `\"\"\"Go away\"\", he said.\"` and "
+ "`\"\\\"Go away\\\", he said.\"` are supported. "
+ "If you have set \"`'`\" to be used as the quotation character, "
+ "you could write the previous example like this instead: " + "`'\"Go away\", he said.'`" ),
MULTILINE_FIELDS( "multiline-fields", org.neo4j.csv.reader.Configuration.DEFAULT.multilineFields(),
"<true/false>",
"Whether or not fields from input source can span multiple lines, i.e. contain newline characters." ),
TRIM_STRINGS( "trim-strings", org.neo4j.csv.reader.Configuration.DEFAULT.trimStrings(),
"<true/false>",
"Whether or not strings should be trimmed for whitespaces."),
INPUT_ENCODING( "input-encoding", null,
"<character set>",
"Character set that input data is encoded in. Provided value must be one out of the available "
+ "character sets in the JVM, as provided by Charset#availableCharsets(). "
+ "If no input encoding is provided, the default character set of the JVM will be used." ),
IGNORE_EMPTY_STRINGS( "ignore-empty-strings", org.neo4j.csv.reader.Configuration.DEFAULT.emptyQuotedStringsAsNull(),
"<true/false>",
"Whether or not empty string fields, i.e. \"\" from input source are ignored, i.e. treated as null." ),
ID_TYPE( "id-type", IdType.STRING,
"<id-type>",
"One out of " + Arrays.toString( IdType.values() )
+ " and specifies how ids in node/relationship "
+ "input files are treated.\n"
+ IdType.STRING + ": arbitrary strings for identifying nodes.\n"
+ IdType.INTEGER + ": arbitrary integer values for identifying nodes.\n"
+ IdType.ACTUAL + ": (advanced) actual node ids. The default option is `" + IdType.STRING + "`." ),
PROCESSORS( "processors", null,
"<max processor count>",
"(advanced) Max number of processors used by the importer. Defaults to the number of "
+ "available processors reported by the JVM"
+ availableProcessorsHint()
+ ". There is a certain amount of minimum threads needed so for that reason there "
+ "is no lower bound for this value. For optimal performance this value shouldn't be "
+ "greater than the number of available processors." ),
STACKTRACE( "stacktrace", null,
"<true/false>",
"Enable printing of error stack traces." ),
BAD_TOLERANCE( "bad-tolerance", 1000,
"<max number of bad entries>",
"Number of bad entries before the import is considered failed. This tolerance threshold is "
+ "about relationships refering to missing nodes. Format errors in input data are "
+ "still treated as errors" ),
SKIP_BAD_RELATIONSHIPS( "skip-bad-relationships", Boolean.TRUE,
"<true/false>",
"Whether or not to skip importing relationships that refers to missing node ids, i.e. either "
+ "start or end node id/group referring to node that wasn't specified by the "
+ "node input data. "
+ "Skipped nodes will be logged"
+ ", containing at most number of entites specified by " + BAD_TOLERANCE.key() + "." ),
SKIP_DUPLICATE_NODES( "skip-duplicate-nodes", Boolean.FALSE,
"<true/false>",
"Whether or not to skip importing nodes that have the same id/group. In the event of multiple "
+ "nodes within the same group having the same id, the first encountered will be imported "
+ "whereas consecutive such nodes will be skipped. "
+ "Skipped nodes will be logged"
+ ", containing at most number of entities specified by " + BAD_TOLERANCE.key() + "." ),
IGNORE_EXTRA_COLUMNS( "ignore-extra-columns", Boolean.FALSE,
"<true/false>",
"Whether or not to ignore extra columns in the data not specified by the header. "
+ "Skipped columns will be logged, containing at most number of entities specified by "
+ BAD_TOLERANCE.key() + "." ),
DATABASE_CONFIG( "db-config", null,
"<path/to/neo4j.conf>",
"(advanced) File specifying database-specific configuration. For more information consult "
+ "manual about available configuration options for a neo4j configuration file. "
+ "Only configuration affecting store at time of creation will be read. "
+ "Examples of supported config are:\n"
+ GraphDatabaseSettings.dense_node_threshold.name() + "\n"
+ GraphDatabaseSettings.string_block_size.name() + "\n"
+ GraphDatabaseSettings.array_block_size.name() ),
PAGE_SIZE( "page-size", Format.bytes( org.neo4j.unsafe.impl.batchimport.Configuration.DEFAULT.pageSize() ),
"<page size in bytes",
"Page size in bytes, or e.g. 4M or 8k" );
private final String key;
private final Object defaultValue;
private final String usage;
private final String description;
private final boolean keyAndUsageGoTogether;
Options( String key, Object defaultValue, String usage, String description )
{
this( key, defaultValue, usage, description, false );
}
Options( String key, Object defaultValue, String usage, String description, boolean keyAndUsageGoTogether )
{
this.key = key;
this.defaultValue = defaultValue;
this.usage = usage;
this.description = description;
this.keyAndUsageGoTogether = keyAndUsageGoTogether;
}
String key()
{
return key;
}
String argument()
{
return "--" + key();
}
void printUsage( PrintStream out )
{
out.println( argument() + spaceInBetweenArgumentAndUsage() + usage );
for ( String line : Args.splitLongLine( descriptionWithDefaultValue().replace( "`", "" ), 80 ) )
{
out.println( "\t" + line );
}
}
private String spaceInBetweenArgumentAndUsage()
{
return keyAndUsageGoTogether ? "" : " ";
}
String descriptionWithDefaultValue()
{
String result = description;
if ( defaultValue != null )
{
if ( !result.endsWith( "." ) )
{
result += ".";
}
result += " Default value: " + defaultValue;
}
return result;
}
String manPageEntry()
{
String filteredDescription = descriptionWithDefaultValue().replace( availableProcessorsHint(), "" );
String usageString = (usage.length() > 0) ? spaceInBetweenArgumentAndUsage() + usage : "";
return "*" + argument() + usageString + "*::\n" + filteredDescription + "\n\n";
}
String manualEntry()
{
return "[[import-tool-option-" + key() + "]]\n" + manPageEntry() + "//^\n\n";
}
Object defaultValue()
{
return defaultValue;
}
private static String availableProcessorsHint()
{
return " (in your case " + Runtime.getRuntime().availableProcessors() + ")";
}
}
/**
* Delimiter used between files in an input group.
*/
static final String MULTI_FILE_DELIMITER = ",";
/**
* Runs the import tool given the supplied arguments.
*
* @param incomingArguments arguments for specifying input and configuration for the import.
*/
public static void main( String[] incomingArguments ) throws IOException
{
main( incomingArguments, false );
}
/**
* Runs the import tool given the supplied arguments.
*
* @param incomingArguments arguments for specifying input and configuration for the import.
* @param defaultSettingsSuitableForTests default configuration geared towards unit/integration
* test environments, for example lower default buffer sizes.
*/
public static void main( String[] incomingArguments, boolean defaultSettingsSuitableForTests ) throws IOException
{
PrintStream out = System.out;
PrintStream err = System.err;
Args args = Args.parse( incomingArguments );
if ( ArrayUtil.isEmpty( incomingArguments ) || asksForUsage( args ) )
{
printUsage( out );
return;
}
FileSystemAbstraction fs = new DefaultFileSystemAbstraction();
File storeDir;
Collection<Option<File[]>> nodesFiles, relationshipsFiles;
boolean enableStacktrace;
Number processors = null;
Input input = null;
int badTolerance;
Charset inputEncoding;
boolean skipBadRelationships, skipDuplicateNodes, ignoreExtraColumns;
Config dbConfig;
OutputStream badOutput = null;
int pageSize = UNSPECIFIED;
org.neo4j.unsafe.impl.batchimport.Configuration configuration = null;
boolean success = false;
try
{
storeDir = args.interpretOption( Options.STORE_DIR.key(), Converters.<File>mandatory(),
Converters.toFile(), Validators.DIRECTORY_IS_WRITABLE, Validators.CONTAINS_NO_EXISTING_DATABASE );
File badFile = new File( storeDir, BAD_FILE_NAME );
badOutput = new BufferedOutputStream( fs.openAsOutputStream( badFile, false ) );
nodesFiles = extractInputFiles( args, Options.NODE_DATA.key(), err );
relationshipsFiles = extractInputFiles( args, Options.RELATIONSHIP_DATA.key(), err );
validateInputFiles( nodesFiles, relationshipsFiles );
enableStacktrace = args.getBoolean( Options.STACKTRACE.key(), Boolean.FALSE, Boolean.TRUE );
processors = args.getNumber( Options.PROCESSORS.key(), null );
IdType idType = args.interpretOption( Options.ID_TYPE.key(),
withDefault( (IdType)Options.ID_TYPE.defaultValue() ), TO_ID_TYPE );
badTolerance = args.getNumber( Options.BAD_TOLERANCE.key(),
(Number) Options.BAD_TOLERANCE.defaultValue() ).intValue();
inputEncoding = Charset.forName( args.get( Options.INPUT_ENCODING.key(), defaultCharset().name() ) );
skipBadRelationships = args.getBoolean( Options.SKIP_BAD_RELATIONSHIPS.key(),
(Boolean)Options.SKIP_BAD_RELATIONSHIPS.defaultValue(), true );
skipDuplicateNodes = args.getBoolean( Options.SKIP_DUPLICATE_NODES.key(),
(Boolean)Options.SKIP_DUPLICATE_NODES.defaultValue(), true );
ignoreExtraColumns = args.getBoolean( Options.IGNORE_EXTRA_COLUMNS.key(),
(Boolean)Options.IGNORE_EXTRA_COLUMNS.defaultValue(), true );
Collector badCollector = badCollector( badOutput, badTolerance, collect( skipBadRelationships,
skipDuplicateNodes, ignoreExtraColumns ) );
dbConfig = loadDbConfig( args.interpretOption( Options.DATABASE_CONFIG.key(), Converters.<File>optional(),
Converters.toFile(), Validators.REGEX_FILE_EXISTS ) );
if ( args.has( Options.PAGE_SIZE.key() ) )
{
pageSize = toIntExact( parseLongWithUnit( args.get( Options.PAGE_SIZE.key(),
String.valueOf( UNSPECIFIED ) ) ) );
}
configuration = importConfiguration( processors, defaultSettingsSuitableForTests, dbConfig, pageSize );
input = new CsvInput( nodeData( inputEncoding, nodesFiles ), defaultFormatNodeFileHeader(),
relationshipData( inputEncoding, relationshipsFiles ), defaultFormatRelationshipFileHeader(),
idType, csvConfiguration( args, defaultSettingsSuitableForTests ), badCollector,
configuration.maxNumberOfProcessors() );
success = true;
}
catch ( IllegalArgumentException e )
{
throw andPrintError( "Input error", e, false, err );
}
catch ( IOException e )
{
throw andPrintError( "File error", e, false, err );
}
finally
{
if ( !success && badOutput != null )
{
badOutput.close();
}
}
LifeSupport life = new LifeSupport();
LogService logService = life.add( StoreLogService.inLogsDirectory( fs, storeDir ) );
life.start();
BatchImporter importer = new ParallelBatchImporter( storeDir,
configuration,
logService,
ExecutionMonitors.defaultVisible(),
dbConfig );
printOverview( storeDir, nodesFiles, relationshipsFiles, configuration, out );
success = false;
try
{
importer.doImport( input );
success = true;
}
catch ( Exception e )
{
throw andPrintError( "Import error", e, enableStacktrace, err );
}
finally
{
input.badCollector().close();
badOutput.close();
if ( input.badCollector().badEntries() > 0 )
{
File badFile = new File( storeDir, BAD_FILE_NAME );
if ( badFile.exists() )
{
out.println(
"There were bad entries which were skipped and logged into " + badFile.getAbsolutePath() );
}
}
life.shutdown();
if ( !success )
{
try
{
StoreFile.fileOperation( FileOperation.DELETE, fs, storeDir, null,
Iterables.<StoreFile,StoreFile>iterable( StoreFile.values() ),
false, ExistingTargetStrategy.FAIL, StoreFileType.values() );
}
catch ( IOException e )
{
err.println( "Unable to delete store files after an aborted import " + e );
if ( enableStacktrace )
{
e.printStackTrace();
}
}
}
}
}
private static Collection<Option<File[]>> extractInputFiles( Args args, String key, PrintStream err )
{
return args
.interpretOptionsWithMetadata( key, Converters.<File[]>optional(),
Converters.toFiles( MULTI_FILE_DELIMITER, Converters.regexFiles( true ) ), filesExist(
err ),
Validators.<File>atLeast( "--" + key, 1 ) );
}
private static Validator<File[]> filesExist( PrintStream err )
{
return files -> {
for ( File file : files )
{
if ( file.getName().startsWith( ":" ) )
{
err.println( "It looks like you're trying to specify default label or relationship type (" +
file.getName() + "). Please put such directly on the key, f.ex. " +
Options.NODE_DATA.argument() + ":MyLabel" );
}
Validators.REGEX_FILE_EXISTS.validate( file );
}
};
}
private static Config loadDbConfig( File file ) throws IOException
{
return file != null && file.exists() ? new Config( MapUtil.load( file ) ) : Config.defaults();
}
private static void printOverview( File storeDir, Collection<Option<File[]>> nodesFiles,
Collection<Option<File[]>> relationshipsFiles,
org.neo4j.unsafe.impl.batchimport.Configuration configuration, PrintStream out )
{
out.println( "Neo4j version: " + Version.getKernel().getReleaseVersion() );
out.println( "Importing the contents of these files into " + storeDir + ":" );
printInputFiles( "Nodes", nodesFiles, out );
printInputFiles( "Relationships", relationshipsFiles, out );
out.println();
out.println( "Available resources:" );
printIndented( "Free machine memory: " + bytes( OsBeanUtil.getFreePhysicalMemory() ), out );
printIndented( "Max heap memory : " + bytes( Runtime.getRuntime().maxMemory() ), out );
printIndented( "Processors: " + configuration.maxNumberOfProcessors(), out );
out.println();
}
private static void printInputFiles( String name, Collection<Option<File[]>> files, PrintStream out )
{
if ( files.isEmpty() )
{
return;
}
out.println( name + ":" );
int i = 0;
for ( Option<File[]> group : files )
{
if ( i++ > 0 )
{
out.println();
}
if ( group.metadata() != null )
{
printIndented( ":" + group.metadata(), out );
}
for ( File file : group.value() )
{
printIndented( file, out );
}
}
}
private static void printIndented( Object value, PrintStream out )
{
out.println( " " + value );
}
private static void validateInputFiles( Collection<Option<File[]>> nodesFiles,
Collection<Option<File[]>> relationshipsFiles )
{
if ( nodesFiles.isEmpty() )
{
if ( relationshipsFiles.isEmpty() )
{
throw new IllegalArgumentException( "No input specified, nothing to import" );
}
throw new IllegalArgumentException( "No node input specified, cannot import relationships without nodes" );
}
}
private static org.neo4j.unsafe.impl.batchimport.Configuration importConfiguration( final Number processors,
final boolean defaultSettingsSuitableForTests, final Config dbConfig, int pageSize )
{
return new org.neo4j.unsafe.impl.batchimport.Configuration.Default()
{
@Override
public long pageCacheMemory()
{
return defaultSettingsSuitableForTests ? mebiBytes( 8 ) : super.pageCacheMemory();
}
@Override
public int maxNumberOfProcessors()
{
return processors != null ? processors.intValue() : super.maxNumberOfProcessors();
}
@Override
public int denseNodeThreshold()
{
return dbConfig.get( GraphDatabaseSettings.dense_node_threshold );
}
@Override
public int pageSize()
{
// Let's call super if not specifically configured, so that proper calculations can be made
return pageSize == UNSPECIFIED ? super.pageSize() : pageSize;
}
};
}
private static String manualReference( ManualPage page, Anchor anchor )
{
// Docs are versioned major.minor-suffix, so drop the patch version.
String[] versionParts = Version.getKernel().getReleaseVersion().split("-");
versionParts[0] = versionParts[0].substring(0, 3);
String docsVersion = String.join("-", versionParts);
return " http://neo4j.com/docs/operations-manual/" + docsVersion + "/" +
page.getReference( anchor );
}
/**
* Method name looks strange, but look at how it's used and you'll see why it's named like that.
* @param stackTrace whether or not to also print the stack trace of the error.
* @param err
*/
private static RuntimeException andPrintError( String typeOfError, Exception e, boolean stackTrace,
PrintStream err )
{
// List of common errors that can be explained to the user
if ( DuplicateInputIdException.class.equals( e.getClass() ) )
{
printErrorMessage( "Duplicate input ids that would otherwise clash can be put into separate id space, " +
"read more about how to use id spaces in the manual:" +
manualReference( ManualPage.IMPORT_TOOL_FORMAT, Anchor.ID_SPACES ), e, stackTrace,
err );
}
else if ( MissingRelationshipDataException.class.equals( e.getClass() ) )
{
printErrorMessage( "Relationship missing mandatory field '" +
((MissingRelationshipDataException) e).getFieldType() + "', read more about " +
"relationship format in the manual: " +
manualReference( ManualPage.IMPORT_TOOL_FORMAT, Anchor.RELATIONSHIP ), e, stackTrace,
err );
}
// This type of exception is wrapped since our input code throws InputException consistently,
// and so IllegalMultilineFieldException comes from the csv component, which has no access to InputException
// therefore it's wrapped.
else if ( Exceptions.contains( e, IllegalMultilineFieldException.class ) )
{
printErrorMessage( "Detected field which spanned multiple lines for an import where " +
Options.MULTILINE_FIELDS.argument() + "=false. If you know that your input data " +
"include fields containing new-line characters then import with this option set to " +
"true.", e, stackTrace, err );
}
else if ( Exceptions.contains( e, InputException.class ) )
{
printErrorMessage( "Error in input data", e, stackTrace, err );
}
// Fallback to printing generic error and stack trace
else
{
printErrorMessage( typeOfError + ": " + e.getMessage(), e, true, err );
}
err.println();
// Mute the stack trace that the default exception handler would have liked to print.
// Calling System.exit( 1 ) or similar would be convenient on one hand since we can set
// a specific exit code. On the other hand It's very inconvenient to have any System.exit
// call in code that is tested.
Thread.currentThread().setUncaughtExceptionHandler( ( t, e1 ) -> { /* Shhhh */ } );
return launderedException( e ); // throw in order to have process exit with !0
}
private static void printErrorMessage( String string, Exception e, boolean stackTrace, PrintStream err )
{
err.println( string );
err.println( "Caused by:" + e.getMessage() );
if ( stackTrace )
{
e.printStackTrace( err );
}
}
private static Iterable<DataFactory<InputRelationship>>
relationshipData( final Charset encoding, Collection<Option<File[]>> relationshipsFiles )
{
return new IterableWrapper<DataFactory<InputRelationship>,Option<File[]>>( relationshipsFiles )
{
@Override
protected DataFactory<InputRelationship> underlyingObjectToObject( Option<File[]> group )
{
return data( defaultRelationshipType( group.metadata() ), encoding, group.value() );
}
};
}
private static Iterable<DataFactory<InputNode>> nodeData( final Charset encoding,
Collection<Option<File[]>> nodesFiles )
{
return new IterableWrapper<DataFactory<InputNode>,Option<File[]>>( nodesFiles )
{
@Override
protected DataFactory<InputNode> underlyingObjectToObject( Option<File[]> input )
{
Decorator<InputNode> decorator = input.metadata() != null
? additiveLabels( input.metadata().split( ":" ) )
: NO_NODE_DECORATOR;
return data( decorator, encoding, input.value() );
}
};
}
private static void printUsage( PrintStream out )
{
out.println( "Neo4j Import Tool" );
for ( String line : Args.splitLongLine( "neo4j-import is used to create a new Neo4j database "
+ "from data in CSV files. "
+
"See the chapter \"Import Tool\" in the Neo4j Manual for details on the CSV file format "
+ "- a special kind of header is required.", 80 ) )
{
out.println( "\t" + line );
}
out.println( "Usage:" );
for ( Options option : Options.values() )
{
option.printUsage( out );
}
out.println( "Example:");
out.print( Strings.joinAsLines(
TAB + "bin/neo4j-import --into retail.db --id-type string --nodes:Customer customers.csv ",
TAB + "--nodes products.csv --nodes orders_header.csv,orders1.csv,orders2.csv ",
TAB + "--relationships:CONTAINS order_details.csv ",
TAB + "--relationships:ORDERED customer_orders_header.csv,orders1.csv,orders2.csv" ) );
}
private static boolean asksForUsage( Args args )
{
for ( String orphan : args.orphans() )
{
if ( isHelpKey( orphan ) )
{
return true;
}
}
for ( Entry<String,String> option : args.asMap().entrySet() )
{
if ( isHelpKey( option.getKey() ) )
{
return true;
}
}
return false;
}
private static boolean isHelpKey( String key )
{
return key.equals( "?" ) || key.equals( "help" );
}
private static Configuration csvConfiguration( Args args, final boolean defaultSettingsSuitableForTests )
{
final Configuration defaultConfiguration = COMMAS;
final Character specificDelimiter = args.interpretOption( Options.DELIMITER.key(),
Converters.<Character>optional(), CHARACTER_CONVERTER );
final Character specificArrayDelimiter = args.interpretOption( Options.ARRAY_DELIMITER.key(),
Converters.<Character>optional(), CHARACTER_CONVERTER );
final Character specificQuote = args.interpretOption( Options.QUOTE.key(), Converters.<Character>optional(),
CHARACTER_CONVERTER );
final Boolean multiLineFields = args.getBoolean( Options.MULTILINE_FIELDS.key(), null );
final Boolean emptyStringsAsNull = args.getBoolean( Options.IGNORE_EMPTY_STRINGS.key(), null );
final Boolean trimStrings = args.getBoolean( Options.TRIM_STRINGS.key(), null);
return new Configuration.Default()
{
@Override
public char delimiter()
{
return specificDelimiter != null
? specificDelimiter.charValue()
: defaultConfiguration.delimiter();
}
@Override
public char arrayDelimiter()
{
return specificArrayDelimiter != null
? specificArrayDelimiter.charValue()
: defaultConfiguration.arrayDelimiter();
}
@Override
public char quotationCharacter()
{
return specificQuote != null
? specificQuote.charValue()
: defaultConfiguration.quotationCharacter();
}
@Override
public boolean multilineFields()
{
return multiLineFields != null
? multiLineFields.booleanValue()
: defaultConfiguration.multilineFields();
}
@Override
public boolean emptyQuotedStringsAsNull()
{
return emptyStringsAsNull != null
? emptyStringsAsNull.booleanValue()
: defaultConfiguration.emptyQuotedStringsAsNull();
}
@Override
public int bufferSize()
{
return defaultSettingsSuitableForTests ? 10_000 : super.bufferSize();
}
@Override
public boolean trimStrings()
{
return trimStrings != null
? trimStrings.booleanValue()
: defaultConfiguration.trimStrings();
}
};
}
private static final Function<String,IdType> TO_ID_TYPE = from -> IdType.valueOf( from.toUpperCase() );
private static final Function<String,Character> CHARACTER_CONVERTER = new CharacterConverter();
private enum ManualPage
{
IMPORT_TOOL_FORMAT( "import-tool-header-format.html" );
private final String page;
ManualPage( String page )
{
this.page = page;
}
public String getReference( Anchor anchor )
{
// As long as the the operations manual is single-page we only use the anchor.
return "#" + anchor.anchor;
}
}
private enum Anchor
{
ID_SPACES( "import-tool-id-spaces" ),
RELATIONSHIP( "import-tool-header-format-rels" );
private final String anchor;
Anchor( String anchor )
{
this.anchor = anchor;
}
}
}