2014-07-09 6 views
2

saveAsTextFile(path)을 사용할 때 어떤 UTF를 사용해야하는지 어떻게 알 수 있습니까? 물론 모든 문자열이 UTF-8 인 것으로 알려져 있다면 디스크 공간을 2 배 절약 할 수 있습니다! (기본 UTF가 Java와 같이 16이라고 가정)제어 구성 집합 Apache Spark saveAsTextFile로 쓰기위한 UTF 인코딩

+0

에서 바이트를 얻을 수 있습니다,'saveAsTextFile' 파일 이름을 제외하고 매개 변수를 사용하지 않는 – aaronman

답변

3

saveAsTextFile 실제로는 UTF-8로 인코딩 된 hadoop에서 Text을 사용합니다. Text.java에서

def saveAsTextFile(path: String, codec: Class[_ <: CompressionCodec]) { 
    this.map(x => (NullWritable.get(), new Text(x.toString))) 
     .saveAsHadoopFile[TextOutputFormat[NullWritable, Text]](path, codec) 
    } 

:

public class Text extends BinaryComparable 
    implements WritableComparable<BinaryComparable> { 

    static final int SHORT_STRING_MAX = 1024 * 1024; 

    private static ThreadLocal<CharsetEncoder> ENCODER_FACTORY = 
    new ThreadLocal<CharsetEncoder>() { 
     protected CharsetEncoder initialValue() { 
     return Charset.forName("UTF-8").newEncoder(). 
       onMalformedInput(CodingErrorAction.REPORT). 
       onUnmappableCharacter(CodingErrorAction.REPORT); 
    } 
    }; 

    private static ThreadLocal<CharsetDecoder> DECODER_FACTORY = 
    new ThreadLocal<CharsetDecoder>() { 
    protected CharsetDecoder initialValue() { 
     return Charset.forName("UTF-8").newDecoder(). 
      onMalformedInput(CodingErrorAction.REPORT). 
      onUnmappableCharacter(CodingErrorAction.REPORT); 
    } 
    }; 

당신이 UTF-16 당신이 org.apache.hadoop.io.BytesWritablesaveAsHadoopFile를 사용하고 (당신이 말한대로하는 자바 String의 바이트를 얻을 수 있다고 생각로 저장하고 싶다면 의지 UTF-16이되어야 함). 이런 식으로 뭔가 :
saveAsHadoopFile[SequenceFileOutputFormat[NullWritable, BytesWritable]](path)
당신은 그냥 하둡 출력 형식을 사용하는 말을 "...".getBytes("UTF-16")