org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter Java Examples

The following examples show how to use org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter. 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 File: CacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public StructConverter(ObjectInspector inputOI, SettableStructObjectInspector outputOI) {
  if (inputOI instanceof StructObjectInspector) {
    this.inputOI = (StructObjectInspector) inputOI;
    this.outputOI = outputOI;
    inputFields = this.inputOI.getAllStructFieldRefs();
    outputFields = outputOI.getAllStructFieldRefs();

    // If the output has some extra fields, set them to NULL.
    int minFields = Math.min(inputFields.size(), outputFields.size());
    fieldConverters = new ArrayList<Converter>(minFields);
    for (int f = 0; f < minFields; f++) {
      fieldConverters.add(getConverter(inputFields.get(f).getFieldObjectInspector(),
          outputFields.get(f).getFieldObjectInspector()));
    }
  } else if (!(inputOI instanceof VoidObjectInspector)) {
    throw new UnsupportedOperationException(
        "Hive internal error: conversion of " + inputOI.getTypeName() + " to " + outputOI.getTypeName()
            + "not supported yet.");
  }
}
 
Example #2
Source File: TestCacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testCaching() {
  // Should return cached OIConverter on subsequent invocations
  CacheableObjectInspectorConverters cacheableObjectInspectorConverters = new CacheableObjectInspectorConverters();

  Converter c1 =
      cacheableObjectInspectorConverters.getConverter(writableStringObjectInspector, javaStringObjectInspector);
  Converter c2 =
      cacheableObjectInspectorConverters.getConverter(writableStringObjectInspector, javaStringObjectInspector);
  Assert.assertSame(c1, c2);

  MapConverter c3 = (MapConverter) cacheableObjectInspectorConverters.getConverter(
      ObjectInspectorFactory.getStandardMapObjectInspector(writableStringObjectInspector,
          writableStringObjectInspector),
      ObjectInspectorFactory.getStandardMapObjectInspector(javaStringObjectInspector, javaStringObjectInspector)
  );
  Assert.assertSame(c1, c3.keyConverter);
  Assert.assertSame(c1, c3.valueConverter);
}
 
Example #3
Source File: HiveData.java    From transport with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Object getUnderlyingDataForObjectInspector(ObjectInspector oi) {
  if (oi.equals(getUnderlyingObjectInspector())) {
    return getUnderlyingData();
  }

  Object result = getObjectFromCache(oi);
  if (result == null) {
    Converter c = ((HiveFactory) _stdFactory).getConverter(getUnderlyingObjectInspector(), oi);
    result = c.convert(getUnderlyingData());
    _cachedObjectsForObjectInspectors.putIfAbsent(oi, result);
  }
  return result;
}
 
Example #4
Source File: CacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns a converter that converts objects from one OI to another OI. The
 * returned (converted) object does not belong to the converter. Hence once convertor can be used
 * multiple times within one eval invocation.
 */
public Converter getConverter(ObjectInspector inputOI, ObjectInspector outputOI) {
  // If the inputOI is the same as the outputOI, just return an
  // IdentityConverter.
  if (inputOI.equals(outputOI)) {
    return new ObjectInspectorConverters.IdentityConverter();
  }
  Converter c = getConverterFromCache(inputOI, outputOI);
  if (c != null) {
    return c;
  }
  switch (outputOI.getCategory()) {
    case PRIMITIVE:
      return getConverter((PrimitiveObjectInspector) inputOI, (PrimitiveObjectInspector) outputOI);
    case STRUCT:
      c = new StructConverter(inputOI, (SettableStructObjectInspector) outputOI);
      break;
    case LIST:
      c = new ListConverter(inputOI, (SettableListObjectInspector) outputOI);
      break;
    case MAP:
      c = new MapConverter(inputOI, (SettableMapObjectInspector) outputOI);
      break;
    default:
      throw new UnsupportedOperationException(
          "Hive internal error: conversion of " + inputOI.getTypeName() + " to " + outputOI.getTypeName()
              + " not supported yet.");
  }
  cacheConverter(inputOI, outputOI, c);
  return c;
}
 
Example #5
Source File: TestCacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testReturnValue() {
  // Should create new return objects inside the converter
  CacheableObjectInspectorConverters cacheableObjectInspectorConverters = new CacheableObjectInspectorConverters();

  Converter c1 =
      cacheableObjectInspectorConverters.getConverter(javaStringObjectInspector, writableStringObjectInspector);
  String s1 = "Test_STR";
  Object o1 = c1.convert(s1);
  Object o2 = c1.convert(s1);
  Assert.assertNotSame(o1, o2);
}
 
