@Test public void testMapWithArrayReadWrite() throws Exception { LinkedMapWritable written = new LinkedMapWritable(); ArrayWritable array = new WritableArrayWritable(Text.class); array.set(new Writable[] { new Text("one") , new Text("two"), new Text("three")} ); written.put(new Text("foo"), array); FastByteArrayOutputStream out = new FastByteArrayOutputStream(); DataOutputStream da = new DataOutputStream(out); written.write(da); da.close(); LinkedMapWritable read = new LinkedMapWritable(); read.readFields(new DataInputStream(new FastByteArrayInputStream(out.bytes()))); assertThat(read.size(), is(written.size())); assertThat(read.toString(), is(written.toString())); }
/** Default constructor. */ public LinkedMapWritable() { this.instance = new LinkedHashMap<Writable, Writable>(); addToMap(LinkedMapWritable.class); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void readFields(DataInput in) throws IOException { super.readFields(in); // First clear the map. Otherwise we will just accumulate // entries every time this method is called. this.instance.clear(); // Read the number of entries in the map int entries = in.readInt(); // Then read each key/value pair for (int i = 0; i < entries; i++) { Writable key = (Writable) ReflectionUtils.newInstance(getClass(in.readByte()), getConf()); key.readFields(in); Writable value = (Writable) ReflectionUtils.newInstance(getClass(in.readByte()), getConf()); value.readFields(in); instance.put(key, value); } }
@SuppressWarnings("rawtypes") @Override public Map createMap() { return new LinkedMapWritable(); }
@Test public void testMap() { LinkedMapWritable map = new LinkedMapWritable(); map.put(new Text("key"), new IntWritable(1)); map.put(new BooleanWritable(Boolean.TRUE), new ArrayWritable(new String[] { "one", "two" })); writableTypeToJson(map); }
@Override public int hashCode() { int h = 0; Iterator<Entry<Writable, Writable>> i = entrySet().iterator(); while (i.hasNext()) h += i.next().hashCode(); return h; }
/** {@inheritDoc} */ public void putAll(Map<? extends Writable, ? extends Writable> t) { for (Map.Entry<? extends Writable, ? extends Writable> e : t.entrySet()) { put(e.getKey(), e.getValue()); } }
/** * Copy constructor. * * @param other the map to copy from */ public LinkedMapWritable(MapWritable other) { this(); copy(other); }
/** {@inheritDoc} */ @Override public void write(DataOutput out) throws IOException { super.write(out); // Write out the number of entries in the map out.writeInt(instance.size()); // Then write out each key/value pair for (Map.Entry<Writable, Writable> e : instance.entrySet()) { out.writeByte(getId(e.getKey().getClass())); e.getKey().write(out); out.writeByte(getId(e.getValue().getClass())); e.getValue().write(out); } }
@Override public Map<Writable, Writable> createValue() { return (useLinkedMapWritable ? new LinkedMapWritable() : new MapWritable()); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void readFields(DataInput in) throws IOException { super.readFields(in); // First clear the map. Otherwise we will just accumulate // entries every time this method is called. this.instance.clear(); // Read the number of entries in the map int entries = in.readInt(); // Then read each key/value pair for (int i = 0; i < entries; i++) { Writable key = (Writable) ReflectionUtils.newInstance(getClass(in.readByte()), getConf()); key.readFields(in); Writable value = (Writable) ReflectionUtils.newInstance(getClass(in.readByte()), getConf()); value.readFields(in); instance.put(key, value); } }
Field[] fields = AnnotationUtils.filterDeepFields(t.getClass()); LinkedMapWritable linkedMapWritable = new LinkedMapWritable(); linkedMapWritable.put(new Text(AnnotationUtils.deepFieldName(field)), getLinkedMapWritableFromObject((IDeepType) object)); } else { linkedMapWritable .put(new Text(AnnotationUtils.deepFieldName(field)), getWritableFromObject(object));
@Override public String toString() { Iterator<Entry<Writable, Writable>> i = entrySet().iterator(); if (!i.hasNext()) return "{}"; StringBuilder sb = new StringBuilder(); sb.append('{'); for (;;) { Entry<Writable, Writable> e = i.next(); Writable key = e.getKey(); Writable value = e.getValue(); sb.append(key == this ? "(this Map)" : key); sb.append('='); if (value instanceof ArrayWritable) { sb.append(Arrays.toString(((ArrayWritable) value).get())); } else { sb.append(value == this ? "(this Map)" : value); } if (!i.hasNext()) return sb.append('}').toString(); sb.append(", "); } } }
/** {@inheritDoc} */ public void putAll(Map<? extends Writable, ? extends Writable> t) { for (Map.Entry<? extends Writable, ? extends Writable> e : t.entrySet()) { put(e.getKey(), e.getValue()); } }
/** * Copy constructor. * * @param other the map to copy from */ public LinkedMapWritable(MapWritable other) { this(); copy(other); }
/** {@inheritDoc} */ @Override public void write(DataOutput out) throws IOException { super.write(out); // Write out the number of entries in the map out.writeInt(instance.size()); // Then write out each key/value pair for (Map.Entry<Writable, Writable> e : instance.entrySet()) { out.writeByte(getId(e.getKey().getClass())); e.getKey().write(out); out.writeByte(getId(e.getValue().getClass())); e.getValue().write(out); } }