Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hbase interact with shell


Published on

  • Be the first to comment

Hbase interact with shell

  1. 1.
  2. 2. LISTList all tables in HBaseSyntax :Htase :> list
  3. 3. CREATECreate table;Pass table nameSyntax :Hbase :> creat ‘tablename’,’colFamily’
  4. 4. MORE EXAPLEShbase> create tableToBeCreated, {NAME => colFammily1, VERSIONS => 5}hbase> create tableToBeCreated, {NAME => colFammily1}, {NAME => colFammily2}, {NAME => f3}hbase> # The above in shorthand would be the following:hbase> create tableToBeCreated, colFammily1, colFammily2, f3hbase> create tableToBeCreated, {NAME => colFammily1, VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true
  5. 5. DESCRIBEDescribe the named table:e.g.hbase> describe table1
  6. 6. COUNTCount the number of rows in a table.This operation may take a LONG time (Run $HADOOP_HOME/bin/hadoop jar hbase.jar rowcount to run a counting mapreduce job).Current count is shown every 1000 rows by default.Count interval may be optionally specified.Examples:hbase> count t1 hbase> count t1, 100
  7. 7. DELETEPut a delete cell value at specified table/row/column and optionally timestamp coordinates.Eg:Delete ‘table’,’colfam:columnname’
  8. 8. DELETEALLDelete all cells in a given row; pass a table name, row, and optionally a column and timestampDelete ‘table’,’rowkey’
  9. 9. DISABLEDisable the named table:e.g."hbase> disable t1"
  10. 10. DROPDrop the named table. Table must first be disabled.Eg:Drop ‘tablename’
  11. 11. ENABLEEnable the named tableEg:Enable ‘tablename’
  12. 12. EXISTSDoes the named table exist?e.g."hbase> exists t1"
  13. 13. GETGet row or cell contents; pass table name, row,hbase> get t1, r1hbase> get t1, r1, {COLUMN => c1}hbase> get t1, r1, {COLUMN => [c1, c2, c3]}hbase> get t1, r1, {COLUMN => c1, TIMESTAMP => ts1}hbase> get t1, r1, {COLUMN => c1, TIMESTAMP => ts1, VERSIONS => 4}
  14. 14. PUTPut a cell value at specified table/row/column and optionally timestamp coordinates.To put a cell value into table t1 at row r1 under column c1do:hbase> put t1, r1, c1, value
  15. 15. SCANScan a tableTo scan all members of a column family, leave the qualifier empty as in col_family:.hbase> scan t1’
  16. 16. STATUSShow cluster status.Can be summary, simple, or detailed.The default is summary.Examples:hbase> statushbase> status simplehbase> status summaryhbase> status detailed
  17. 17. SHUTDOWNShut down the cluster.
  18. 18. TRUNCATEDisables, drops and recreates the specified table.
  19. 19. VERSIONOutput this HBase version
  20. 20. ALTERAlter column family schema; pass table name and a dictionary specifying new column family schema. Dictionaries are described below in the GENERAL NOTES section. Dictionary must include name of column family to alter.For example,To change or add the f1 column family in table t1 from defaults to instead keep a maximum of 5 cell VERSIONS, do:hbase> alter t1, {NAME => f1, VERSIONS => 5}To delete the f1 column family in table t1, do:hbase> alter t1, {NAME => f1, METHOD => delete}
  21. 21. EXITTypehbase> exitto leave the HBase Shell
  22. 22. SCRIPTINGYou can pass scripts to the HBase Shell by doing the following:bin/hbase shell PATH_TO_SCRIPT
  23. 23. SOME HBASE TRICKSecho "create user, info" | hbase shellUsing this command you can create table called use with column family name info without entering to the hbase shell.for i in 0..4 do put "user", "user_#{i}", "info:email", "user_#{i}" EndThis will create 4 users with user#1 to 4 and put email address too
  24. 24. SCRIPT FOR HBASEcat hbase_user_part_scan.txt scan user, {STARTROW => user_1, STOPROW => user_3} exit$ hbase shell hbase_user_part_scan.txtThis script will scan the user table and give output.
  25. 25. USING BASH SCRIPT#!/bin/bash TABLE=$1 STARTROW=$2 STOPROW=$3 exec hbase shell <<EOF scan "${TABLE}", {STARTROW => "${STARTROW}", STOPROW => "${STOPROW}"} EOF$ ./ user user_1 user_3
  26. 26. SEE WHEN A RECORD WAS ADDEDget user, user_1‘ show you the human understable time likeSun Aug 12 11:28:21 +0200 2012
  27. 27. SCHEMA DESIGNWe can take advantage of table schema and store all information realated to a given user in one row (but in two separate column families: info and friend). Last but not least, the rowkey can be simply the user’s unique username.It leads us to the following table schema:Just visit this link for understanding more on schema designing : schema-design-case-studies
  28. 28. ADDING MORE DATA TO HBASE TABLES$ hbase shellhbase(main):001:0> create user, info, friendhbase(main):002:0> put user, username1, friend:username2, childhoodhbase(main):003:0> put user, username1, friend:username3, childhoodhbase(main):004:0> put user, username2, friend:username3, childhoodhbase(main):005:0> put user, username2, friend:username4, childhoodhbase(main):006:0> put user, username3, friend:username5, childhoodhbase(main):007:0> put user, username1, friend:username5, childhood‘Or you can write an script to create these value
  29. 29. SPECIFYING DIFFERENT CONFIGURATION FOR HBASE SHELLHBase Shell is started by using the hbase shell command. This command uses the HBase configuration file (hbase-site.xml) for the client to find the cluster to connect to. After connecting to the cluster, it starts a prompt, waiting for commands. As shown in the following code, you can also use the -- config option, which allows you to pass a differentconfiguration for HBase Shell to use:hbase --config <configuration_directory> shell
  30. 30. SOME HELPFUL COMMANDS : EXERCISEGet a specified row by using the get command:Delete a specified cell by using the delete command:Delete all the cells in a given row using the deleteall command:enable the balancer
  31. 31. ANSWERSget t1, row1delete t1, row1, f1:c1‘deleteall t1, row1‘balance_switch true
  33. 33. YOU WANT TO QUERY HBASE USING SQL????Use hive to query . For that you need to create a table in hive as external table to hbase as follows:create external table hbase_tablehbase (key string, v01 string, v02 string, v03 string) stored by org.apache.hadoop.hive.hbase.HBaseStorageHandler with serdeproperties ("hbase.columns.mapping" = ":key,n:v01,n:v02,n:v03") tblproperties("" = "tablehbase");select * from hbase_hly_temp where v01=808C;You can give this query in hive to get data from hbase  and you have lot of other options with hive just visit :
  34. 34. BACKING UP AND RESTORING HBASE DATA• Full shutdown backup using distcp• Using CopyTable to copy data from one table to another• Exporting an HBase table to dump files on HDFS• Restoring HBase data by importing dump files from HDFS• Backing up NameNode metadata• Backing up region starting keys• Cluster replication
  35. 35. Gmail dwivedishashwat@gmail.comTwitter shashwat_2010Facebook shriparv@gmail.comSkype shriparv