Search in sources :

Example 6 with LazyMap

use of org.apache.hadoop.hive.serde2.lazy.LazyMap in project presto by prestodb.

the class TestLazyMap method assertMapDecode.

public static void assertMapDecode(String encodedMap, Map<? extends Object, ? extends Object> expectedMap) {
    LazyMap lazyMap = (LazyMap) createLazyObject(getLazySimpleMapObjectInspector(LAZY_STRING_OBJECT_INSPECTOR, getLazyStringObjectInspector(false, (byte) 0), (byte) 2, (byte) 3, new Text("\\N"), false, (byte) 0));
    lazyMap.init(newByteArrayRef(encodedMap), 0, encodedMap.length());
    Map<Object, Object> map = lazyMap.getMap();
    assertEquals(map, expectedMap);
}
Also used : LazyMap(org.apache.hadoop.hive.serde2.lazy.LazyMap) Text(org.apache.hadoop.io.Text) LazyFactory.createLazyObject(org.apache.hadoop.hive.serde2.lazy.LazyFactory.createLazyObject)

Example 7 with LazyMap

use of org.apache.hadoop.hive.serde2.lazy.LazyMap in project hive by apache.

the class AvroLazyObjectInspector method getStructFieldData.

@SuppressWarnings("unchecked")
@Override
public Object getStructFieldData(Object data, StructField f) {
    if (data == null) {
        return null;
    }
    int fieldID = f.getFieldID();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Getting struct field data for field: [" + f.getFieldName() + "] on data [" + data.getClass() + "]");
    }
    if (data instanceof LazyStruct) {
        LazyStruct row = (LazyStruct) data;
        // get the field out of struct
        Object rowField = row.getField(fieldID);
        if (rowField instanceof LazyStruct) {
            if (LOG.isDebugEnabled() && rowField != null) {
                LOG.debug("Deserializing struct [" + rowField.getClass() + "]");
            }
            return deserializeStruct(rowField, f.getFieldName());
        } else if (rowField instanceof LazyMap) {
            // We have found a map. Systematically deserialize the values of the map and return back the
            // map
            LazyMap lazyMap = (LazyMap) rowField;
            for (Entry<Object, Object> entry : lazyMap.getMap().entrySet()) {
                Object _key = entry.getKey();
                Object _value = entry.getValue();
                if (_value instanceof LazyStruct) {
                    lazyMap.getMap().put(_key, deserializeStruct(_value, f.getFieldName()));
                }
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Returning a lazy map for field [" + f.getFieldName() + "]");
            }
            return lazyMap;
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Returning [" + rowField + "] for field [" + f.getFieldName() + "]");
            }
            // Just return the object. We need no further operation on it
            return rowField;
        }
    } else {
        // hive can operate on. Here we should be getting the same object back.
        if (!(data instanceof List)) {
            throw new IllegalArgumentException("data should be an instance of list");
        }
        if (!(fieldID < ((List<Object>) data).size())) {
            return null;
        }
        // lookup the field corresponding to the given field ID and return
        Object field = ((List<Object>) data).get(fieldID);
        if (field == null) {
            return null;
        }
        // convert to a lazy object and return
        return toLazyObject(field, f.getFieldObjectInspector());
    }
}
Also used : Entry(java.util.Map.Entry) LazyMap(org.apache.hadoop.hive.serde2.lazy.LazyMap) LazyObject(org.apache.hadoop.hive.serde2.lazy.LazyObject) ArrayList(java.util.ArrayList) List(java.util.List) LazyStruct(org.apache.hadoop.hive.serde2.lazy.LazyStruct)

Example 8 with LazyMap

use of org.apache.hadoop.hive.serde2.lazy.LazyMap in project hive by apache.

the class TestHBaseSerDe method deserializeAndSerializeHiveMapHBaseColumnFamily.

private void deserializeAndSerializeHiveMapHBaseColumnFamily(HBaseSerDe hbaseSerDe, Result[] r, Put[] p, Object[][] expectedData, byte[][] rowKeys, byte[][] columnFamilies, byte[][][] columnQualifiersAndValues) throws SerDeException {
    StructObjectInspector soi = (StructObjectInspector) hbaseSerDe.getObjectInspector();
    List<? extends StructField> fieldRefs = soi.getAllStructFieldRefs();
    assertEquals(8, fieldRefs.size());
    // Deserialize
    for (int i = 0; i < r.length; i++) {
        Object row = hbaseSerDe.deserialize(new ResultWritable(r[i]));
        Put serializedPut = ((PutWritable) hbaseSerDe.serialize(row, soi)).getPut();
        byte[] rowKey = serializedPut.getRow();
        for (int k = 0; k < rowKey.length; k++) {
            assertEquals(rowKey[k], rowKeys[i][k]);
        }
        assertEquals(columnFamilies.length, serializedPut.numFamilies());
        for (int j = 0; j < fieldRefs.size(); j++) {
            Object fieldData = soi.getStructFieldData(row, fieldRefs.get(j));
            assertNotNull(fieldData);
            if (fieldData instanceof LazyPrimitive<?, ?>) {
                assertEquals(expectedData[i][j], ((LazyPrimitive<?, ?>) fieldData).getWritableObject());
            } else if (fieldData instanceof LazyHBaseCellMap) {
                LazyPrimitive<?, ?> lazyPrimitive = (LazyPrimitive<?, ?>) ((LazyHBaseCellMap) fieldData).getMapValueElement(expectedData[i][j]);
                assertEquals(expectedData[i][j], lazyPrimitive.getWritableObject());
            } else {
                fail("Error: field data not an instance of LazyPrimitive<?,?> or LazyMap");
            }
        }
    }
}
Also used : LazyPrimitive(org.apache.hadoop.hive.serde2.lazy.LazyPrimitive) Put(org.apache.hadoop.hbase.client.Put) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 9 with LazyMap

