Examples of using HBase Java APIs

Source: Internet
Author: User

Through the use of HBase APIs, the following example shows common operations on HBase, as shown below:

  1. PackageNet. bkjia. www;
  2. ImportOrg. apache. Hadoop. conf. Configuration;
  3. ImportOrg. apache. hadoop. hbase. HBaseConfiguration;
  4. ImportOrg. apache. hadoop. hbase. HColumnDescriptor;
  5. ImportOrg. apache. hadoop. hbase. HTableDescriptor;
  6. ImportOrg. apache. hadoop. hbase. KeyValue;
  7. ImportOrg. apache. hadoop. hbase. client. HBaseAdmin;
  8. ImportOrg. apache. hadoop. hbase. client. HTable;
  9. ImportOrg. apache. hadoop. hbase. client. Result;
  10. ImportOrg. apache. hadoop. hbase. client. resultworkflow;
  11. ImportOrg. apache. hadoop. hbase. client. Scan;
  12. ImportOrg. apache. hadoop. hbase. io. BatchUpdate;
  13. Public ClassHBaseDBDao {
  14. // Define the configuration object HBaseConfiguration
  15. StaticHBaseConfiguration cfg =Null;
  16. Static{
  17. Configuration configuration =NewConfiguration ();
  18. Cfg =NewHBaseConfiguration (configuration );
  19. }
  20. // Create a table and specify the table name and column family.
  21. Public Static VoidCreateTable (String tableName, String columnFarily)ThrowsException {
  22. HBaseAdmin admin =NewHBaseAdmin (cfg );
  23. If(Admin. tableExists (tableName )){
  24. System. out. println (tableName +"Does not exist! ");
  25. System. exit (0);
  26. }Else{
  27. HTableDescriptor tableDesc =NewHTableDescriptor (tableName );
  28. TableDesc. addFamily (NewHColumnDescriptor (columnFarily +":"));
  29. System. out. println ("Table created successfully! ");
  30. }
  31. }
  32. // Add data through HTable. And BatchUpdate add data to an existing table
  33. Public Static VoidAddData (String tableName, String row, String columnFamily, String column, String data)ThrowsException {
  34. HTable table =NewHTable (cfg, tableName );
  35. BatchUpdate update =NewBatchUpdate (row );
  36. Update. put (columnFamily +":"+ Column, data. getBytes ());
  37. Table. commit (update );
  38. System. out. println ("Added successfully! ");
  39. }
  40. // Display all data. Use the HTable Scan class to obtain information about existing tables.
  41. Public Static VoidGetAllData (String tableName)ThrowsException {
  42. HTable table =NewHTable (cfg, tableName );
  43. Scan scan =NewScan ();
  44. Resultpartition rs = table. getpartition (scan );
  45. For(Result r: rs ){
  46. For(KeyValue kv: r. raw ()){
  47. System. out. println (NewString (kv. getColumn () +NewString (kv. getValue ()));
  48. }
  49. }
  50. }
  51. // Test the Function
  52. Public Static VoidMain (String [] args ){
  53. Try{
  54. String tableName ="Student";
  55. HBaseDBDao. createTable (tableName,"C1");
  56. HBaseDBDao. addData (tableName,"Row1","C1","1","This is row 1 column c1: c1");
  57. HBaseDBDao. getAllData (tableName );
  58. }Catch(Exception e ){
  59. E. printStackTrace ();
  60. }
  61. }
  62. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.