hashmap和float[]上的hadoop序列化

zxlwwiss  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(450)

目前,我正在将普通java代码转换为 MapReduce 上的结构 Hadoop .
我想修改这个类 Graph 像下面一样,我仍然不知道如何序列化和反序列化 HashMap 以及 float[] 类型。代码部分如下。
代码:

  1. public class Graph implements WritableComparable, Cloneable {
  2. private static long serialVersionUID = 3L;
  3. public static int MAX_FREQUENCY = 3;
  4. public static float[] freqWeight = { 1.0F, 1.6F, 2.0F };
  5. int nNodes;
  6. int nEdges;
  7. String strName;
  8. HashMap<String, Node> nodes;
  9. boolean isMCS;
  10. String taxonomyName;
  11. @Override
  12. public void write(DataOutput out) throws IOException {
  13. // TODO Auto-generated method stub
  14. out.writeBoolean(isMCS);
  15. out.writeInt(nEdges);
  16. out.writeInt(nNodes);
  17. out.writeLong(serialVersionUID);
  18. out.writeInt(MAX_FREQUENCY);
  19. out.writeBytes(strName);
  20. out.writeBytes(taxonomyName);
  21. //ArrayWritable a=new ArrayWritable(FloatWritable.class);
  22. //HashMap
  23. //float[]
  24. }
  25. @Override
  26. public void readFields(DataInput in) throws IOException {
  27. // TODO Auto-generated method stub
  28. isMCS=in.readBoolean();
  29. nEdges=in.readInt();
  30. nNodes=in.readInt();
  31. serialVersionUID=in.readLong();
  32. MAX_FREQUENCY=in.readInt();
  33. strName=in.readLine();
  34. taxonomyName=in.readLine();
  35. //HashMap
  36. //float[]
  37. }
  38. @Override
  39. public int compareTo(Graph graph) {
  40. // TODO Auto-generated method stub
  41. return 0;
  42. }
  43. .........
mfpqipee

mfpqipee1#

使用arrayprimitivewritable:

  1. // float[] floats
  2. new ArrayPrimitiveWritable(floats).write(out);
  3. ArrayPrimitiveWritable apw = (float[]) new ArrayPrimitiveWritable().readFields(in);
  4. float[] floats = (float[]) apw.get();

对Map使用mapwritable。mapwritable基本上是一个键和值类型都是可写的Map。与ArrayPrimiveWritable类似,mapwritable实现readfiles()和write()方法进行序列化。

相关问题