use of org.apache.hadoop.hive.serde2.lazy.LazyMap in project hive by apache.

the class TestLazyHBaseObject method testLazyHBaseCellMap1.

/**
   * Test the LazyMap class with Integer-to-String.
   * @throws SerDeException
   */
public void testLazyHBaseCellMap1() throws SerDeException {
    // Map of Integer to String
    Text nullSequence = new Text("\\N");
    ObjectInspector oi = LazyFactory.createLazyObjectInspector(TypeInfoUtils.getTypeInfosFromTypeString("map<int,string>").get(0), new byte[] { (byte) 1, (byte) 2 }, 0, nullSequence, false, (byte) 0);
    LazyHBaseCellMap b = new LazyHBaseCellMap((LazyMapObjectInspector) oi);
    // Initialize a result
    List<KeyValue> kvs = new ArrayList<KeyValue>();
    kvs.add(new KeyValue(Bytes.toBytes("test-row"), Bytes.toBytes("cfa"), Bytes.toBytes("col1"), Bytes.toBytes("cfacol1")));
    kvs.add(new KeyValue(Bytes.toBytes("test-row"), Bytes.toBytes("cfa"), Bytes.toBytes("col2"), Bytes.toBytes("cfacol2")));
    kvs.add(new KeyValue(Bytes.toBytes("test-row"), Bytes.toBytes("cfb"), Bytes.toBytes("2"), Bytes.toBytes("def")));
    kvs.add(new KeyValue(Bytes.toBytes("test-row"), Bytes.toBytes("cfb"), Bytes.toBytes("-1"), Bytes.toBytes("")));
    kvs.add(new KeyValue(Bytes.toBytes("test-row"), Bytes.toBytes("cfb"), Bytes.toBytes("0"), Bytes.toBytes("0")));
    kvs.add(new KeyValue(Bytes.toBytes("test-row"), Bytes.toBytes("cfb"), Bytes.toBytes("8"), Bytes.toBytes("abc")));
    kvs.add(new KeyValue(Bytes.toBytes("test-row"), Bytes.toBytes("cfc"), Bytes.toBytes("col3"), Bytes.toBytes("cfccol3")));
    Result r = new Result(kvs);
    List<Boolean> mapBinaryStorage = new ArrayList<Boolean>();
    mapBinaryStorage.add(false);
    mapBinaryStorage.add(false);
    b.init(r, "cfb".getBytes(), mapBinaryStorage);
    assertEquals(new Text("def"), ((LazyString) b.getMapValueElement(new IntWritable(2))).getWritableObject());
    assertNull(b.getMapValueElement(new IntWritable(-1)));
    assertEquals(new Text("0"), ((LazyString) b.getMapValueElement(new IntWritable(0))).getWritableObject());
    assertEquals(new Text("abc"), ((LazyString) b.getMapValueElement(new IntWritable(8))).getWritableObject());
    assertNull(b.getMapValueElement(new IntWritable(12345)));
    assertEquals("{0:'0',2:'def',8:'abc'}".replace('\'', '\"'), SerDeUtils.getJSONString(b, oi));
}
Also used : LazySimpleStructObjectInspector(org.apache.hadoop.hive.serde2.lazy.objectinspector.LazySimpleStructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) LazyMapObjectInspector(org.apache.hadoop.hive.serde2.lazy.objectinspector.LazyMapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) KeyValue(org.apache.hadoop.hbase.KeyValue) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) IntWritable(org.apache.hadoop.io.IntWritable) Result(org.apache.hadoop.hbase.client.Result)

Example 10 with LazyMap

use of org.apache.hadoop.hive.serde2.lazy.LazyMap in project presto by prestodb.

the class RcFileTester method decodeRecordReaderValue.

