在Eclipse中运行JAVA代码远程操作HBase的示例

来源:互联网 发布:mac开启root权限 编辑:程序博客网 时间:2024/05/21 11:00
下面是一个在Windows的Eclipse中通过JAVA操作一个Linux上运行的hbase的示例。
Hbase的配置需要注意下面一些要点:
1,服务器端用主机名配置hadoop和hbase,不要用IP
比如如下:
<property>  
 <name>hbase.zookeeper.quorum</name>  
 <value>hadoopsrv</value>  
</property>
2,hbase运行的机器上的机器名不能叫localhost
改/etc/sysconfig/network中的HOSTNAME
比如:
HOSTNAME=hadoopsrv
3,修改eclipse运行的windows客户端的C:\Windows\System32\drivers\etc\hosts文件.
对应到hbase运行服务器的ip,比如:
192.168.2.6 hadoopsrv
JAVA代码如下

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. package org.apache.hadoop.hbase;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8. import org.apache.hadoop.hbase.client.Delete;  
  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.HTablePool;  
  13. import org.apache.hadoop.hbase.client.Put;  
  14. import org.apache.hadoop.hbase.client.Result;  
  15. import org.apache.hadoop.hbase.client.ResultScanner;  
  16. import org.apache.hadoop.hbase.client.Scan;  
  17.   
  18. public class HbaseTest {  
  19.     private HBaseAdmin admin = null;  
  20.     // 定义配置对象HBaseConfiguration  
  21.     private HBaseConfiguration cfg = null;  
  22.   
  23.     public HbaseTest() throws Exception {  
  24.         Configuration HBASE_CONFIG = new Configuration();  
  25.   
  26.         HBASE_CONFIG.set("hbase.zookeeper.quorum""192.168.2.6");  
  27.   
  28.         HBASE_CONFIG.set("hbase.zookeeper.property.clientPort""2181");  
  29.   
  30.         cfg = new HBaseConfiguration(HBASE_CONFIG);  
  31.   
  32.         admin = new HBaseAdmin(cfg);  
  33.     }  
  34.   
  35.     // 创建一张表,指定表名,列族  
  36.     public void createTable(String tableName, String columnFarily)  
  37.             throws Exception {  
  38.   
  39.         if (admin.tableExists(tableName)) {  
  40.             System.out.println(tableName + "存在!");  
  41.             System.exit(0);  
  42.         } else {  
  43.             HTableDescriptor tableDesc = new HTableDescriptor(tableName);  
  44.             tableDesc.addFamily(new HColumnDescriptor(columnFarily));  
  45.             admin.createTable(tableDesc);  
  46.             System.out.println("创建表成功!");  
  47.         }  
  48.     }  
  49.   
  50.     // Hbase获取所有的表信息  
  51.     public List getAllTables() {  
  52.         List<String> tables = null;  
  53.         if (admin != null) {  
  54.             try {  
  55.                 HTableDescriptor[] allTable = admin.listTables();  
  56.                 if (allTable.length > 0)  
  57.                     tables = new ArrayList<String>();  
  58.                 for (HTableDescriptor hTableDescriptor : allTable) {  
  59.                     tables.add(hTableDescriptor.getNameAsString());  
  60.                     System.out.println(hTableDescriptor.getNameAsString());  
  61.                 }  
  62.             } catch (IOException e) {  
  63.                 e.printStackTrace();  
  64.             }  
  65.         }  
  66.         return tables;  
  67.     }  
  68.   
  69.     // Hbase中往某个表中添加一条记录  
  70.     public boolean addOneRecord(String table, String key, String family,  
  71.             String col, byte[] dataIn) {  
  72.         HTablePool tp = new HTablePool(cfg, 1000);  
  73.         HTable tb = (HTable) tp.getTable(table);  
  74.         Put put = new Put(key.getBytes());  
  75.         put.add(family.getBytes(), col.getBytes(), dataIn);  
  76.         try {  
  77.             tb.put(put);  
  78.             System.out.println("插入数据条" + key + "成功!!!");  
  79.             return true;  
  80.         } catch (IOException e) {  
  81.             System.out.println("插入数据条" + key + "失败!!!");  
  82.             return false;  
  83.         }  
  84.     }  
  85.   
  86.     // Hbase表中记录信息的查询  
  87.     public void getValueFromKey(String table, String key) {  
  88.         HTablePool tp = new HTablePool(cfg, 1000);  
  89.         HTable tb = (HTable) tp.getTable(table);  
  90.         Get get = new Get(key.getBytes());  
  91.         try {  
  92.             Result rs = tb.get(get);  
  93.             if (rs.raw().length == 0) {  
  94.                 System.out.println("不存在关键字为" + key + "的行!!");  
  95.   
  96.             } else {  
  97.                 for (KeyValue kv : rs.raw()) {  
  98.                     System.out.println(new String(kv.getKey()) + " "  
  99.                             + new String(kv.getValue()));  
  100.                 }  
  101.   
  102.             }  
  103.         } catch (IOException e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.     }  
  107.   
  108.     // 显示所有数据,通过HTable Scan类获取已有表的信息  
  109.     public void getAllData(String tableName) throws Exception {  
  110.         HTable table = new HTable(cfg, tableName);  
  111.         Scan scan = new Scan();  
  112.         ResultScanner rs = table.getScanner(scan);  
  113.         for (Result r : rs) {  
  114.             for (KeyValue kv : r.raw()) {  
  115.                 System.out.println(new String(kv.getKey())  
  116.                         + new String(kv.getValue()));  
  117.             }  
  118.         }  
  119.     }  
  120.   
  121.     // Hbase表中记录信息的删除  
  122.     public boolean deleteRecord(String table, String key) {  
  123.         HTablePool tp = new HTablePool(cfg, 1000);  
  124.         HTable tb = (HTable) tp.getTable(table);  
  125.         Delete de = new Delete(key.getBytes());  
  126.         try {  
  127.             tb.delete(de);  
  128.             return true;  
  129.         } catch (IOException e) {  
  130.             System.out.println("删除记录" + key + "异常!!!");  
  131.             return false;  
  132.         }  
  133.     }  
  134.   
  135.     // Hbase中表的删除  
  136.     public boolean deleteTable(String table) {  
  137.         try {  
  138.             if (admin.tableExists(table)) {  
  139.                 admin.disableTable(table);  
  140.                 admin.deleteTable(table);  
  141.                 System.out.println("删除表" + table + "!!!");  
  142.             }  
  143.             return true;  
  144.         } catch (IOException e) {  
  145.             System.out.println("删除表" + table + "异常!!!");  
  146.             return false;  
  147.         }  
  148.     }  
  149.   
  150.     // 测试函数  
  151.     public static void main(String[] args) {  
  152.         try {  
  153.             HbaseTest hbase = new HbaseTest();  
  154.             // hbase.createTable("student", "fam1");  
  155.             // hbase.getAllTables();  
  156.   
  157.             // hbase.addOneRecord("student","id1","fam1","name","Jack".getBytes());  
  158.             // hbase.addOneRecord("student","id1","fam1","address","HZ".getBytes());  
  159.             // hbase.getValueFromKey("student","id1");  
  160.             // hbase.getAllData("student");  
  161.   
  162.             //hbase.deleteRecord("student", "id1");  
  163.               
  164.             hbase.deleteTable("student");  
  165.               
  166.         } catch (Exception e) {  
  167.             e.printStackTrace();  
  168.         }  
  169.     }  
  170. }
0 0
原创粉丝点击