配置项目环境:

1.当第三方应用访问HBase时,需要首先访问ZooKeeper(由$HBASE_HOME/conf/hbase-site.xml设置),因此需要通过classpath来指定HBase配置文件的位置(即$HBASE_HOME/conf的位置)

使用Eclipse连接hbase1.1 hadoop2.6  其它文章介绍将hbase-site.xml拷贝过来就可以了,我是将hadoop下的两个文件也拷贝过来了。

2.将hbase目录lib下的jar包拷贝在工程中,具体拷贝jar包是用maven还是用Build path就不介绍了

3.在/etc/system32/driver/etc/hosts中将hadoop的主机和IP标识写上:

使用Eclipse连接hbase1.1 hadoop2.6

3.测试通过的代码:


[html] view plain copy
  1. package use.hbase.api;    
  2.     
  3. import java.io.IOException;    
  4.     
  5. import org.apache.hadoop.conf.Configuration;    
  6. import org.apache.hadoop.hbase.HBaseConfiguration;    
  7. import org.apache.hadoop.hbase.HColumnDescriptor;    
  8. import org.apache.hadoop.hbase.HTableDescriptor;    
  9. import org.apache.hadoop.hbase.client.Get;    
  10. import org.apache.hadoop.hbase.client.HBaseAdmin;    
  11. import org.apache.hadoop.hbase.client.HTable;    
  12. import org.apache.hadoop.hbase.client.Put;    
  13. import org.apache.hadoop.hbase.client.Result;    
  14. import org.apache.hadoop.hbase.client.ResultScanner;    
  15. import org.apache.hadoop.hbase.client.Scan;    
  16. import org.apache.hadoop.hbase.util.Bytes;    
  17.     
  18. public class HBaseTest {    
  19.     
  20.     static Configuration cfg = HBaseConfiguration.create();    
  21.     /**   
  22.      * @param args   
  23.      */    
  24.     public static void main(String[] args) {    
  25.         // TODO Auto-generated method stub    
  26.         String tablename = "hbase_table";    
  27.         String columnFamily = "cf";    
  28.         try {    
  29.             HBaseTestCase.create(tablename, columnFamily);    
  30.             HBaseTestCase.put(tablename, "row1", columnFamily, "cl1", "hello world!");    
  31.             HBaseTestCase.get(tablename, "row1");    
  32.             HBaseTestCase.scan(tablename);    
  33.             if(true==HBaseTestCase.delete(tablename)) {    
  34.                 System.out.println("Delete table:"+tablename+" success!");    
  35.             }    
  36.         } catch (Exception e) {    
  37.             // TODO Auto-generated catch block    
  38.             e.printStackTrace();    
  39.         }    
  40.             
  41.             
  42.     }    
  43.         
  44.     /**   
  45.      * create a table :table_name(columnFamily)    
  46.      * @param tablename   
  47.      * @param columnFamily   
  48.      * @throws Exception   
  49.      */    
  50.     public static void create(String tablename, String columnFamily) throws Exception {    
  51.         HBaseAdmin admin = new HBaseAdmin(cfg);    
  52.         if(admin.tableExists(tablename)) {    
  53.             System.out.println("table exists!");    
  54.             System.exit(0);    
  55.         }    
  56.         else {    
  57.             HTableDescriptor tableDesc = new HTableDescriptor(tablename);    
  58.             tableDesc.addFamily(new HColumnDescriptor(columnFamily));    
  59.             admin.createTable(tableDesc);    
  60.             System.out.println("create table success!");    
  61.         }    
  62.     }    
  63.     
  64.     /**   
  65.      * put a row data into table    
  66.      * @param tablename   
  67.      * @param row   
  68.      * @param columnFamily   
  69.      * @param column   
  70.      * @param data   
  71.      * @throws Exception   
  72.      */    
  73.     public static void put(String tablename, String row, String columnFamily, String column, String data) throws Exception{    
  74.         HTable table = new HTable(cfg, tablename);    
  75.         Put p1 = new Put(Bytes.toBytes(row));    
  76.         p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));    
  77.         table.put(p1);    
  78.         System.out.println("put '"+row+"', '"+columnFamily+":"+column+"', '"+data+"'");    
  79.             
  80.     }    
  81.         
  82.     /**   
  83.      * get a row data from a table   
  84.      * @param tablename   
  85.      * @param row   
  86.      * @throws Exception   
  87.      */    
  88.     public static void get(String tablename, String row) throws Exception {    
  89.         HTable table = new HTable(cfg, tablename);    
  90.         Get get = new Get(Bytes.toBytes(row));    
  91.         Result result = table.get(get);    
  92.         System.out.println("Get: "+result);    
  93.     }    
  94.         
  95.     /**   
  96.      * show all data from a table   
  97.      * @param tablename   
  98.      * @throws Exception   
  99.      */    
  100.     public static void scan(String tablename) throws Exception {    
  101.         HTable table = new HTable(cfg, tablename);    
  102.         Scan s =new Scan();    
  103.         ResultScanner rs = table.getScanner(s);    
  104.         for(Result r:rs) {    
  105.             System.out.println("Scan: "+r);    
  106.         }    
  107.     }    
  108.     
  109.     /**   
  110.      * delete a table's data   
  111.      * @param tablename   
  112.      * @return   
  113.      * @throws IOException   
  114.      */    
  115.     public static boolean delete(String tablename) throws IOException {    
  116.         HBaseAdmin admin = new HBaseAdmin(cfg);    
  117.         if(admin.tableExists(tablename)) {    
  118.             try {    
  119.                 admin.disableTable(tablename);    
  120.                 admin.deleteTable(tablename);    
  121.             } catch (Exception e) {    
  122.                 // TODO: handle exception    
  123.                 e.printStackTrace();    
  124.                 return false;    
  125.             }    
  126.         }    
  127.         return true;    
  128.     }    
  129. }    
在hdfs中可以看到

使用Eclipse连接hbase1.1 hadoop2.6

相关文章:

  • 2021-10-16
  • 2021-11-22
  • 2021-05-29
  • 2021-11-21
  • 2021-11-22
  • 2021-07-03
  • 2021-06-23
  • 2021-04-06
猜你喜欢
  • 2022-12-23
  • 2021-09-20
  • 2021-07-10
  • 2021-04-24
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
相关资源
相似解决方案