private static Object decodeRecordReaderValue(Type type, Object actualValue) {
    if (actualValue instanceof LazyPrimitive) {
        actualValue = ((LazyPrimitive<?, ?>) actualValue).getWritableObject();
    }
    if (actualValue instanceof BooleanWritable) {
        actualValue = ((BooleanWritable) actualValue).get();
    } else if (actualValue instanceof ByteWritable) {
        actualValue = ((ByteWritable) actualValue).get();
    } else if (actualValue instanceof BytesWritable) {
        actualValue = new SqlVarbinary(((BytesWritable) actualValue).copyBytes());
    } else if (actualValue instanceof DateWritable) {
        actualValue = new SqlDate(((DateWritable) actualValue).getDays());
    } else if (actualValue instanceof DoubleWritable) {
        actualValue = ((DoubleWritable) actualValue).get();
    } else if (actualValue instanceof FloatWritable) {
        actualValue = ((FloatWritable) actualValue).get();
    } else if (actualValue instanceof IntWritable) {
        actualValue = ((IntWritable) actualValue).get();
    } else if (actualValue instanceof LongWritable) {
        actualValue = ((LongWritable) actualValue).get();
    } else if (actualValue instanceof ShortWritable) {
        actualValue = ((ShortWritable) actualValue).get();
    } else if (actualValue instanceof HiveDecimalWritable) {
        DecimalType decimalType = (DecimalType) type;
        HiveDecimalWritable writable = (HiveDecimalWritable) actualValue;
        // writable messes with the scale so rescale the values to the Presto type
        BigInteger rescaledValue = rescale(writable.getHiveDecimal().unscaledValue(), writable.getScale(), decimalType.getScale());
        actualValue = new SqlDecimal(rescaledValue, decimalType.getPrecision(), decimalType.getScale());
    } else if (actualValue instanceof Text) {
        actualValue = actualValue.toString();
    } else if (actualValue instanceof TimestampWritable) {
        TimestampWritable timestamp = (TimestampWritable) actualValue;
        actualValue = new SqlTimestamp((timestamp.getSeconds() * 1000) + (timestamp.getNanos() / 1000000L), UTC_KEY);
    } else if (actualValue instanceof StructObject) {
        StructObject structObject = (StructObject) actualValue;
        actualValue = decodeRecordReaderStruct(type, structObject.getFieldsAsList());
    } else if (actualValue instanceof LazyBinaryArray) {
        actualValue = decodeRecordReaderList(type, ((LazyBinaryArray) actualValue).getList());
    } else if (actualValue instanceof LazyBinaryMap) {
        actualValue = decodeRecordReaderMap(type, ((LazyBinaryMap) actualValue).getMap());
    } else if (actualValue instanceof LazyArray) {
        actualValue = decodeRecordReaderList(type, ((LazyArray) actualValue).getList());
    } else if (actualValue instanceof LazyMap) {
        actualValue = decodeRecordReaderMap(type, ((LazyMap) actualValue).getMap());
    } else if (actualValue instanceof List) {
        actualValue = decodeRecordReaderList(type, ((List<?>) actualValue));
    }
    return actualValue;
}
Also used : SqlVarbinary(com.facebook.presto.spi.type.SqlVarbinary) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) LazyBinaryArray(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryArray) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) LazyPrimitive(org.apache.hadoop.hive.serde2.lazy.LazyPrimitive) StructObject(org.apache.hadoop.hive.serde2.StructObject) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) LazyMap(org.apache.hadoop.hive.serde2.lazy.LazyMap) BytesWritable(org.apache.hadoop.io.BytesWritable) SqlDecimal(com.facebook.presto.spi.type.SqlDecimal) Text(org.apache.hadoop.io.Text) FloatWritable(org.apache.hadoop.io.FloatWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) SqlDate(com.facebook.presto.spi.type.SqlDate) DecimalType(com.facebook.presto.spi.type.DecimalType) BigInteger(java.math.BigInteger) LazyArray(org.apache.hadoop.hive.serde2.lazy.LazyArray) LazyBinaryMap(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryMap)

Aggregations

LazyMap (org.apache.hadoop.hive.serde2.lazy.LazyMap)7 Text (org.apache.hadoop.io.Text)5 ArrayList (java.util.ArrayList)4 LazyMapObjectInspector (org.apache.hadoop.hive.serde2.lazy.objectinspector.LazyMapObjectInspector)4 LazySimpleStructObjectInspector (org.apache.hadoop.hive.serde2.lazy.objectinspector.LazySimpleStructObjectInspector)4 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)4 IntWritable (org.apache.hadoop.io.IntWritable)4 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 Configuration (org.apache.hadoop.conf.Configuration)3 LazyPrimitive (org.apache.hadoop.hive.serde2.lazy.LazyPrimitive)3 LazyStruct (org.apache.hadoop.hive.serde2.lazy.LazyStruct)3 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Properties (java.util.Properties)2 KeyValue (org.apache.hadoop.hbase.KeyValue)2 Result (org.apache.hadoop.hbase.client.Result)2 AccumuloHiveRow (org.apache.hadoop.hive.accumulo.AccumuloHiveRow)2 LazyAccumuloRow (org.apache.hadoop.hive.accumulo.LazyAccumuloRow)2