|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.hadoop.hbase.client.HTable
public class HTable
Used to communicate with a single HBase table. This class is not thread safe for updates; the underlying write buffer can be corrupted if multiple threads contend over a single HTable instance.
Instances of HTable passed the same Configuration
instance will
share connections to servers out on the cluster and to the zookeeper ensemble
as well as caches of region locations. This is usually a *good* thing and it
is recommended to reuse the same configuration object for all your tables.
This happens because they will all share the same underlying
HConnection
instance. See HConnectionManager
for more on
how this mechanism works.
HConnection
will read most of the
configuration it needs from the passed Configuration
on initial
construction. Thereafter, for settings such as
hbase.client.pause
, hbase.client.retries.number
,
and hbase.client.rpc.maxattempts
updating their values in the
passed Configuration
subsequent to HConnection
construction
will go unnoticed. To run with changed values, make a new
HTable
passing a new Configuration
instance that has the
new configuration.
for create, drop, list, enable and disable of tables.
,
HConnection
,
HConnectionManager
Nested Class Summary | |
---|---|
protected class |
HTable.ClientScanner
Implements the scanner interface for the HBase client. |
Field Summary | |
---|---|
protected int |
scannerCaching
|
protected int |
scannerTimeout
|
Constructor Summary | |
---|---|
HTable(byte[] tableName)
Creates an object to access a HBase table. |
|
HTable(org.apache.hadoop.conf.Configuration conf,
byte[] tableName)
Creates an object to access a HBase table. |
|
HTable(org.apache.hadoop.conf.Configuration conf,
String tableName)
Creates an object to access a HBase table. |
|
HTable(String tableName)
Creates an object to access a HBase table. |
Method Summary | |
---|---|
Object[] |
batch(List<Row> actions)
Method that does a batch call on Deletes, Gets and Puts. |
void |
batch(List<Row> actions,
Object[] results)
Method that does a batch call on Deletes, Gets and Puts. |
boolean |
checkAndDelete(byte[] row,
byte[] family,
byte[] qualifier,
byte[] value,
Delete delete)
Atomically checks if a row/family/qualifier value match the expectedValue. |
boolean |
checkAndPut(byte[] row,
byte[] family,
byte[] qualifier,
byte[] value,
Put put)
Atomically checks if a row/family/qualifier value match the expectedValue. |
void |
clearRegionCache()
Explicitly clears the region cache to fetch the latest value from META. |
void |
close()
Releases any resources help or pending changes in internal buffers. |
void |
delete(Delete delete)
Deletes the specified cells/row. |
void |
delete(List<Delete> deletes)
Deletes the specified cells/rows in bulk. |
Map<HRegionInfo,HServerAddress> |
deserializeRegionInfo(DataInput in)
Read from in and deserialize the regions information. |
boolean |
exists(Get get)
Test for the existence of columns in the table, as specified in the Get. |
void |
flushCommits()
Executes all the buffered Put operations. |
Result |
get(Get get)
Extracts certain cells from a given row. |
Result[] |
get(List<Get> gets)
Extracts certain cells from the given rows, in batch. |
org.apache.hadoop.conf.Configuration |
getConfiguration()
Returns the Configuration object used by this instance. |
HConnection |
getConnection()
INTERNAL Used by unit tests and tools to do low-level manipulations. |
int |
getCurrentNrHRS()
|
byte[][] |
getEndKeys()
Gets the ending row key for every region in the currently open table. |
static boolean |
getRegionCachePrefetch(byte[] tableName)
Check whether region cache prefetch is enabled or not for the table. |
static boolean |
getRegionCachePrefetch(org.apache.hadoop.conf.Configuration conf,
byte[] tableName)
Check whether region cache prefetch is enabled or not for the table. |
HRegionLocation |
getRegionLocation(byte[] row)
Finds the region on which the given row is being served. |
HRegionLocation |
getRegionLocation(String row)
Find region location hosting passed row using cached info |
Map<HRegionInfo,HServerAddress> |
getRegionsInfo()
Gets all the regions and their address for this table. |
Result |
getRowOrBefore(byte[] row,
byte[] family)
Return the row that matches row exactly, or the one that immediately precedes it. |
ResultScanner |
getScanner(byte[] family)
Gets a scanner on the current table for the given family. |
ResultScanner |
getScanner(byte[] family,
byte[] qualifier)
Gets a scanner on the current table for the given family and qualifier. |
ResultScanner |
getScanner(Scan scan)
Returns a scanner on the current table as specified by the Scan
object. |
int |
getScannerCaching()
Gets the number of rows that a scanner will fetch at once. |
Pair<byte[][],byte[][]> |
getStartEndKeys()
Gets the starting and ending row keys for every region in the currently open table. |
byte[][] |
getStartKeys()
Gets the starting row key for every region in the currently open table. |
HTableDescriptor |
getTableDescriptor()
Gets the table descriptor for this table. |
byte[] |
getTableName()
Gets the name of this table. |
ArrayList<Put> |
getWriteBuffer()
Returns the write buffer. |
long |
getWriteBufferSize()
Returns the maximum size in bytes of the write buffer for this HTable. |
Result |
increment(Increment increment)
Increments one or more columns within a single row. |
long |
incrementColumnValue(byte[] row,
byte[] family,
byte[] qualifier,
long amount)
Atomically increments a column value. |
long |
incrementColumnValue(byte[] row,
byte[] family,
byte[] qualifier,
long amount,
boolean writeToWAL)
Atomically increments a column value. |
boolean |
isAutoFlush()
Tells whether or not 'auto-flush' is turned on. |
static boolean |
isTableEnabled(byte[] tableName)
Tells whether or not a table is enabled or not. |
static boolean |
isTableEnabled(org.apache.hadoop.conf.Configuration conf,
byte[] tableName)
Tells whether or not a table is enabled or not. |
static boolean |
isTableEnabled(org.apache.hadoop.conf.Configuration conf,
String tableName)
Tells whether or not a table is enabled or not. |
static boolean |
isTableEnabled(String tableName)
Tells whether or not a table is enabled or not. |
RowLock |
lockRow(byte[] row)
Obtains a lock on a row. |
void |
prewarmRegionCache(Map<HRegionInfo,HServerAddress> regionMap)
Save the passed region information and the table's regions cache. |
void |
put(List<Put> puts)
Puts some data in the table, in batch. |
void |
put(Put put)
Puts some data in the table. |
void |
serializeRegionInfo(DataOutput out)
Serialize the regions information of this table and output to out . |
void |
setAutoFlush(boolean autoFlush)
See setAutoFlush(boolean, boolean) |
void |
setAutoFlush(boolean autoFlush,
boolean clearBufferOnFail)
Turns 'auto-flush' on or off. |
static void |
setRegionCachePrefetch(byte[] tableName,
boolean enable)
Enable or disable region cache prefetch for the table. |
static void |
setRegionCachePrefetch(org.apache.hadoop.conf.Configuration conf,
byte[] tableName,
boolean enable)
Enable or disable region cache prefetch for the table. |
void |
setScannerCaching(int scannerCaching)
Sets the number of rows that a scanner will fetch at once. |
void |
setWriteBufferSize(long writeBufferSize)
Sets the size of the buffer in bytes. |
void |
unlockRow(RowLock rl)
Releases a row lock. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final int scannerTimeout
protected int scannerCaching
Constructor Detail |
---|
public HTable(String tableName) throws IOException
Configuration
and a new
client to zookeeper as well as other resources. It also comes up with
a fresh view of the cluster and must do discovery from scratch of region
locations; i.e. it will not make use of already-cached region locations if
available. Use only when being quick and dirty.
IOException
- if a remote or network exception occursHTable(Configuration, String)
public HTable(byte[] tableName) throws IOException
Configuration
and a new
client to zookeeper as well as other resources. It also comes up with
a fresh view of the cluster and must do discovery from scratch of region
locations; i.e. it will not make use of already-cached region locations if
available. Use only when being quick and dirty.
tableName
- Name of the table.
IOException
- if a remote or network exception occursHTable(Configuration, String)
public HTable(org.apache.hadoop.conf.Configuration conf, String tableName) throws IOException
conf
instance. Uses already-populated
region cache if one is available, populated by any other HTable instances
sharing this conf
instance. Recommended.
conf
- Configuration object to use.tableName
- Name of the table.
IOException
- if a remote or network exception occurspublic HTable(org.apache.hadoop.conf.Configuration conf, byte[] tableName) throws IOException
conf
instance. Uses already-populated
region cache if one is available, populated by any other HTable instances
sharing this conf
instance. Recommended.
conf
- Configuration object to use.tableName
- Name of the table.
IOException
- if a remote or network exception occursMethod Detail |
---|
public int getCurrentNrHRS() throws IOException
IOException
- if a remote or network exception occurspublic org.apache.hadoop.conf.Configuration getConfiguration()
HTableInterface
Configuration
object used by this instance.
The reference returned is not a copy, so any change made to it will affect this instance.
getConfiguration
in interface HTableInterface
public static boolean isTableEnabled(String tableName) throws IOException
HBaseAdmin.isTableEnabled(byte[])
instead.
tableName
- Name of table to check.
true
if table is online.
IOException
- if a remote or network exception occurspublic static boolean isTableEnabled(byte[] tableName) throws IOException
HBaseAdmin.isTableEnabled(byte[])
instead.
tableName
- Name of table to check.
true
if table is online.
IOException
- if a remote or network exception occurspublic static boolean isTableEnabled(org.apache.hadoop.conf.Configuration conf, String tableName) throws IOException
HBaseAdmin.isTableEnabled(byte[])
instead.
conf
- The Configuration object to use.tableName
- Name of table to check.
true
if table is online.
IOException
- if a remote or network exception occurspublic static boolean isTableEnabled(org.apache.hadoop.conf.Configuration conf, byte[] tableName) throws IOException
conf
- The Configuration object to use.tableName
- Name of table to check.
true
if table is online.
IOException
- if a remote or network exception occurspublic HRegionLocation getRegionLocation(String row) throws IOException
row
- Row to find.
IOException
- if a remote or network exception occurspublic HRegionLocation getRegionLocation(byte[] row) throws IOException
row
- Row to find.
IOException
- if a remote or network exception occurspublic byte[] getTableName()
HTableInterface
getTableName
in interface HTableInterface
public HConnection getConnection()
public int getScannerCaching()
The default value comes from hbase.client.scanner.caching
.
public void setScannerCaching(int scannerCaching)
This will override the value specified by
hbase.client.scanner.caching
.
Increasing this value will reduce the amount of work needed each time
next()
is called on a scanner, at the expense of memory use
(since more rows will need to be maintained in memory by the scanners).
scannerCaching
- the number of rows a scanner will fetch at once.public HTableDescriptor getTableDescriptor() throws IOException
HTableInterface
table descriptor
for this table.
getTableDescriptor
in interface HTableInterface
IOException
- if a remote or network exception occurs.public byte[][] getStartKeys() throws IOException
This is mainly useful for the MapReduce integration.
IOException
- if a remote or network exception occurspublic byte[][] getEndKeys() throws IOException
This is mainly useful for the MapReduce integration.
IOException
- if a remote or network exception occurspublic Pair<byte[][],byte[][]> getStartEndKeys() throws IOException
This is mainly useful for the MapReduce integration.
IOException
- if a remote or network exception occurspublic Map<HRegionInfo,HServerAddress> getRegionsInfo() throws IOException
This is mainly useful for the MapReduce integration.
IOException
- if a remote or network exception occurspublic void prewarmRegionCache(Map<HRegionInfo,HServerAddress> regionMap)
This is mainly useful for the MapReduce integration. You can call
deserializeRegionInfo
to deserialize regions information from a
DataInput
, then call this method to load them to cache.
HTable t1 = new HTable("foo");
FileInputStream fis = new FileInputStream("regions.dat");
DataInputStream dis = new DataInputStream(fis);
Map<HRegionInfo, HServerAddress> hm = t1.deserializeRegionInfo(dis);
t1.prewarmRegionCache(hm);
regionMap
- This piece of regions information will be loaded
to region cache.public void serializeRegionInfo(DataOutput out) throws IOException
out
.
This is mainly useful for the MapReduce integration. A client could perform a large scan for all the regions for the table, serialize the region info to a file. MR job can ship a copy of the meta for the table in the DistributedCache.
FileOutputStream fos = new FileOutputStream("regions.dat");
DataOutputStream dos = new DataOutputStream(fos);
table.serializeRegionInfo(dos);
dos.flush();
dos.close();
out
- DataOutput
to serialize this object into.
IOException
- if a remote or network exception occurspublic Map<HRegionInfo,HServerAddress> deserializeRegionInfo(DataInput in) throws IOException
in
and deserialize the regions information.
It behaves similarly as getRegionsInfo
, except
that it loads the region map from a DataInput
object.
It is supposed to be followed immediately by prewarmRegionCache
.
Please refer to prewarmRegionCache
for usage.
in
- DataInput
object.
IOException
- if an I/O exception occurs.public Result getRowOrBefore(byte[] row, byte[] family) throws IOException
HTableInterface
getRowOrBefore
in interface HTableInterface
row
- A row key.family
- Column family to include in the Result
.
IOException
- if a remote or network exception occurs.public ResultScanner getScanner(Scan scan) throws IOException
HTableInterface
Scan
object.
getScanner
in interface HTableInterface
scan
- A configured Scan
object.
IOException
- if a remote or network exception occurs.public ResultScanner getScanner(byte[] family) throws IOException
HTableInterface
getScanner
in interface HTableInterface
family
- The column family to scan.
IOException
- if a remote or network exception occurs.public ResultScanner getScanner(byte[] family, byte[] qualifier) throws IOException
HTableInterface
getScanner
in interface HTableInterface
family
- The column family to scan.qualifier
- The column qualifier to scan.
IOException
- if a remote or network exception occurs.public Result get(Get get) throws IOException
HTableInterface
get
in interface HTableInterface
get
- The object that specifies what data to fetch and from which row.
Result
instance returned won't
contain any KeyValue
, as indicated by Result.isEmpty()
.
IOException
- if a remote or network exception occurs.public Result[] get(List<Get> gets) throws IOException
HTableInterface
get
in interface HTableInterface
gets
- The objects that specify what data to fetch and from which rows.
Result
instance returned won't
contain any KeyValue
, as indicated by Result.isEmpty()
.
A null in the return array means that the get operation for that
Get failed, even after retries.
IOException
- if a remote or network exception occurs.public void batch(List<Row> actions, Object[] results) throws InterruptedException, IOException
batch(java.util.List, java.lang.Object[])
call, you will not necessarily be
guaranteed that the Get returns what the Put had put.
batch
in interface HTableInterface
actions
- list of Get, Put, Delete objectsresults
- Empty Result[], same size as actions. Provides access to
partial results, in case an exception is thrown. If there are any failures,
there will be a null or Throwable will be in the results array, AND an
exception will be thrown.
IOException
InterruptedException
public Object[] batch(List<Row> actions) throws InterruptedException, IOException
batch
in interface HTableInterface
actions
- list of Get, Put, Delete objects
IOException
InterruptedException
public void delete(Delete delete) throws IOException
delete
in interface HTableInterface
delete
- The object that specifies what to delete.
IOException
- if a remote or network exception occurs.public void delete(List<Delete> deletes) throws IOException
delete
in interface HTableInterface
deletes
- List of things to delete. As a side effect, it will be modified:
successful Delete
s are removed. The ordering of the list will not change.
IOException
- if a remote or network exception occurs. In that case
the deletes
argument will contain the Delete
instances
that have not be successfully applied.batch(java.util.List, Object[])
public void put(Put put) throws IOException
HTableInterface
If isAutoFlush
is false, the update is buffered
until the internal buffer is full.
put
in interface HTableInterface
put
- The data to put.
IOException
- if a remote or network exception occurs.public void put(List<Put> puts) throws IOException
HTableInterface
If isAutoFlush
is false, the update is buffered
until the internal buffer is full.
put
in interface HTableInterface
puts
- The list of mutations to apply. The batch put is done by
aggregating the iteration of the Puts over the write buffer
at the client-side for a single RPC call.
IOException
- if a remote or network exception occurs.public Result increment(Increment increment) throws IOException
HTableInterface
This operation does not appear atomic to readers. Increments are done under a single row lock, so write operations to a row are synchronized, but readers do not take row locks so get and scan operations can see this operation partially completed.
increment
in interface HTableInterface
increment
- object that specifies the columns and amounts to be used
for the increment operations
IOException
- epublic long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount) throws IOException
HTableInterface
Equivalent to {@link #incrementColumnValue(byte[], byte[], byte[],
long, boolean) incrementColumnValue}(row, family, qualifier, amount,
<b>true</b>)
incrementColumnValue
in interface HTableInterface
row
- The row that contains the cell to increment.family
- The column family of the cell to increment.qualifier
- The column qualifier of the cell to increment.amount
- The amount to increment the cell with (or decrement, if the
amount is negative).
IOException
- if a remote or network exception occurs.public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount, boolean writeToWAL) throws IOException
HTableInterface
amount
and
written to the specified column.
Setting writeToWAL to false means that in a fail scenario, you will lose any increments that have not been flushed.
incrementColumnValue
in interface HTableInterface
row
- The row that contains the cell to increment.family
- The column family of the cell to increment.qualifier
- The column qualifier of the cell to increment.amount
- The amount to increment the cell with (or decrement, if the
amount is negative).writeToWAL
- if true
, the operation will be applied to the
Write Ahead Log (WAL). This makes the operation slower but safer, as if
the call returns successfully, it is guaranteed that the increment will
be safely persisted. When set to false
, the call may return
successfully before the increment is safely persisted, so it's possible
that the increment be lost in the event of a failure happening before the
operation gets persisted.
IOException
- if a remote or network exception occurs.public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put) throws IOException
checkAndPut
in interface HTableInterface
row
- to checkfamily
- column familyqualifier
- column qualifiervalue
- the expected valueput
- put to execute if value matches.
IOException
public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, byte[] value, Delete delete) throws IOException
checkAndDelete
in interface HTableInterface
row
- to checkfamily
- column familyqualifier
- column qualifiervalue
- the expected valuedelete
- delete to execute if value matches.
IOException
public boolean exists(Get get) throws IOException
This will return true if the Get matches one or more keys, false if not.
This is a server-side call so it prevents any data from being transfered to the client.
exists
in interface HTableInterface
get
- param to check for
IOException
public void flushCommits() throws IOException
Put
operations.
This method gets called once automatically for every Put
or batch
of Put
s (when batch(List)
is used) when
isAutoFlush()
is true
.
flushCommits
in interface HTableInterface
IOException
- if a remote or network exception occurs.public void close() throws IOException
HTableInterface
close
in interface HTableInterface
IOException
- if a remote or network exception occurs.public RowLock lockRow(byte[] row) throws IOException
HTableInterface
lockRow
in interface HTableInterface
row
- The row to lock.
RowLock
containing the row and lock id.
IOException
- if a remote or network exception occurs.RowLock
,
HTableInterface.unlockRow(org.apache.hadoop.hbase.client.RowLock)
public void unlockRow(RowLock rl) throws IOException
HTableInterface
unlockRow
in interface HTableInterface
rl
- The row lock to release.
IOException
- if a remote or network exception occurs.RowLock
,
HTableInterface.unlockRow(org.apache.hadoop.hbase.client.RowLock)
public void clearRegionCache()
public boolean isAutoFlush()
HTableInterface
isAutoFlush
in interface HTableInterface
true
if 'auto-flush' is enabled (default), meaning
Put
operations don't get buffered/delayed and are immediately
executed.public void setAutoFlush(boolean autoFlush)
setAutoFlush(boolean, boolean)
autoFlush
- Whether or not to enable 'auto-flush'.public void setAutoFlush(boolean autoFlush, boolean clearBufferOnFail)
When enabled (default), Put
operations don't get buffered/delayed
and are immediately executed. Failed operations are not retried. This is
slower but safer.
Turning off autoFlush
means that multiple Put
s will be
accepted before any RPC is actually sent to do the write operations. If the
application dies before pending writes get flushed to HBase, data will be
lost.
When you turn autoFlush
off, you should also consider the
clearBufferOnFail
option. By default, asynchronous Put
requests will be retried on failure until successful. However, this can
pollute the writeBuffer and slow down batching performance. Additionally,
you may want to issue a number of Put requests and call
flushCommits()
as a barrier. In both use cases, consider setting
clearBufferOnFail to true to erase the buffer after flushCommits()
has been called, regardless of success.
autoFlush
- Whether or not to enable 'auto-flush'.clearBufferOnFail
- Whether to keep Put failures in the writeBufferflushCommits()
public long getWriteBufferSize()
The default value comes from the configuration parameter
hbase.client.write.buffer
.
public void setWriteBufferSize(long writeBufferSize) throws IOException
If the new size is less than the current amount of data in the write buffer, the buffer gets flushed.
writeBufferSize
- The new write buffer size, in bytes.
IOException
- if a remote or network exception occurs.public ArrayList<Put> getWriteBuffer()
public static void setRegionCachePrefetch(byte[] tableName, boolean enable) throws ZooKeeperConnectionException
tableName
- name of table to configure.enable
- Set to true to enable region cache prefetch. Or set to
false to disable it.
ZooKeeperConnectionException
public static void setRegionCachePrefetch(org.apache.hadoop.conf.Configuration conf, byte[] tableName, boolean enable) throws ZooKeeperConnectionException
conf
- The Configuration object to use.tableName
- name of table to configure.enable
- Set to true to enable region cache prefetch. Or set to
false to disable it.
ZooKeeperConnectionException
public static boolean getRegionCachePrefetch(org.apache.hadoop.conf.Configuration conf, byte[] tableName) throws ZooKeeperConnectionException
conf
- The Configuration object to use.tableName
- name of table to check
ZooKeeperConnectionException
public static boolean getRegionCachePrefetch(byte[] tableName) throws ZooKeeperConnectionException
tableName
- name of table to check
ZooKeeperConnectionException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |