2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
package eu.dnetlib.dhp.common;
|
2020-11-03 16:54:50 +01:00
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
|
|
|
import org.apache.hadoop.fs.*;
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
public class MakeTarArchive implements Serializable {
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
private static TarArchiveOutputStream getTar(FileSystem fileSystem, String outputPath) throws IOException {
|
|
|
|
Path hdfsWritePath = new Path(outputPath);
|
|
|
|
FSDataOutputStream fsDataOutputStream = null;
|
|
|
|
if (fileSystem.exists(hdfsWritePath)) {
|
|
|
|
fileSystem.delete(hdfsWritePath, true);
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
}
|
|
|
|
fsDataOutputStream = fileSystem.create(hdfsWritePath);
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
return new TarArchiveOutputStream(fsDataOutputStream.getWrappedStream());
|
|
|
|
}
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
private static void write(FileSystem fileSystem, String inputPath, String outputPath, String dir_name)
|
|
|
|
throws IOException {
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
Path hdfsWritePath = new Path(outputPath);
|
|
|
|
FSDataOutputStream fsDataOutputStream = null;
|
|
|
|
if (fileSystem.exists(hdfsWritePath)) {
|
|
|
|
fileSystem.delete(hdfsWritePath, true);
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
}
|
|
|
|
fsDataOutputStream = fileSystem.create(hdfsWritePath);
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
TarArchiveOutputStream ar = new TarArchiveOutputStream(fsDataOutputStream.getWrappedStream());
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
RemoteIterator<LocatedFileStatus> fileStatusListIterator = fileSystem
|
|
|
|
.listFiles(
|
|
|
|
new Path(inputPath), true);
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
while (fileStatusListIterator.hasNext()) {
|
|
|
|
writeCurrentFile(fileSystem, dir_name, fileStatusListIterator, ar, 0);
|
|
|
|
}
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
ar.close();
|
|
|
|
}
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
public static void tarMaxSize(FileSystem fileSystem, String inputPath, String outputPath, String dir_name,
|
|
|
|
int gBperSplit) throws IOException {
|
|
|
|
final long bytesPerSplit = 1024L * 1024L * 1024L * gBperSplit;
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
long sourceSize = fileSystem.getContentSummary(new Path(inputPath)).getSpaceConsumed();
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
if (sourceSize < bytesPerSplit) {
|
|
|
|
write(fileSystem, inputPath, outputPath + ".tar", dir_name);
|
|
|
|
} else {
|
|
|
|
int partNum = 0;
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
RemoteIterator<LocatedFileStatus> fileStatusListIterator = fileSystem
|
|
|
|
.listFiles(
|
|
|
|
new Path(inputPath), true);
|
|
|
|
boolean next = fileStatusListIterator.hasNext();
|
|
|
|
while (next) {
|
|
|
|
TarArchiveOutputStream ar = getTar(fileSystem, outputPath + "_" + (partNum + 1) + ".tar");
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
long current_size = 0;
|
|
|
|
while (next && current_size < bytesPerSplit) {
|
|
|
|
current_size = writeCurrentFile(fileSystem, dir_name, fileStatusListIterator, ar, current_size);
|
|
|
|
next = fileStatusListIterator.hasNext();
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
}
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
partNum += 1;
|
|
|
|
ar.close();
|
|
|
|
}
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
}
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
}
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
private static long writeCurrentFile(FileSystem fileSystem, String dir_name,
|
|
|
|
RemoteIterator<LocatedFileStatus> fileStatusListIterator,
|
|
|
|
TarArchiveOutputStream ar, long current_size) throws IOException {
|
|
|
|
LocatedFileStatus fileStatus = fileStatusListIterator.next();
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
Path p = fileStatus.getPath();
|
|
|
|
String p_string = p.toString();
|
|
|
|
if (!p_string.endsWith("_SUCCESS")) {
|
|
|
|
String name = p_string.substring(p_string.lastIndexOf("/") + 1);
|
|
|
|
TarArchiveEntry entry = new TarArchiveEntry(dir_name + "/" + name);
|
|
|
|
entry.setSize(fileStatus.getLen());
|
|
|
|
current_size += fileStatus.getLen();
|
|
|
|
ar.putArchiveEntry(entry);
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
InputStream is = fileSystem.open(fileStatus.getPath());
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
BufferedInputStream bis = new BufferedInputStream(is);
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
int count;
|
|
|
|
byte data[] = new byte[1024];
|
|
|
|
while ((count = bis.read(data, 0, data.length)) != -1) {
|
|
|
|
ar.write(data, 0, count);
|
|
|
|
}
|
|
|
|
bis.close();
|
|
|
|
ar.closeArchiveEntry();
|
2020-11-03 16:54:50 +01:00
|
|
|
|
2020-11-04 18:12:43 +01:00
|
|
|
}
|
|
|
|
return current_size;
|
|
|
|
}
|
2020-11-03 16:54:50 +01:00
|
|
|
|
|
|
|
}
|