All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.hive.hbase.HBaseCompositeKey Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
/*
 * 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.hbase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.hive.serde2.lazy.ByteArrayRef;
import org.apache.hadoop.hive.serde2.lazy.LazyFactory;
import org.apache.hadoop.hive.serde2.lazy.LazyObject;
import org.apache.hadoop.hive.serde2.lazy.LazyStruct;
import org.apache.hadoop.hive.serde2.lazy.objectinspector.LazySimpleStructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructField;

/**
 * HBaseCompositeKey extension of LazyStruct. All complex composite keys should extend this class
 * and override the {@link LazyStruct#getField(int)} method where fieldID corresponds to the ID of a
 * key in the composite key.
 * 

* For example, for a composite key "/part1/part2/part3", part1 will have an id * 0, part2 will have an id 1 and part3 will have an id 2. Custom * implementations of getField(fieldID) should return the value corresponding to that fieldID. So, * for the above example, the value returned for getField(0) should be part1, * getField(1) should be part2 and getField(2) should be part3. *

* *

* All custom implementation are expected to have a constructor of the form: * *

 * MyCustomCompositeKey(LazySimpleStructObjectInspector oi, Properties tbl, Configuration conf)
 * 
* *

* * */ public class HBaseCompositeKey extends LazyStruct { public HBaseCompositeKey(LazySimpleStructObjectInspector oi) { super(oi); } @Override public ArrayList getFieldsAsList() { ArrayList allFields = new ArrayList(); List fields = oi.getAllStructFieldRefs(); for (int i = 0; i < fields.size(); i++) { allFields.add(getField(i)); } return allFields; } /** * Create an initialize a {@link LazyObject} with the given bytes for the given fieldID. * * @param fieldID * field for which the object is to be created * @param bytes * value with which the object is to be initialized with * @return initialized {@link LazyObject} * */ public LazyObject toLazyObject(int fieldID, byte[] bytes) { ObjectInspector fieldOI = oi.getAllStructFieldRefs().get(fieldID).getFieldObjectInspector(); LazyObject lazyObject = LazyFactory .createLazyObject(fieldOI); ByteArrayRef ref = new ByteArrayRef(); ref.setData(bytes); // initialize the lazy object lazyObject.init(ref, 0, ref.getData().length); return lazyObject; } /** * Return the different parts of the key. By default, this returns an empty map. Consumers can * choose to override this to provide their own names and types of parts of the key. * * @return map of parts name to their type * */ public Map getParts() { return Collections.emptyMap(); } }