Example #6
Source File: CacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private Converter getConverterFromCache(ObjectInspector inputOI, ObjectInspector outputOI) {
  return _cachedConverters.get(Pair.of(inputOI, outputOI));
}
 
Example #7
Source File: CacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void cacheConverter(ObjectInspector inputOI, ObjectInspector outputOI, Converter c) {
  _cachedConverters.putIfAbsent(Pair.of(inputOI, outputOI), c);
}
 
Example #8
Source File: CacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private Converter getConverter(PrimitiveObjectInspector inputOI, PrimitiveObjectInspector outputOI) {
  Converter c = getConverterFromCache(inputOI, outputOI);
  if (c != null) {
    return c;
  }
  switch (outputOI.getPrimitiveCategory()) {
    case BOOLEAN:
      c = new CacheablePrimitiveObjectInspectorConverter.BooleanConverter(inputOI,
          (SettableBooleanObjectInspector) outputOI);
      break;
    case BYTE:
      c = new CacheablePrimitiveObjectInspectorConverter.ByteConverter(inputOI,
          (SettableByteObjectInspector) outputOI);
      break;
    case SHORT:
      c = new CacheablePrimitiveObjectInspectorConverter.ShortConverter(inputOI,
          (SettableShortObjectInspector) outputOI);
      break;
    case INT:
      c = new CacheablePrimitiveObjectInspectorConverter.IntConverter(inputOI, (SettableIntObjectInspector) outputOI);
      break;
    case LONG:
      c = new CacheablePrimitiveObjectInspectorConverter.LongConverter(inputOI,
          (SettableLongObjectInspector) outputOI);
      break;
    case FLOAT:
      c = new CacheablePrimitiveObjectInspectorConverter.FloatConverter(inputOI,
          (SettableFloatObjectInspector) outputOI);
      break;
    case DOUBLE:
      c = new CacheablePrimitiveObjectInspectorConverter.DoubleConverter(inputOI,
          (SettableDoubleObjectInspector) outputOI);
      break;
    case STRING:
      if (outputOI instanceof WritableStringObjectInspector) {
        c = new CacheablePrimitiveObjectInspectorConverter.TextConverter(inputOI);
      } else if (outputOI instanceof JavaStringObjectInspector) {
        c = new CacheablePrimitiveObjectInspectorConverter.StringConverter(inputOI);
      }
      break;
    case CHAR:
      c = new CacheablePrimitiveObjectInspectorConverter.HiveCharConverter(inputOI,
          (SettableHiveCharObjectInspector) outputOI);
      break;
    case VARCHAR:
      c = new CacheablePrimitiveObjectInspectorConverter.HiveVarcharConverter(inputOI,
          (SettableHiveVarcharObjectInspector) outputOI);
      break;
    case DATE:
      c = new CacheablePrimitiveObjectInspectorConverter.DateConverter(inputOI,
          (SettableDateObjectInspector) outputOI);
      break;
    case TIMESTAMP:
      c = new CacheablePrimitiveObjectInspectorConverter.TimestampConverter(inputOI,
          (SettableTimestampObjectInspector) outputOI);
      break;
    case BINARY:
      c = new CacheablePrimitiveObjectInspectorConverter.BinaryConverter(inputOI,
          (SettableBinaryObjectInspector) outputOI);
      break;
    case DECIMAL:
      c = new CacheablePrimitiveObjectInspectorConverter.HiveDecimalConverter(inputOI,
          (SettableHiveDecimalObjectInspector) outputOI);
      break;
    default:
      throw new UnsupportedOperationException(
          "Hive internal error: conversion of " + inputOI.getTypeName() + " to " + outputOI.getTypeName()
              + " not supported yet.");
  }
  cacheConverter(inputOI, outputOI, c);
  return c;
}
 
Example #9
Source File: HiveFactory.java    From transport with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Converter getConverter(ObjectInspector inputOI, ObjectInspector outputOI) {
  return _converters.getConverter(inputOI, outputOI);
}
 
