public class HBaseStorage extends LoadFunc implements StoreFuncInterface, LoadPushDown, OrderedLoadFunc, StoreResources, CollectableLoadFunc
Below is an example showing how to load data from HBase:
raw = LOAD 'hbase://SampleTable'
USING org.apache.pig.backend.hadoop.hbase.HBaseStorage(
'info:first_name info:last_name friends:* info:*', '-loadKey true -limit 5')
AS (id:bytearray, first_name:chararray, last_name:chararray, friends_map:map[], info_map:map[]);
This example loads data redundantly from the info column family just to
illustrate usage. Note that the row key is inserted first in the result schema.
To load only column names that start with a given prefix, specify the column
name with a trailing '*'. For example passing friends:bob_*
to
the constructor in the above example would cause only columns that start with
bob_ to be loaded.
Note that when using a prefix like friends:bob_*
, explicit HBase filters are set for
all columns and prefixes specified. Querying HBase with many filters can cause performance
degredation. This is typically seen when mixing one or more prefixed descriptors with a large list
of columns. In that case better perfomance will be seen by either loading the entire family via
friends:*
or by specifying explicit column descriptor names.
Below is an example showing how to store data into HBase:
copy = STORE raw INTO 'hbase://SampleTableCopy'
USING org.apache.pig.backend.hadoop.hbase.HBaseStorage(
'info:first_name info:last_name friends:* info:*');
Note that STORE will expect the first value in the tuple to be the row key.
Scalars values need to map to an explicit column descriptor and maps need to
map to a column family name. In the above examples, the friends
column family data from SampleTable
will be written to a
buddies
column family in the SampleTableCopy
table.Modifier and Type | Class and Description |
---|---|
class |
HBaseStorage.ColumnInfo
Class to encapsulate logic around which column names were specified in each
position of the column list.
|
LoadPushDown.OperatorSet, LoadPushDown.RequiredField, LoadPushDown.RequiredFieldList, LoadPushDown.RequiredFieldResponse
Modifier and Type | Field and Description |
---|---|
protected byte[] |
gt_ |
protected byte[] |
gte_ |
protected byte[] |
lt_ |
protected byte[] |
lte_ |
Constructor and Description |
---|
HBaseStorage(String columnList)
Constructor.
|
HBaseStorage(String columnList,
String optString)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
checkSchema(ResourceSchema s)
Set the schema for data to be stored.
|
void |
cleanupOnFailure(String location,
org.apache.hadoop.mapreduce.Job job)
This method will be called by Pig if the job which contains this store
fails.
|
void |
cleanupOnSuccess(String location,
org.apache.hadoop.mapreduce.Job job)
This method will be called by Pig if the job which contains this store
is successful, and some cleanup of intermediate resources is required.
|
org.apache.hadoop.hbase.client.Delete |
createDelete(Object key,
byte type,
long timestamp)
Public method to initialize a Delete.
|
org.apache.hadoop.hbase.client.Put |
createPut(Object key,
byte type)
Public method to initialize a Put.
|
void |
ensureAllKeyInstancesInSameSplit()
When this method is called, Pig is communicating to the Loader that it must
load data such that all instances of a key are in same split.
|
List<HBaseStorage.ColumnInfo> |
getColumnInfoList()
Returns the ColumnInfo list so external objects can inspect it.
|
List<LoadPushDown.OperatorSet> |
getFeatures()
Determine the operators that can be pushed to the loader.
|
org.apache.hadoop.mapreduce.InputFormat |
getInputFormat()
This will be called during planning on the front end.
|
LoadCaster |
getLoadCaster()
Set up the caster to use for reading values out of, and writing to, HBase.
|
Tuple |
getNext()
Retrieves the next tuple to be processed.
|
org.apache.hadoop.mapreduce.OutputFormat |
getOutputFormat()
Return the OutputFormat associated with StoreFuncInterface.
|
List<String> |
getShipFiles()
Allow a LoadFunc to specify a list of files located locally and would like to ship to backend
(through distributed cache).
|
org.apache.hadoop.io.WritableComparable<org.apache.hadoop.hbase.mapreduce.TableSplit> |
getSplitComparable(org.apache.hadoop.mapreduce.InputSplit split)
The WritableComparable object returned will be used to compare
the position of different splits in an ordered stream
|
void |
prepareToRead(org.apache.hadoop.mapreduce.RecordReader reader,
PigSplit split)
Initializes LoadFunc for reading data.
|
void |
prepareToWrite(org.apache.hadoop.mapreduce.RecordWriter writer)
Initialize StoreFuncInterface to write data.
|
LoadPushDown.RequiredFieldResponse |
pushProjection(LoadPushDown.RequiredFieldList requiredFieldList)
Indicate to the loader fields that will be needed.
|
void |
putNext(Tuple t)
Write a tuple to the data store.
|
String |
relativeToAbsolutePath(String location,
org.apache.hadoop.fs.Path curDir)
This method is called by the Pig runtime in the front end to convert the
input location to an absolute path if the location is relative.
|
String |
relToAbsPathForStoreLocation(String location,
org.apache.hadoop.fs.Path curDir)
This method is called by the Pig runtime in the front end to convert the
output location to an absolute path if the location is relative.
|
protected void |
setColumnInfoList(List<HBaseStorage.ColumnInfo> columnInfoList)
Updates the ColumnInfo List.
|
void |
setLocation(String location,
org.apache.hadoop.mapreduce.Job job)
Communicate to the loader the location of the object(s) being loaded.
|
void |
setStoreFuncUDFContextSignature(String signature)
This method will be called by Pig both in the front end and back end to
pass a unique signature to the
StoreFuncInterface which it can use to store
information in the UDFContext which it needs to store between
various method invocations in the front end and back end. |
void |
setStoreLocation(String location,
org.apache.hadoop.mapreduce.Job job)
Communicate to the storer the location where the data needs to be stored.
|
void |
setUDFContextSignature(String signature)
This method will be called by Pig both in the front end and back end to
pass a unique signature to the
LoadFunc . |
protected void |
storeProjectedFieldNames(LoadPushDown.RequiredFieldList requiredFieldList)
Stores the requiredFieldsList as a serialized object so it can be fetched on the cluster.
|
getAbsolutePath, getCacheFiles, getPathStrings, join, warn
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getCacheFiles
protected transient byte[] gt_
protected transient byte[] gte_
protected transient byte[] lt_
protected transient byte[] lte_
public HBaseStorage(String columnList) throws org.apache.commons.cli.ParseException, IOException
columnList
- columnlist that is a presented string delimited by space and/or
commas. To retreive all columns in a column family Foo
,
specify a column as either Foo:
or Foo:*
.
To fetch only columns in the CF that start with bar, specify
Foo:bar*
. The resulting tuple will always be the size
of the number of tokens in columnList
. Items in the
tuple will be scalar values when a full column descriptor is
specified, or a map of column descriptors to values when a column
family is specified.org.apache.commons.cli.ParseException
- when unable to parse argumentsIOException
public HBaseStorage(String columnList, String optString) throws org.apache.commons.cli.ParseException, IOException
columnList
- optString
- Loader options. Known options:org.apache.commons.cli.ParseException
IOException
public List<HBaseStorage.ColumnInfo> getColumnInfoList()
protected void setColumnInfoList(List<HBaseStorage.ColumnInfo> columnInfoList)
protected void storeProjectedFieldNames(LoadPushDown.RequiredFieldList requiredFieldList) throws FrontendException
FrontendException
public Tuple getNext() throws IOException
LoadFunc
getNext
in class LoadFunc
IOException
- if there is an exception while retrieving the next
tuplepublic org.apache.hadoop.mapreduce.InputFormat getInputFormat()
LoadFunc
getInputFormat
in class LoadFunc
public void prepareToRead(org.apache.hadoop.mapreduce.RecordReader reader, PigSplit split)
LoadFunc
prepareToRead
in class LoadFunc
reader
- RecordReader
to be used by this instance of the LoadFuncsplit
- The input PigSplit
to processpublic void setUDFContextSignature(String signature)
LoadFunc
LoadFunc
. The signature can be used
to store into the UDFContext
any information which the
LoadFunc
needs to store between various method invocations in the
front end and back end. A use case is to store LoadPushDown.RequiredFieldList
passed to it in LoadPushDown.pushProjection(RequiredFieldList)
for
use in the back end before returning tuples in LoadFunc.getNext()
.
This method will be call before other methods in LoadFunc
setUDFContextSignature
in class LoadFunc
signature
- a unique signature to identify this LoadFuncpublic void setLocation(String location, org.apache.hadoop.mapreduce.Job job) throws IOException
LoadFunc
LoadFunc.relativeToAbsolutePath(String, Path)
. Implementations
should use this method to communicate the location (and any other information)
to its underlying InputFormat through the Job object.
This method will be called in the frontend and backend multiple times. Implementations
should bear in mind that this method is called multiple times and should
ensure there are no inconsistent side effects due to the multiple calls.setLocation
in class LoadFunc
location
- Location as returned by
LoadFunc.relativeToAbsolutePath(String, Path)
job
- the Job
object
store or retrieve earlier stored information from the UDFContext
IOException
- if the location is not valid.public List<String> getShipFiles()
LoadFunc
FuncUtils
for utility function to facilitate it
The default implementation returns null.getShipFiles
in interface StoreResources
getShipFiles
in class LoadFunc
public String relativeToAbsolutePath(String location, org.apache.hadoop.fs.Path curDir) throws IOException
LoadFunc
relativeToAbsolutePath
in class LoadFunc
location
- location as provided in the "load" statement of the scriptcurDir
- the current working direction based on any "cd" statements
in the script before the "load" statement. If there are no "cd" statements
in the script, this would be the home directory -
/user/
IOException
- if the conversion is not possiblepublic LoadCaster getLoadCaster() throws IOException
getLoadCaster
in class LoadFunc
LoadCaster
associated with this loader. Returning null
indicates that casts from byte array are not supported for this loader.
constructionIOException
- if there is an exception during LoadCasterpublic org.apache.hadoop.mapreduce.OutputFormat getOutputFormat() throws IOException
StoreFuncInterface
getOutputFormat
in interface StoreFuncInterface
OutputFormat
associated with StoreFuncInterfaceIOException
- if an exception occurs while constructing the
OutputFormatpublic void checkSchema(ResourceSchema s) throws IOException
StoreFuncInterface
checkSchema
in interface StoreFuncInterface
s
- to be checkedIOException
- if this schema is not acceptable. It should include
a detailed error message indicating what is wrong with the schema.public void prepareToWrite(org.apache.hadoop.mapreduce.RecordWriter writer) throws IOException
StoreFuncInterface
prepareToWrite
in interface StoreFuncInterface
writer
- RecordWriter to use.IOException
- if an exception occurs during initializationpublic void putNext(Tuple t) throws IOException
StoreFuncInterface
putNext
in interface StoreFuncInterface
t
- the tuple to store.IOException
- if an exception occurs during the writepublic org.apache.hadoop.hbase.client.Delete createDelete(Object key, byte type, long timestamp) throws IOException
key
- type
- timestamp
- IOException
public org.apache.hadoop.hbase.client.Put createPut(Object key, byte type) throws IOException
key
- type
- IOException
public String relToAbsPathForStoreLocation(String location, org.apache.hadoop.fs.Path curDir) throws IOException
StoreFuncInterface
LoadFunc.getAbsolutePath(java.lang.String, org.apache.hadoop.fs.Path)
provides a default
implementation for hdfs and hadoop local file system and it can be used
to implement this method.relToAbsPathForStoreLocation
in interface StoreFuncInterface
location
- location as provided in the "store" statement of the scriptcurDir
- the current working direction based on any "cd" statements
in the script before the "store" statement. If there are no "cd" statements
in the script, this would be the home directory -
/user/
IOException
- if the conversion is not possiblepublic void setStoreFuncUDFContextSignature(String signature)
StoreFuncInterface
StoreFuncInterface
which it can use to store
information in the UDFContext
which it needs to store between
various method invocations in the front end and back end. This is necessary
because in a Pig Latin script with multiple stores, the different
instances of store functions need to be able to find their (and only their)
data in the UDFContext object.setStoreFuncUDFContextSignature
in interface StoreFuncInterface
signature
- a unique signature to identify this StoreFuncInterfacepublic void setStoreLocation(String location, org.apache.hadoop.mapreduce.Job job) throws IOException
StoreFuncInterface
StoreFuncInterface
here is the
return value of StoreFuncInterface.relToAbsPathForStoreLocation(String, Path)
This method will be called in the frontend and backend multiple times. Implementations
should bear in mind that this method is called multiple times and should
ensure there are no inconsistent side effects due to the multiple calls.
StoreFuncInterface.checkSchema(ResourceSchema)
will be called before any call to
StoreFuncInterface.setStoreLocation(String, Job)
.setStoreLocation
in interface StoreFuncInterface
location
- Location returned by
StoreFuncInterface.relToAbsPathForStoreLocation(String, Path)
job
- The Job
objectIOException
- if the location is not valid.public void cleanupOnFailure(String location, org.apache.hadoop.mapreduce.Job job) throws IOException
StoreFuncInterface
cleanupOnFailure
in interface StoreFuncInterface
location
- Location returned by
StoreFuncInterface.relToAbsPathForStoreLocation(String, Path)
job
- The Job
object - this should be used only to obtain
cluster properties through JobContextImpl.getConfiguration()
and not to set/query
any runtime job information.IOException
public void cleanupOnSuccess(String location, org.apache.hadoop.mapreduce.Job job) throws IOException
StoreFuncInterface
cleanupOnSuccess
in interface StoreFuncInterface
location
- Location returned by
StoreFuncInterface.relToAbsPathForStoreLocation(String, Path)
job
- The Job
object - this should be used only to obtain
cluster properties through JobContextImpl.getConfiguration()
and not to set/query
any runtime job information.IOException
public List<LoadPushDown.OperatorSet> getFeatures()
LoadPushDown
getFeatures
in interface LoadPushDown
public LoadPushDown.RequiredFieldResponse pushProjection(LoadPushDown.RequiredFieldList requiredFieldList) throws FrontendException
LoadPushDown
pushProjection
in interface LoadPushDown
requiredFieldList
- RequiredFieldList indicating which columns will be needed.
This structure is read only. User cannot make change to it inside pushProjection.FrontendException
public void ensureAllKeyInstancesInSameSplit() throws IOException
CollectableLoadFunc
ensureAllKeyInstancesInSameSplit
in interface CollectableLoadFunc
IOException
public org.apache.hadoop.io.WritableComparable<org.apache.hadoop.hbase.mapreduce.TableSplit> getSplitComparable(org.apache.hadoop.mapreduce.InputSplit split) throws IOException
OrderedLoadFunc
getSplitComparable
in interface OrderedLoadFunc
split
- An InputSplit from the InputFormat underlying this loader.IOException
Copyright © 2007-2017 The Apache Software Foundation