use of org.apache.hadoop.io.ArrayWritable in project hive by apache.
the class AbstractParquetMapInspector method getMap.
@Override
public Map<?, ?> getMap(final Object data) {
if (data == null) {
return null;
}
if (data instanceof ArrayWritable) {
final Writable[] mapArray = ((ArrayWritable) data).get();
if (mapArray == null) {
return null;
}
final Map<Writable, Writable> map = new LinkedHashMap<Writable, Writable>();
for (final Writable obj : mapArray) {
final ArrayWritable mapObj = (ArrayWritable) obj;
final Writable[] arr = mapObj.get();
map.put(arr[0], arr[1]);
}
return map;
}
if (data instanceof Map) {
return (Map) data;
}
throw new UnsupportedOperationException("Cannot inspect " + data.getClass().getCanonicalName());
}
use of org.apache.hadoop.io.ArrayWritable in project mongo-hadoop by mongodb.
the class BSONWritable method toBSON.
/**
* Unwrap a (usually Writable) Object, getting back a value suitable for
* putting into a BSONObject. If the given object is not Writable, then
* simply return the Object back.
*
* @param x the Object to turn into BSON.
* @return the BSON representation of the Object.
*/
@SuppressWarnings("unchecked")
public static Object toBSON(final Object x) {
if (x == null) {
return null;
}
if (x instanceof Text) {
return x.toString();
}
if (x instanceof BSONWritable) {
return ((BSONWritable) x).getDoc();
}
if (x instanceof Writable) {
if (x instanceof AbstractMapWritable) {
if (!(x instanceof Map)) {
throw new IllegalArgumentException(String.format("Cannot turn %s into BSON, since it does " + "not implement java.util.Map.", x.getClass().getName()));
}
Map<Writable, Writable> map = (Map<Writable, Writable>) x;
BasicBSONObject bson = new BasicBSONObject();
for (Map.Entry<Writable, Writable> entry : map.entrySet()) {
bson.put(entry.getKey().toString(), toBSON(entry.getValue()));
}
return bson;
}
if (x instanceof ArrayWritable) {
Writable[] o = ((ArrayWritable) x).get();
Object[] a = new Object[o.length];
for (int i = 0; i < o.length; i++) {
a[i] = toBSON(o[i]);
}
return a;
}
if (x instanceof NullWritable) {
return null;
}
if (x instanceof BooleanWritable) {
return ((BooleanWritable) x).get();
}
if (x instanceof BytesWritable) {
return ((BytesWritable) x).getBytes();
}
if (x instanceof ByteWritable) {
return ((ByteWritable) x).get();
}
if (x instanceof DoubleWritable) {
return ((DoubleWritable) x).get();
}
if (x instanceof FloatWritable) {
return ((FloatWritable) x).get();
}
if (x instanceof LongWritable) {
return ((LongWritable) x).get();
}
if (x instanceof IntWritable) {
return ((IntWritable) x).get();
}
// TODO - Support counters
}
return x;
}
use of org.apache.hadoop.io.ArrayWritable in project mongo-hadoop by mongodb.
the class BSONWritableTest method testToBSON.
@Test
public void testToBSON() {
assertEquals(null, toBSON(null));
assertEquals(null, toBSON(NullWritable.get()));
assertEquals("hello", toBSON(new Text("hello")));
DBObject obj = new BasicDBObject("hello", "world");
assertEquals(obj, toBSON(new BSONWritable(obj)));
final BasicBSONObject bsonResult = new BasicBSONObject("one", 1);
SortedMapWritable smw = new SortedMapWritable();
smw.put(new Text("one"), new IntWritable(1));
assertEquals(bsonResult, toBSON(smw));
MapWritable mw = new MapWritable();
mw.put(new Text("one"), new IntWritable(1));
assertEquals(bsonResult, toBSON(mw));
String[] expectedObjects = new String[] { "one", "two" };
Writable[] writableObjects = new Writable[] { new Text("one"), new Text("two") };
ArrayWritable aw = new ArrayWritable(Text.class, writableObjects);
Object[] actual = (Object[]) toBSON(aw);
assertTrue(Arrays.equals(expectedObjects, actual));
assertEquals(false, toBSON(new BooleanWritable(false)));
byte[] bytes = new byte[] { '0', '1', '2' };
assertEquals(bytes, toBSON(new BytesWritable(bytes)));
byte b = (byte) 'c';
assertEquals(b, toBSON(new ByteWritable(b)));
assertEquals(3.14159, toBSON(new DoubleWritable(3.14159)));
assertEquals(3.14159f, toBSON(new FloatWritable(3.14159f)));
assertEquals(42L, toBSON(new LongWritable(42L)));
assertEquals(42, toBSON(new IntWritable(42)));
// Catchall
assertEquals("hi", toBSON("hi"));
}
use of org.apache.hadoop.io.ArrayWritable in project hadoop by apache.
the class TypedBytesWritableOutput method writeArray.
public void writeArray(ArrayWritable aw) throws IOException {
Writable[] writables = aw.get();
out.writeVectorHeader(writables.length);
for (Writable writable : writables) {
write(writable);
}
}
use of org.apache.hadoop.io.ArrayWritable in project carbondata by apache.
the class CarbonHiveSerDe method createStruct.
public ArrayWritable createStruct(Object obj, StructObjectInspector inspector) throws SerDeException {
List fields = inspector.getAllStructFieldRefs();
Writable[] arr = new Writable[fields.size()];
for (int i = 0; i < fields.size(); i++) {
StructField field = (StructField) fields.get(i);
Object subObj = inspector.getStructFieldData(obj, field);
ObjectInspector subInspector = field.getFieldObjectInspector();
arr[i] = createObject(subObj, subInspector);
}
return new ArrayWritable(Writable.class, arr);
}
Aggregations