2013-12-18 3 views
0

Hadoop에 WritableComparable 클래스를 구현하는 클래스가 있습니다. 이 클래스는 short와 very long이라는 두 개의 문자열 변수를 가지고 있습니다. 나는이 변수들을 쓰려면 writeChars을 사용하고 그것들을 읽으려면 readLine을 사용한다. 그러나 나는 어떤 종류의 오류가있는 것처럼 보인다. Hadoop에서 이러한 긴 문자열을 직렬화하는 가장 좋은 방법은 무엇입니까?Hadoop에서 긴 문자열 직렬화

+0

''readline''이 각 끝나는 시점을 인식 할 수 있도록 각 문자열 변수의 끝 부분에 줄 종결자를 추가해야합니까? –

답변

0

여러분은 byteswritable을 사용하여 더욱 효율적으로 사용할 수 있다고 생각합니다. callId와 같이 BytesWritable 유형을 가진 아래의 맞춤 키를 확인하십시오.

당신은으로 호출 할 수 있습니다

CustomMRKey customKey을 = 새 - :

가에서 사용하려면

public class CustomMRKey implements WritableComparable<CustomMRKey> { 
private BytesWritable callId; 
private IntWritable mapperType; 

/** 
* @default constructor 
*/ 
public CustomMRKey() { 
    set(new BytesWritable(), new IntWritable()); 
} 

/** 
* Constructor 
* 
* @param callId 
* @param mapperType 
*/ 
public CustomMRKey(BytesWritable callId, IntWritable mapperType) { 
    set(callId, mapperType); 
} 

/** 
* sets the call id and mapper type 
* 
* @param callId 
* @param mapperType 
*/ 
public void set(BytesWritable callId, IntWritable mapperType) { 
    this.callId = callId; 
    this.mapperType = mapperType; 
} 

/** 
* This method returns the callId 
* 
* @return callId 
*/ 
public BytesWritable getCallId() { 
    return callId; 
} 

/** 
* This method sets the callId given a callId 
* 
* @param callId 
*/ 
public void setCallId(BytesWritable callId) { 
    this.callId = callId; 
} 

/** 
* This method returns the mapper type 
* 
* 
* @return 
*/ 
public IntWritable getMapperType() { 
    return mapperType; 
} 

/** 
* This method is set to store the mapper type 
* 
* @param mapperType 
*/ 
public void setMapperType(IntWritable mapperType) { 
    this.mapperType = mapperType; 
} 

@Override 
public void readFields(DataInput in) throws IOException { 
    callId.readFields(in); 
    mapperType.readFields(in); 

} 

@Override 
public void write(DataOutput out) throws IOException { 
    callId.write(out); 
    mapperType.write(out); 
} 

@Override 
public boolean equals(Object obj) { 
    if (obj instanceof CustomMRCdrKey) { 
     CustomMRCdrKey key = (CustomMRCdrKey) obj; 
     return callId.equals(key.callId) 
       && mapperType.equals(key.mapperType); 
    } 
    return false; 
} 

@Override 
public int compareTo(CustomMRCdrKey key) { 
    int cmp = callId.compareTo(key.getCallId()); 
    if (cmp != 0) { 
     return cmp; 
    } 
    return mapperType.compareTo(key.getMapperType()); 
} 

은} 매퍼 코드는 다음과 같은 당신이 뭔가를 사용 BytesWritable 형태의 키를 생성 할 수 있다고 말한다 CustomMRKey (새 BytesWritable(), 새 IntWritable()); customKey.setCallId (makeKey (value, this.resultKey)); customKey.setMapperType (this.mapTypeIndicator);

다음

makeKey 방법은 무엇인가 다음과 같다 : -이 도움이 될 수

public BytesWritable makeKey(Text value, BytesWritable key) throws IOException { 
    try { 
     ByteArrayOutputStream byteKey = new ByteArrayOutputStream(Constants.MR_DEFAULT_KEY_SIZE); 
     for (String field : keyFields) { 
      byte[] bytes = value.getString(field).getBytes(); 
      byteKey.write(bytes,0,bytes.length); 
     } 
     if(key==null){ 
      return new BytesWritable(byteKey.toByteArray()); 
     }else{ 
      key.set(byteKey.toByteArray(), 0, byteKey.size()); 
      return key; 
     } 
    } catch (Exception ex) { 
     throw new IOException("Could not generate key", ex); 
    } 
} 

희망.