복붙노트

[HADOOP] 먼저 로컬 파일 시스템에 복사하지 않고 Java를 사용하여 HDFS에 저장된 파일의 압축을 해제 하시겠습니까?

HADOOP

먼저 로컬 파일 시스템에 복사하지 않고 Java를 사용하여 HDFS에 저장된 파일의 압축을 해제 하시겠습니까?

우리는 XML 파일을 포함하는 zip 파일을 HDFS에 저장합니다. 프로그래밍 방식으로 파일의 압축을 풀고 포함 된 XML 파일을 Java를 사용하여 스트리밍 할 수 있어야합니다. FileSystem.open은 FSDataInputStream을 반환하지만 ZipFile 생성자는 File 또는 String 만 매개 변수로 사용합니다. 필자는 FileSystem.copyToLocalFile을 사용하고 싶지 않습니다.

zip 파일을 먼저 로컬 파일 시스템에 복사하지 않고 HDFS에 저장된 zip 파일의 내용을 스트리밍 할 수 있습니까? 그렇다면 어떻게?

해결법

  1. ==============================

    1.안녕 샘플 코드를 찾으십시오.

    안녕 샘플 코드를 찾으십시오.

    public static Map<String, byte[]> loadZipFileData(String hdfsFilePath) {
                try {
                    ZipInputStream zipInputStream = readZipFileFromHDFS(new Path(hdfsFilePath));
                    ZipEntry zipEntry = null;
                    byte[] buf = new byte[1024];
                    Map<String, byte[]> listOfFiles = new LinkedHashMap<>();
                    while ((zipEntry = zipInputStream.getNextEntry()) != null ) {
                        int bytesRead = 0;
                        String entryName = zipEntry.getName();
                        if (!zipEntry.isDirectory()) {
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                            while ((bytesRead = zipInputStream.read(buf, 0, 1024)) > -1) {
                                outputStream.write(buf, 0, bytesRead);
                            }
                            listOfFiles.put(entryName, outputStream.toByteArray());
                            outputStream.close();
                        }
                        zipInputStream.closeEntry();
                    }
                    zipInputStream.close();
                    return listOfFiles;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
    
    
    protected ZipInputStream readZipFileFromHDFS(FileSystem fileSystem, Path path) throws Exception {
        if (!fileSystem.exists(path)) {
            throw new IllegalArgumentException(path.getName() + " does not exist");
        }
        FSDataInputStream fsInputStream = fileSystem.open(path);
        ZipInputStream zipInputStream = new ZipInputStream(fsInputStream);
        return zipInputStream;
    }
    
  2. from https://stackoverflow.com/questions/36180597/how-to-unzip-files-stored-in-hdfs-using-java-without-first-copying-to-the-local by cc-by-sa and MIT license