如何在Hadoop中定制Writable类? [英] How to customize Writable class in Hadoop?
本文介绍了如何在Hadoop中定制Writable类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图实现Writable类,但我不知道如何实现一个可写类,如果在我的类中有嵌套对象,如列表等。可以任何身体帮助我吗?谢谢
public class StorageClass implements Writable {
public String xStr;
public String yStr;
公共列表< Field> sStor
//省略ctors
@override
public void write(DataOutput out)throws IOException {
out.writeChars(xStr );
out.WriteChars(yStr);
//我应该做些什么List< Field>
$ b @override
public void readFields(DataInput in)throws IOException {
xStr = in.readLine();
yStr = in.readLine();
//我应该做些什么List< Field>
}
}
公共类SubStorage {
public String x;
public String y;
}
}
以下是Field类:
public final class Field implements Comparable< Field>,Serializable {
private String name;
private DataType dataType;
私有对象值;
private FieldType fieldType;
public Field(){
}
public Field(String name,DataType dataType ,FieldType fieldType){
this(name,dataType,null,fieldType);
$ b $ public Field(String name,DataType type,Object value,FieldType fieldType){
this.name = name;
this.dataType = type;
this.value = value;
this.fieldType = fieldType;
$ b public enum FieldType {
PRI,LOOKUP,SCD,VERSION,其他
public enum DataType {
UNDEFINED(4){
public int getSizeInBytes(Object value) {
return STRING.getSizeInBytes(value);
STRING(4){
public int getSizeInBytes(Object value){
if(value == null){
返回0;
}
return super.getSizeInBytes(value)+(value.toString()。length()* 2); (8),
(8),
)DATETIME(8),
BOOLEAN(1),
BYTE(1),
FLOAT(4),
SHORT(2),
CHAR(2) ,
DATE(8),
TIME(8),
BLOB(0){
public int getSizeInBytes(Object value){
if(值== null){
return 0;
}
return((byte [])value).length;
}
};
private final int sizeInBytes;
private DataType(int sizeInBytes){
this.sizeInBytes = sizeInBytes;
}
public int getSizeInBytes(Object value){
return sizeInBytes;
}
}
解决方案
序列化集合非常简单。
@Override
public void readFields(DataInput in)throws IOException { b $ b int size = in.readInt();
list = new ArrayList< Field>(size);
for(int i = 0; i< size; i ++){
Field f = new Field();
f.readFields(in);
list.add(f);
}
}
@Override
public void write(DataOutput out)throws IOException {
out.writeInt(list.size());
for(Field l:list){
l.write(out);
$ b $ p $字段必须实现可写还有
。
I'm trying to implement Writable class, but i have no idea on how to implement a writable class if in my class there is nested object, such as list, etc. Could any body help me? thanks
public class StorageClass implements Writable{
public String xStr;
public String yStr;
public List<Field> sStor
//omitted ctors
@override
public void write(DataOutput out) throws IOException{
out.writeChars(xStr);
out.WriteChars(yStr);
//WHAT SHOULD I DO FOR List<Field>
}
@override
public void readFields(DataInput in) throws IOException{
xStr = in.readLine();
yStr = in.readLine();
//WHAT SHOULD I DO FOR List<Field>
}
}
public class SubStorage{
public String x;
public String y;
}
}
Following is the Field class:
public final class Field implements Comparable<Field>, Serializable {
private String name;
private DataType dataType;
private Object value;
private FieldType fieldType;
public Field(){
}
public Field(String name, DataType dataType, FieldType fieldType){
this(name, dataType, null, fieldType);
}
public Field(String name, DataType type, Object value, FieldType fieldType){
this.name = name;
this.dataType = type;
this.value = value;
this.fieldType = fieldType;
}
}
public enum FieldType {
PRI, LOOKUP, SCD, VERSION, OTHER
}
public enum DataType {
UNDEFINED(4) {
public int getSizeInBytes(Object value) {
return STRING.getSizeInBytes(value);
}
},
STRING(4) {
public int getSizeInBytes(Object value) {
if (value == null) {
return 0;
}
return super.getSizeInBytes(value) + (value.toString().length() * 2); // length + chars
}
},
INT(4),
LONG(8),
DOUBLE(8),
DATETIME(8),
BOOLEAN(1),
BYTE(1),
FLOAT(4),
SHORT(2),
CHAR(2),
DATE(8),
TIME(8),
BLOB(0) {
public int getSizeInBytes(Object value) {
if (value == null) {
return 0;
}
return ((byte[])value).length;
}
};
private final int sizeInBytes;
private DataType(int sizeInBytes) {
this.sizeInBytes = sizeInBytes;
}
public int getSizeInBytes(Object value) {
return sizeInBytes;
}
}
解决方案 Serializing collections is quite simple.
@Override
public void readFields(DataInput in) throws IOException {
int size = in.readInt();
list= new ArrayList<Field>(size);
for(int i = 0; i < size; i++){
Field f = new Field();
f.readFields(in);
list.add(f);
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(list.size());
for (Field l : list) {
l.write(out);
}
}
Field has to implement Writable
as well.
这篇关于如何在Hadoop中定制Writable类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文