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(java.lang.String columnList)
Constructor.
|
HBaseStorage(java.lang.String columnList,
java.lang.String optString)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
checkSchema(ResourceSchema s)
Set the schema for data to be stored.
|
void |
cleanupOnFailure(java.lang.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(java.lang.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.Put |
createPut(java.lang.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.
|
java.util.List<HBaseStorage.ColumnInfo> |
getColumnInfoList()
Returns the ColumnInfo list so external objects can inspect it.
|
java.util.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.
|
java.util.List<java.lang.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.
|
java.lang.String |
relativeToAbsolutePath(java.lang.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.
|
java.lang.String |
relToAbsPathForStoreLocation(java.lang.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(java.util.List<HBaseStorage.ColumnInfo> columnInfoList)
Updates the ColumnInfo List.
|
void |
setLocation(java.lang.String location,
org.apache.hadoop.mapreduce.Job job)
Communicate to the loader the location of the object(s) being loaded.
|
void |
setStoreFuncUDFContextSignature(java.lang.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(java.lang.String location,
org.apache.hadoop.mapreduce.Job job)
Communicate to the storer the location where the data needs to be stored.
|
void |
setUDFContextSignature(java.lang.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(java.lang.String columnList) throws org.apache.commons.cli.ParseException, java.io.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 argumentsjava.io.IOException
public HBaseStorage(java.lang.String columnList, java.lang.String optString) throws org.apache.commons.cli.ParseException, java.io.IOException
columnList
- optString
- Loader options. Known options:org.apache.commons.cli.ParseException
java.io.IOException
public java.util.List<HBaseStorage.ColumnInfo> getColumnInfoList()
protected void setColumnInfoList(java.util.List<HBaseStorage.ColumnInfo> columnInfoList)
protected void storeProjectedFieldNames(LoadPushDown.RequiredFieldList requiredFieldList) throws FrontendException
FrontendException
public Tuple getNext() throws java.io.IOException
LoadFunc
public 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(java.lang.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(java.lang.String location, org.apache.hadoop.mapreduce.Job job) throws java.io.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
java.io.IOException
- if the location is not valid.public java.util.List<java.lang.String> getShipFiles()
LoadFunc
FuncUtils
for utility function to facilitate it
The default implementation returns null.getShipFiles
in interface StoreResources
getShipFiles
in class LoadFunc
public java.lang.String relativeToAbsolutePath(java.lang.String location, org.apache.hadoop.fs.Path curDir) throws java.io.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/
java.io.IOException
- if the conversion is not possiblepublic LoadCaster getLoadCaster() throws java.io.IOException
getLoadCaster
in class LoadFunc
LoadCaster
associated with this loader. Returning null
indicates that casts from byte array are not supported for this loader.
constructionjava.io.IOException
- if there is an exception during LoadCasterpublic org.apache.hadoop.mapreduce.OutputFormat getOutputFormat() throws java.io.IOException
StoreFuncInterface
getOutputFormat
in interface StoreFuncInterface
OutputFormat
associated with StoreFuncInterfacejava.io.IOException
- if an exception occurs while constructing the
OutputFormatpublic void checkSchema(ResourceSchema s) throws java.io.IOException
StoreFuncInterface
checkSchema
in interface StoreFuncInterface
s
- to be checkedjava.io.IOException
- 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 java.io.IOException
StoreFuncInterface
prepareToWrite
in interface StoreFuncInterface
writer
- RecordWriter to use.java.io.IOException
- if an exception occurs during initializationpublic void putNext(Tuple t) throws java.io.IOException
StoreFuncInterface
putNext
in interface StoreFuncInterface
t
- the tuple to store.java.io.IOException
- if an exception occurs during the writepublic org.apache.hadoop.hbase.client.Put createPut(java.lang.Object key, byte type) throws java.io.IOException
key
- type
- java.io.IOException
public java.lang.String relToAbsPathForStoreLocation(java.lang.String location, org.apache.hadoop.fs.Path curDir) throws java.io.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/
java.io.IOException
- if the conversion is not possiblepublic void setStoreFuncUDFContextSignature(java.lang.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(java.lang.String location, org.apache.hadoop.mapreduce.Job job) throws java.io.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
objectjava.io.IOException
- if the location is not valid.public void cleanupOnFailure(java.lang.String location, org.apache.hadoop.mapreduce.Job job) throws java.io.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 JobContext.getConfiguration()
and not to set/query
any runtime job information.java.io.IOException
public void cleanupOnSuccess(java.lang.String location, org.apache.hadoop.mapreduce.Job job) throws java.io.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 JobContext.getConfiguration()
and not to set/query
any runtime job information.java.io.IOException
public java.util.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 java.io.IOException
CollectableLoadFunc
ensureAllKeyInstancesInSameSplit
in interface CollectableLoadFunc
java.io.IOException
public org.apache.hadoop.io.WritableComparable<org.apache.hadoop.hbase.mapreduce.TableSplit> getSplitComparable(org.apache.hadoop.mapreduce.InputSplit split) throws java.io.IOException
OrderedLoadFunc
getSplitComparable
in interface OrderedLoadFunc
split
- An InputSplit from the InputFormat underlying this loader.java.io.IOException
Copyright © 2007-2012 The Apache Software Foundation