Search in sources :

Example 1 with DatabaseAccessor

use of org.apache.hive.storage.jdbc.dao.DatabaseAccessor in project hive by apache.

the class JdbcSerDe method initialize.

/*
   * This method gets called multiple times by Hive. On some invocations, the properties will be empty.
   * We need to detect when the properties are not empty to initialise the class variables.
   *
   * @see org.apache.hadoop.hive.serde2.Deserializer#initialize(org.apache.hadoop.conf.Configuration, java.util.Properties)
   */
@Override
public void initialize(Configuration conf, Properties tbl) throws SerDeException {
    try {
        LOGGER.debug("Initializing the SerDe");
        // Hive cdh-4.3 does not provide the properties object on all calls
        if (tbl.containsKey(JdbcStorageConfig.DATABASE_TYPE.getPropertyName())) {
            Configuration tableConfig = JdbcStorageConfigManager.convertPropertiesToConfiguration(tbl);
            DatabaseAccessor dbAccessor = DatabaseAccessorFactory.getAccessor(tableConfig);
            columnNames = dbAccessor.getColumnNames(tableConfig);
            numColumns = columnNames.size();
            String[] hiveColumnNameArray = parseProperty(tbl.getProperty(serdeConstants.LIST_COLUMNS), ",");
            if (numColumns != hiveColumnNameArray.length) {
                throw new SerDeException("Expected " + numColumns + " columns. Table definition has " + hiveColumnNameArray.length + " columns");
            }
            List<String> hiveColumnNames = Arrays.asList(hiveColumnNameArray);
            hiveColumnTypeArray = parseProperty(tbl.getProperty(serdeConstants.LIST_COLUMN_TYPES), ":");
            if (hiveColumnTypeArray.length == 0) {
                throw new SerDeException("Received an empty Hive column type definition");
            }
            List<ObjectInspector> fieldInspectors = new ArrayList<ObjectInspector>(numColumns);
            for (int i = 0; i < numColumns; i++) {
                fieldInspectors.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
            }
            objectInspector = ObjectInspectorFactory.getStandardStructObjectInspector(hiveColumnNames, fieldInspectors);
            row = new ArrayList<String>(numColumns);
        }
    } catch (Exception e) {
        LOGGER.error("Caught exception while initializing the SqlSerDe", e);
        throw new SerDeException(e);
    }
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) Configuration(org.apache.hadoop.conf.Configuration) ArrayList(java.util.ArrayList) DatabaseAccessor(org.apache.hive.storage.jdbc.dao.DatabaseAccessor) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Aggregations

ArrayList (java.util.ArrayList)1 Configuration (org.apache.hadoop.conf.Configuration)1 SerDeException (org.apache.hadoop.hive.serde2.SerDeException)1 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)1 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)1 DatabaseAccessor (org.apache.hive.storage.jdbc.dao.DatabaseAccessor)1