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 custom separator support for CSV files #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 22 additions & 9 deletions src/main/java/com/vaticle/typedb/osi/loader/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static String[] getFileHeader(String filePath, char separator) throws IOE
} else if (separator == '\t') {
return parseTSV(br.readLine());
} else {
throw new IllegalArgumentException("currently supported separators are: <,>, <\t>");
return parseCSV(br.readLine(), separator);
}
}

Expand Down Expand Up @@ -108,19 +108,24 @@ public static String[] parseBySeparator(String line, char separator) throws Ille
} else if (separator == '\t') {
return parseTSV(line);
} else {
throw new IllegalArgumentException("currently supported separators are: <,>, <\t>");
return parseCSV(line, separator);
}
}

public static String[] parseCSV(String line) {
return parseCSV(line, ',');
}

public static String[] parseCSV(String line, char delimiter) {
if (!line.isEmpty()) {
CSVFormat format = CSV_FORMAT.withDelimiter(delimiter);
try {
CSVRecord csv = CSVParser.parse(line, CSV_FORMAT).getRecords().get(0);
CSVRecord csv = CSVParser.parse(line, format).getRecords().get(0);
return parse(csv);
} catch (IOException ioException) {
line = line.replace("\"", "");
try {
CSVRecord csv = CSVParser.parse(line, CSV_FORMAT).getRecords().get(0);
CSVRecord csv = CSVParser.parse(line, format).getRecords().get(0);
return parse(csv);
} catch (IOException ioException2) {
//TODO LOG INTO ERRORS
Expand All @@ -130,7 +135,7 @@ public static String[] parseCSV(String line) {
} else {
return new String[0];
}
}
}

public static String[] parseTSV(String line) {
if (!line.isEmpty()) {
Expand All @@ -155,10 +160,18 @@ public static String[] parseTSV(String line) {
private static String[] parse(CSVRecord record) {
String[] arr = new String[record.size()];
for (int i = 0; i < record.size(); i++) {
arr[i] = record.get(i);
if (arr[i] != null) {
if ((arr[i].equals("\\N") || arr[i].equalsIgnoreCase("null"))) arr[i] = null;
else arr[i] = escapeDoubleQuotes(arr[i]);
try {
arr[i] = record.get(i);
if (arr[i] != null) {
if (arr[i].equals("\\N") || arr[i].equalsIgnoreCase("null")) {
arr[i] = null;
} else {
arr[i] = escapeDoubleQuotes(arr[i]);
}
}
} catch (Exception e) {
appLogger.error("Error parsing CSV record: " + record.toString());
arr[i] = null;
}
}
return arr;
Expand Down