I have a list of CSV records.
import org.apache.commons.csv.CSVRecord
List<CSVRecord> records = getRecords();
I format records into a stringbuilder.
private StringBuilder formatRecords(final List<CSVRecord> records, final List<String> headersList) {
final StringBuilder sb = new StringBuilder();
final String headers = String.join(",", headersList);
sb.append(headers + "\n");
if (!records.isEmpty()) {
for (final CSVRecord r : records) {
for (int i = 0; i < r.size(); i++) {
sb.append(r.get(i));
// add comma if not last element
if (i < (r.size() - 1)) {
sb.append(",");
}
}
sb.append("\n");
}
}
return sb;
}
Then write string to HDFS file.
DataOutputStream outputStream = getHdfsOutputStream(destPath);
outputStream.writeBytes(records.toString());
This works fine with a small list.
But with a large list (say > 100000) the JVM has crashed with an OOME.
Administratively Yielded for 1 sec: java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:3664)
at java.lang.String.<init>(String.java:207)
at java.lang.StringBuilder.toString(StringBuilder.java:407)
Is it possible to bypass creation of the string builder and stream the contents of the list to HDFS?
This could involve 2 steps.
Pseudo code
outputStream.write(records.stream().map(<format to add commas and new line>))
But I'm not sure what is best for step 1.
This returns a list of strings so it's not streaming anything.
recs.stream().map(v -> v.get(0) + "," + v.get(1)).collect(toList());
This returns a stream of objects.
records.stream().map(v -> v.get(0) + "," + v.get(1) + "\n"));
How to feed this stream into HDFS?
Any help/tips would be greatly appreciated.
**** EDIT: ****
This approach seems to works for step 2 (write stream) but without the formatting of record.
final FSDataOutputStream outputStream = getHdfsOutputStream(destPath);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
for(CSVRecord r: records) {
bufferedWriter.write(r.toString());
bufferedWriter.newLine();
}
bufferedWriter.close();
configOutputStream.close();
Here the final code. Creates a string builder p/record and writes one string at a time.
private void persistRecords(final List<CSVRecord> records, final String targetDir, final String targetFile,
final List<String> headers) throws IOException {
final String targetPath = targetDir + targetFile;
try (final FSDataOutputStream outputStream = getHdfsOutputStream(targetPath);
final BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
bufferedWriter.write(String.join(",", headers));
bufferedWriter.newLine();
for (final CSVRecord r : records) {
final StringBuilder sb = format(r);
bufferedWriter.write(sb.toString());
bufferedWriter.newLine();
}
}
}
private StringBuilder format(final CSVRecord r) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < r.size(); i++) {
sb.append(r.get(i));
// add comma if not last element
if (i < (r.size() - 1)) {
sb.append(",");
}
}
return sb;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.