Example #10
Source File: TestCacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void testObjectInspectorConverters() throws Throwable {
  try {
    CacheableObjectInspectorConverters cacheableObjectInspectorConverters = new CacheableObjectInspectorConverters();

    // Boolean
    Converter booleanConverter =
        cacheableObjectInspectorConverters.getConverter(javaIntObjectInspector, writableBooleanObjectInspector);
    Assert.assertEquals(new BooleanWritable(false), booleanConverter.convert(Integer.valueOf(0)), "BooleanConverter");
    Assert.assertEquals(new BooleanWritable(true), booleanConverter.convert(Integer.valueOf(1)), "BooleanConverter");
    Assert.assertEquals(null, booleanConverter.convert(null), "BooleanConverter");

    // Byte
    Converter byteConverter =
        cacheableObjectInspectorConverters.getConverter(javaIntObjectInspector, writableByteObjectInspector);
    Assert.assertEquals(new ByteWritable((byte) 0), byteConverter.convert(Integer.valueOf(0)), "ByteConverter");
    Assert.assertEquals(new ByteWritable((byte) 1), byteConverter.convert(Integer.valueOf(1)), "ByteConverter");
    Assert.assertEquals(null, byteConverter.convert(null), "ByteConverter");

    // Short
    Converter shortConverter =
        cacheableObjectInspectorConverters.getConverter(javaIntObjectInspector, writableShortObjectInspector);
    Assert.assertEquals(new ShortWritable((short) 0), shortConverter.convert(Integer.valueOf(0)), "ShortConverter");
    Assert.assertEquals(new ShortWritable((short) 1), shortConverter.convert(Integer.valueOf(1)), "ShortConverter");
    Assert.assertEquals(null, shortConverter.convert(null), "ShortConverter");

    // Int
    Converter intConverter =
        cacheableObjectInspectorConverters.getConverter(javaIntObjectInspector, writableIntObjectInspector);
    Assert.assertEquals(new IntWritable(0), intConverter.convert(Integer.valueOf(0)), "IntConverter");
    Assert.assertEquals(new IntWritable(1), intConverter.convert(Integer.valueOf(1)), "IntConverter");
    Assert.assertEquals(null, intConverter.convert(null), "IntConverter");

    // Long
    Converter longConverter =
        cacheableObjectInspectorConverters.getConverter(javaIntObjectInspector, writableLongObjectInspector);
    Assert.assertEquals(new LongWritable(0), longConverter.convert(Integer.valueOf(0)), "LongConverter");
    Assert.assertEquals(new LongWritable(1), longConverter.convert(Integer.valueOf(1)), "LongConverter");
    Assert.assertEquals(null, longConverter.convert(null), "LongConverter");

    // Float
    Converter floatConverter =
        cacheableObjectInspectorConverters.getConverter(javaIntObjectInspector, writableFloatObjectInspector);
    Assert.assertEquals(new FloatWritable(0), floatConverter.convert(Integer.valueOf(0)), "FloatConverter");
    Assert.assertEquals(new FloatWritable(1), floatConverter.convert(Integer.valueOf(1)), "FloatConverter");
    Assert.assertEquals(null, floatConverter.convert(null), "FloatConverter");

    // Double
    Converter doubleConverter =
        cacheableObjectInspectorConverters.getConverter(javaIntObjectInspector, writableDoubleObjectInspector);
    Assert.assertEquals(new DoubleWritable(0), doubleConverter.convert(Integer.valueOf(0)), "DoubleConverter");
    Assert.assertEquals(new DoubleWritable(1), doubleConverter.convert(Integer.valueOf(1)), "DoubleConverter");
    Assert.assertEquals(null, doubleConverter.convert(null), "DoubleConverter");

    // Text
    Converter textConverter =
        cacheableObjectInspectorConverters.getConverter(javaIntObjectInspector, writableStringObjectInspector);
    Assert.assertEquals(new Text("0"), textConverter.convert(Integer.valueOf(0)), "TextConverter");
    Assert.assertEquals(new Text("1"), textConverter.convert(Integer.valueOf(1)), "TextConverter");
    Assert.assertEquals(null, textConverter.convert(null), "TextConverter");

    textConverter =
        cacheableObjectInspectorConverters.getConverter(writableBinaryObjectInspector, writableStringObjectInspector);
    Assert.assertEquals(new Text("hive"), textConverter
        .convert(new BytesWritable(new byte[]{(byte) 'h', (byte) 'i', (byte) 'v', (byte) 'e'})), "TextConverter");
    Assert.assertEquals(null, textConverter.convert(null), "TextConverter");

    textConverter =
        cacheableObjectInspectorConverters.getConverter(writableStringObjectInspector, writableStringObjectInspector);
    Assert.assertEquals(new Text("hive"), textConverter.convert(new Text("hive")), "TextConverter");
    Assert.assertEquals(null, textConverter.convert(null), "TextConverter");

    textConverter =
        cacheableObjectInspectorConverters.getConverter(javaStringObjectInspector, writableStringObjectInspector);
    Assert.assertEquals(new Text("hive"), textConverter.convert(new String("hive")), "TextConverter");
    Assert.assertEquals(null, textConverter.convert(null), "TextConverter");

    textConverter = cacheableObjectInspectorConverters.getConverter(javaHiveDecimalObjectInspector,
        writableStringObjectInspector);
    Assert.assertEquals(new Text("100.001"), textConverter.convert(HiveDecimal.create("100.001")), "TextConverter");
    Assert.assertEquals(null, textConverter.convert(null), "TextConverter");

    // Binary
    Converter baConverter =
        cacheableObjectInspectorConverters.getConverter(javaStringObjectInspector, writableBinaryObjectInspector);
    Assert.assertEquals(new BytesWritable(new byte[]{(byte) 'h', (byte) 'i', (byte) 'v', (byte) 'e'}),
        baConverter.convert("hive"), "BAConverter");
    Assert.assertEquals(null, baConverter.convert(null), "BAConverter");

    baConverter =
        cacheableObjectInspectorConverters.getConverter(writableStringObjectInspector, writableBinaryObjectInspector);
    Assert.assertEquals(new BytesWritable(new byte[]{(byte) 'h', (byte) 'i', (byte) 'v', (byte) 'e'}),
        baConverter.convert(new Text("hive")), "BAConverter");
    Assert.assertEquals(null, baConverter.convert(null), "BAConverter");
  } catch (Throwable e) {
    e.printStackTrace();
    throw e;
  }
}
 
