복붙노트

[HADOOP] HDFS 디렉토리의 파일 수

HADOOP

HDFS 디렉토리의 파일 수

Java 코드에서 HDFS의 디렉토리에 연결하고 해당 디렉토리의 파일 수를 확인하고 이름을 가져 와서 읽고 싶습니다. 이미 파일을 읽을 수는 있지만 디렉토리의 파일 수를 계산하고 일반 디렉토리와 같은 파일 이름을 얻는 방법을 파악할 수는 없습니다.

읽을 때 DFSClient를 사용하고 InputStream에 파일을 엽니 다.

해결법

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

    1.카운트

    카운트

    Usage: hadoop fs -count [-q] <paths>
    

    지정된 파일 패턴과 일치하는 경로 아래의 디렉토리, 파일 및 바이트의 수를 센다. 출력 열은 다음과 같습니다. DIR_COUNT 개, FILE_COUNT 개, CONTENT_SIZE 개 FILE_NAME

    -q가있는 출력 열은 다음과 같습니다. QUOTA, REMAINING_QUATA, SPACE_QUOTA, REMAINING_SPACE_QUOTA, DIR_COUNT, FILE_COUNT, CONTENT_SIZE, FILE_NAME.

    예:

    hadoop fs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
    hadoop fs -count -q hdfs://nn1.example.com/file1
    

    종료 코드 :

    성공시 0을 반환하고 오류시 -1을 반환합니다.

    FileSystem을 사용하고 경로 안의 파일을 반복 할 수 있습니다. 다음은 몇 가지 예제 코드입니다.

    int count = 0;
    FileSystem fs = FileSystem.get(getConf());
    boolean recursive = false;
    RemoteIterator<LocatedFileStatus> ri = fs.listFiles(new Path("hdfs://my/path"), recursive);
    while (ri.hasNext()){
        count++;
        ri.next();
    }
    
  2. ==============================

    2.

    FileSystem fs = FileSystem.get(conf);
    Path pt = new Path("/path");
    ContentSummary cs = fs.getContentSummary(pt);
    long fileCount = cs.getFileCount();
    
  3. ==============================

    3.빠르고 간단한 계산을하려면 다음 한 줄짜리 시도해보십시오.

    빠르고 간단한 계산을하려면 다음 한 줄짜리 시도해보십시오.

    hdfs dfs -ls -R /path/to/your/directory/ | grep -E '^-' | wc -l
    

    간략한 설명 :

    grep -E '^ -'또는 egrep '^ -': Grep all files : 파일은 '-'로 시작하고 폴더는 'd'로 시작합니다.

    wc -l : 줄 수.

  4. ==============================

    4.명령 줄에서 다음과 같이 할 수 있습니다.

    명령 줄에서 다음과 같이 할 수 있습니다.

     hdfs dfs -ls $parentdirectory | awk '{system("hdfs dfs -count " $6) }'
    
  5. ==============================

    5.hadoop fs -du [-s] [-h] [-x] URI [URI ...]

    hadoop fs -du [-s] [-h] [-x] URI [URI ...]

    지정된 디렉토리에 포함 된 파일 및 디렉토리의 크기 또는 해당 파일의 경우 파일 길이를 표시합니다.

    옵션 :

    The -s option will result in an aggregate summary of file lengths being displayed, rather than the individual files. Without the -s option, calculation is done by going 1-level deep from the given path.
    The -h option will format file sizes in a “human-readable” fashion (e.g 64.0m instead of 67108864)
    The -x option will exclude snapshots from the result calculation. Without the -x option (default), the result is always calculated from all INodes, including all snapshots under the given path.
    
  6. from https://stackoverflow.com/questions/20381422/file-count-in-an-hdfs-directory by cc-by-sa and MIT license