Java Code Examples for org.apache.hadoop.hive.serde2.lazy.LazyPrimitive

The following examples show how to use org.apache.hadoop.hive.serde2.lazy.LazyPrimitive. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Hive-Cassandra   Source File: LazyCassandraCellMap.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get the value in the map for the given key.
 *
 * @param key
 * @return
 */

@Override
public Object getMapValueElement(Object key) {
  if (!getParsed()) {
    parse();
  }

  for (Map.Entry<Object, Object> entry : cachedMap.entrySet()) {
    LazyPrimitive<?, ?> lazyKeyI = (LazyPrimitive<?, ?>) entry.getKey();
    // getWritableObject() will convert LazyPrimitive to actual primitive
    // writable objects.
    Object keyI = lazyKeyI.getWritableObject();
    if (keyI != null) {
      if (keyI.equals(key)) {
        // Got a match, return the value
        LazyObject v = (LazyObject) entry.getValue();
        return v == null ? v : v.getObject();
      }
    }
  }

  return null;
}
 
Example 2
/**
 * Send to the generator, the value of the cell, using column type
 * 
 * @param value The attribute value as the object given by Hive
 * @param fieldIndex column index of field in row
 * @param jsonGen JsonGenerator
 * @throws JsonProcessingException
 * @throws IOException
 */
private void generateJsonFromValue(Object value, int fieldIndex, JsonGenerator jsonGen)
	throws JsonProcessingException, IOException {
	String label = columnNames.get(fieldIndex);
	PrimitiveObjectInspector poi = (PrimitiveObjectInspector)this.columnOIs.get(fieldIndex);
	if (value == null) {
		jsonGen.writeObjectField(label, null);
	} else if (value instanceof LazyPrimitive<?,?>) {  // have seen LazyString, #25
		generateJsonFromLazy((LazyPrimitive<?,?>)value, fieldIndex, label, poi, jsonGen);
	} else if (value instanceof Writable) {
		generateJsonFromWritable((Writable)value, fieldIndex, label, poi, jsonGen);
	} else {  // SparkSQL, #97
		jsonGen.writeObjectField(label, value);
	}
}
 
Example 3
Source Project: presto   Source File: RcFileTester.java    License: Apache License 2.0 4 votes vote down vote up
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;
        if (SESSION.isLegacyTimestamp()) {
            actualValue = SqlTimestamp.legacyFromMillis(3, (timestamp.getSeconds() * 1000) + (timestamp.getNanos() / 1000000L), UTC_KEY);
        }
        else {
            actualValue = SqlTimestamp.fromMillis(3, (timestamp.getSeconds() * 1000) + (timestamp.getNanos() / 1000000L));
        }
    }
    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;
}
 
Example 4
private void generateJsonFromLazy(LazyPrimitive<?,?> value, int fieldIndex, String label,
								  PrimitiveObjectInspector poi, JsonGenerator jsonGen)
	throws JsonProcessingException, IOException {
	generateJsonFromWritable(value.getWritableObject(), fieldIndex, label, poi, jsonGen);
}