Example #11
Source File: HiveTextReader.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public int populateData() throws IOException, SerDeException {
  final SkipRecordsInspector skipRecordsInspector = this.skipRecordsInspector;
  final RecordReader<Object, Object> reader = this.reader;
  final Converter partTblObjectInspectorConverter = this.partTblObjectInspectorConverter;
  final Object key = this.key;

  final int numRowsPerBatch = (int) this.numRowsPerBatch;

  final StructField[] selectedStructFieldRefs = this.selectedStructFieldRefs;
  final SerDe partitionSerDe = this.partitionSerDe;
  final StructObjectInspector finalOI = this.finalOI;
  final ObjectInspector[] selectedColumnObjInspectors = this.selectedColumnObjInspectors;
  final HiveFieldConverter[] selectedColumnFieldConverters = this.selectedColumnFieldConverters;
  final ValueVector[] vectors = this.vectors;

  skipRecordsInspector.reset();
  Object value;

  int recordCount = 0;

  while (recordCount < numRowsPerBatch) {
    try (OperatorStats.WaitRecorder recorder = OperatorStats.getWaitRecorder(this.context.getStats())) {
      boolean hasNext = reader.next(key, value = skipRecordsInspector.getNextValue());
      if (!hasNext) {
        break;
      }
    }
    catch(FSError e) {
      throw HadoopFileSystemWrapper.propagateFSError(e);
    }
    if (skipRecordsInspector.doSkipHeader(recordCount++)) {
      continue;
    }
    Object bufferedValue = skipRecordsInspector.bufferAdd(value);
    if (bufferedValue != null) {
      Object deSerializedValue = partitionSerDe.deserialize((Writable) bufferedValue);
      if (partTblObjectInspectorConverter != null) {
        deSerializedValue = partTblObjectInspectorConverter.convert(deSerializedValue);
      }

      for (int i = 0; i < selectedStructFieldRefs.length; i++) {
        Object hiveValue = finalOI.getStructFieldData(deSerializedValue, selectedStructFieldRefs[i]);
        if (hiveValue != null) {
          selectedColumnFieldConverters[i].setSafeValue(selectedColumnObjInspectors[i], hiveValue, vectors[i], skipRecordsInspector.getActualCount());
        }
      }
      skipRecordsInspector.incrementActualCount();
    }
    skipRecordsInspector.incrementTempCount();
  }
  for (int i = 0; i < selectedStructFieldRefs.length; i++) {
    vectors[i].setValueCount(skipRecordsInspector.getActualCount());
  }

  skipRecordsInspector.updateContinuance();
  return skipRecordsInspector.getActualCount();
}
 
