gpt4 book ai didi

java - 如何在 Hadoop 中自定义 Writable 类?

转载 作者:搜寻专家 更新时间:2023-10-30 19:41:45 25 4
gpt4 key购买 nike

我正在尝试实现可写类,但如果在我的类中有嵌套对象(例如列表等),我不知道如何实现可写类。有人可以帮助我吗?谢谢

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;
}

}

以下是 Field 类:

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;
}

}

最佳答案

序列化集合非常简单。

@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);
}
}

字段也必须实现Writable

关于java - 如何在 Hadoop 中自定义 Writable 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7994438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com