[ https://issues.apache.org/jira/browse/HIVE-12971?focusedWorklogId=280842&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-280842 ]
ASF GitHub Bot logged work on HIVE-12971: ----------------------------------------- Author: ASF GitHub Bot Created on: 23/Jul/19 06:56 Start Date: 23/Jul/19 06:56 Worklog Time Spent: 10m Work Description: jcamachor commented on pull request #733: HIVE-12971: Add Support for Kudu Tables URL: https://github.com/apache/hive/pull/733#discussion_r306148461 ########## File path: kudu-handler/src/java/org/apache/hadoop/hive/kudu/KuduSerDe.java ########## @@ -0,0 +1,263 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hive.kudu; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import com.google.common.base.Preconditions; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.common.type.HiveDecimal; +import org.apache.hadoop.hive.common.type.Timestamp; +import org.apache.hadoop.hive.metastore.utils.StringUtils; +import org.apache.hadoop.hive.serde2.AbstractSerDe; +import org.apache.hadoop.hive.serde2.SerDeException; +import org.apache.hadoop.hive.serde2.SerDeSpec; +import org.apache.hadoop.hive.serde2.SerDeStats; +import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable; +import org.apache.hadoop.hive.serde2.io.TimestampWritableV2; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.StructField; +import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector; +import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo; +import org.apache.hadoop.hive.serde2.io.ShortWritable; +import org.apache.hadoop.io.BooleanWritable; +import org.apache.hadoop.io.ByteWritable; +import org.apache.hadoop.io.BytesWritable; +import org.apache.hadoop.io.DoubleWritable; +import org.apache.hadoop.io.FloatWritable; +import org.apache.hadoop.io.IntWritable; +import org.apache.hadoop.io.LongWritable; +import org.apache.hadoop.io.Text; +import org.apache.hadoop.io.Writable; +import org.apache.kudu.ColumnSchema; +import org.apache.kudu.Schema; +import org.apache.kudu.Type; +import org.apache.kudu.client.KuduClient; +import org.apache.kudu.client.KuduException; +import org.apache.kudu.client.PartialRow; + +import static org.apache.hadoop.hive.kudu.KuduHiveUtils.createOverlayedConf; +import static org.apache.hadoop.hive.kudu.KuduHiveUtils.toHiveType; +import static org.apache.hadoop.hive.kudu.KuduStorageHandler.KUDU_TABLE_NAME_KEY; + +/** + * A Kudu serializer and deserializer to support reading and writing Kudu data from Hive. + */ +@SerDeSpec(schemaProps = { KuduStorageHandler.KUDU_TABLE_NAME_KEY }) +public class KuduSerDe extends AbstractSerDe { + + private ObjectInspector objectInspector; + private Schema schema; + + @SuppressWarnings("unused") + public KuduSerDe() {} + + @Override + public void initialize(Configuration sysConf, Properties tblProps) + throws SerDeException { + Configuration conf = createOverlayedConf(sysConf, tblProps); + String tableName = conf.get(KuduStorageHandler.KUDU_TABLE_NAME_KEY); + if (StringUtils.isEmpty(tableName)) { + throw new SerDeException(KUDU_TABLE_NAME_KEY + " is not set."); + } + try (KuduClient client = KuduHiveUtils.getKuduClient(conf)) { + if (!client.tableExists(tableName)) { + throw new SerDeException("Kudu table does not exist: " + tableName); + } + schema = client.openTable(tableName).getSchema(); + } catch (KuduException|IllegalArgumentException ex) { + throw new SerDeException(ex); + } + this.objectInspector = createObjectInspector(schema); + } + + private static ObjectInspector createObjectInspector(Schema schema) throws SerDeException { + Preconditions.checkNotNull(schema); + List<String> fieldNames = new ArrayList<>(); + List<ObjectInspector> fieldInspectors = new ArrayList<>(); + List<String> fieldComments = new ArrayList<>(); + for (int i = 0; i < schema.getColumnCount(); i++) { + ColumnSchema col = schema.getColumnByIndex(i); + PrimitiveTypeInfo typeInfo = toHiveType(col.getType(), col.getTypeAttributes()); + fieldNames.add(col.getName()); + fieldInspectors.add( + PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo)); + fieldComments.add(col.getComment()); + } + return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldInspectors, + fieldComments); + } + + public Schema getSchema() { + return schema; + } + + @Override + public ObjectInspector getObjectInspector() { + return objectInspector; + } + + @Override + public Class<? extends Writable> getSerializedClass() { + return KuduWritable.class; + } + + /** + * Serialize an object by navigating inside the Object with the ObjectInspector. + */ + @Override + public KuduWritable serialize(Object obj, ObjectInspector objectInspector) throws SerDeException { + Preconditions.checkArgument(objectInspector.getCategory() == Category.STRUCT); + + StructObjectInspector soi = (StructObjectInspector) objectInspector; + List<Object> writableObj = soi.getStructFieldsDataAsList(obj); + List<? extends StructField> fields = soi.getAllStructFieldRefs(); + + PartialRow row = schema.newPartialRow(); + for (int i = 0; i < schema.getColumnCount(); i++) { + StructField field = fields.get(i); + Object value = writableObj.get(i); + + if (value == null) { + row.setNull(i); + } else { + Type type = schema.getColumnByIndex(i).getType(); + ObjectInspector inspector = field.getFieldObjectInspector(); + switch (type) { + case BOOL: + boolean boolVal = ((BooleanObjectInspector) inspector).get(value); + row.addBoolean(i, boolVal); + break; + case INT8: + byte byteVal = ((ByteObjectInspector) inspector).get(value); + row.addByte(i, byteVal); + break; + case INT16: + short shortVal = ((ShortObjectInspector) inspector).get(value); + row.addShort(i, shortVal); + break; + case INT32: + int intVal = ((IntObjectInspector) inspector).get(value); + row.addInt(i, intVal); + break; + case INT64: + long longVal = ((LongObjectInspector) inspector).get(value); + row.addLong(i, longVal); + break; + case UNIXTIME_MICROS: + java.sql.Timestamp timestampVal = ((TimestampObjectInspector) inspector) + .getPrimitiveJavaObject(value).toSqlTimestamp(); Review comment: Same as above, please verify that this method is returning the timestamp object that you expect. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 280842) Time Spent: 1h 50m (was: 1h 40m) > Hive Support for Kudu > --------------------- > > Key: HIVE-12971 > URL: https://issues.apache.org/jira/browse/HIVE-12971 > Project: Hive > Issue Type: New Feature > Affects Versions: 2.0.0 > Reporter: Lenni Kuff > Assignee: Grant Henke > Priority: Major > Labels: pull-request-available > Attachments: HIVE-12971.0.patch, HIVE-12971.1.patch, > HIVE-12971.2.patch, HIVE-12971.3.patch > > Time Spent: 1h 50m > Remaining Estimate: 0h > > JIRA for tracking work related to Hive/Kudu integration. > It would be useful to allow Kudu data to be accessible via Hive. This would > involve creating a Kudu SerDe/StorageHandler and implementing support for > QUERY and DML commands like SELECT, INSERT, UPDATE, and DELETE. Kudu > Input/OutputFormats classes already exist. The work can be staged to support > this functionality incrementally. -- This message was sent by Atlassian JIRA (v7.6.14#76016)