Example #12
Source File: HiveTextReader.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public int populateData() throws IOException, SerDeException {
  final SkipRecordsInspector skipRecordsInspector = this.skipRecordsInspector;
  final RecordReader<Object, Object> reader = this.reader;
  final Converter partTblObjectInspectorConverter = this.partTblObjectInspectorConverter;
  final Object key = this.key;

  final int numRowsPerBatch = (int) this.numRowsPerBatch;

  final StructField[] selectedStructFieldRefs = this.selectedStructFieldRefs;
  final AbstractSerDe partitionSerDe = this.partitionSerDe;
  final StructObjectInspector finalOI = this.finalOI;
  final ObjectInspector[] selectedColumnObjInspectors = this.selectedColumnObjInspectors;
  final HiveFieldConverter[] selectedColumnFieldConverters = this.selectedColumnFieldConverters;
  final ValueVector[] vectors = this.vectors;

  skipRecordsInspector.reset();
  Object value;

  int recordCount = 0;

  while (recordCount < numRowsPerBatch) {
    try (OperatorStats.WaitRecorder recorder = OperatorStats.getWaitRecorder(this.context.getStats())) {
      boolean hasNext = reader.next(key, value = skipRecordsInspector.getNextValue());
      if (!hasNext) {
        break;
      }
    }
    catch(FSError e) {
      throw HadoopFileSystemWrapper.propagateFSError(e);
    }
    if (skipRecordsInspector.doSkipHeader(recordCount++)) {
      continue;
    }
    Object bufferedValue = skipRecordsInspector.bufferAdd(value);
    if (bufferedValue != null) {
      Object deSerializedValue = partitionSerDe.deserialize((Writable) bufferedValue);
      if (partTblObjectInspectorConverter != null) {
        deSerializedValue = partTblObjectInspectorConverter.convert(deSerializedValue);
      }

      for (int i = 0; i < selectedStructFieldRefs.length; i++) {
        Object hiveValue = finalOI.getStructFieldData(deSerializedValue, selectedStructFieldRefs[i]);
        if (hiveValue != null) {
          selectedColumnFieldConverters[i].setSafeValue(selectedColumnObjInspectors[i], hiveValue, vectors[i], skipRecordsInspector.getActualCount());
        }
      }
      skipRecordsInspector.incrementActualCount();
    }
    skipRecordsInspector.incrementTempCount();
  }
  for (int i = 0; i < selectedStructFieldRefs.length; i++) {
    vectors[i].setValueCount(skipRecordsInspector.getActualCount());
  }

  skipRecordsInspector.updateContinuance();
  return skipRecordsInspector.getActualCount();
}
 
Example #13
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
private Object getObjectOfCorrespondingPrimitiveType(String s, PrimitiveObjectInspector oi)
        throws IOException {
    PrimitiveTypeInfo typeInfo = oi.getTypeInfo();
    if (writeablePrimitives) {
        Converter c = ObjectInspectorConverters.getConverter(
            PrimitiveObjectInspectorFactory.javaStringObjectInspector, oi);
        return c.convert(s);
    }

    switch (typeInfo.getPrimitiveCategory()) {
        case INT:
            return Integer.valueOf(s);
        case BYTE:
            return Byte.valueOf(s);
        case SHORT:
            return Short.valueOf(s);
        case LONG:
            return Long.valueOf(s);
        case BOOLEAN:
            return (s.equalsIgnoreCase("true"));
        case FLOAT:
            return Float.valueOf(s);
        case DOUBLE:
            return Double.valueOf(s);
        case STRING:
            return s;
        case BINARY:
            try {
                String t = Text.decode(s.getBytes(), 0, s.getBytes().length);
                return t.getBytes();
            } catch (CharacterCodingException e) {
                LOG.warn("Error generating json binary type from object.", e);
                return null;
            }
        case DATE:
            return Date.valueOf(s);
        case TIMESTAMP:
            return Timestamp.valueOf(s);
        case DECIMAL:
            return HiveDecimal.create(s);
        case VARCHAR:
            return new HiveVarchar(s, ((BaseCharTypeInfo) typeInfo).getLength());
        case CHAR:
            return new HiveChar(s, ((BaseCharTypeInfo) typeInfo).getLength());
        default:
            throw new IOException(
                "Could not convert from string to " + typeInfo.getPrimitiveCategory());
    }
}
 
Example #14
Source File: ArrayRemoveUDF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
private static void removeAll(@Nonnull final List<?> values, @Nonnull final Object target,
        @Nonnull final Converter converter, @Nonnull final ListObjectInspector valueListOI) {
    Object converted = converter.convert(target);
    List<?> convertedList = valueListOI.getList(converted);
    values.removeAll(convertedList);
}
 
Example #15
Source File: ArrayRemoveUDF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
private static void removeAll(@Nonnull final List<?> values, @Nonnull final Object target,
        @Nonnull final Converter converter) {
    Object converted = converter.convert(target);
    values.removeAll(Collections.singleton(converted));
}