eclipse远程操作hbase

一、新建本地java工程
file->new->java project
 
二、添加jar包和配置文件
1、添加JAR包
  右击Propertie在弹出的快捷菜单中选择Java Build Path对话框,在该对话框中单击Libraries选项卡,在该选项卡下单击
Add External JARs按钮,定位到$HBASE/lib目录下,并选取如下JAR包。
hadoop-core-1.0.0.jar
commons-loggings-version.jar
commons-cli-version.jar
commons-lang-version.jar
commons-configuration-version.jar
hbase-0.94.1.jar
zookeeper-3.4.3.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
log4j-1.2.16.jar
protobuf-java-2.4.1.jar
2、添加hbase-site.xml配置文件
  在工程根目录下创建conf文件夹,将$HBASE_HOME/conf/目录中的hbase-site.xml文件复制到该文件夹中。通过右键
选择Propertie->Java Build Path->Libraries->Add Class Folder。
 
3、windows下开发HBase应用程序,HBase部署在linux环境中,在运行调试时可能会出现无法找到主机,类似异常信息如下:java.net.UnknownHostException: unknown host: master
解决办法如下:在C:\WINDOWS\system32\drivers\etc\hosts文件中添加如下信息
192.168.2.34 master
 
4、HBase Java API简单用例
 1 package com.hbase.examples;
 2 
 3 import java.io.IOException;
 4 import org.apache.hadoop.conf.Configuration;       
 5 import org.apache.hadoop.hbase.HBaseConfiguration;       
 6 import org.apache.hadoop.hbase.HColumnDescriptor;       
 7 import org.apache.hadoop.hbase.HTableDescriptor;  
 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.Result;       
13 import org.apache.hadoop.hbase.client.ResultScanner;       
14 import org.apache.hadoop.hbase.client.Scan;       
15 import org.apache.hadoop.hbase.client.Put;       
16 import org.apache.hadoop.hbase.util.Bytes;  
17 
18 public class HBaseTestCase {
19     //声明静态配置    HBaseConfiguration
20     static Configuration cfg = HBaseConfiguration.create();
21     
22     //创建一张表,通过HBaseAdmin HTableDescriptor来创建
23     public static void creat(String tablename,String columnFamily) throws Exception{
24         HBaseAdmin admin = new HBaseAdmin(cfg);
25         if(admin.tableExists(tablename)){
26             System.out.println("table Exists!");
27             //System.exit(0);
28         }else{
29             HTableDescriptor tableDesc = new HTableDescriptor(tablename);
30             tableDesc.addFamily(new HColumnDescriptor(columnFamily));
31             admin.createTable(tableDesc);
32             System.out.println("create table success!");
33         }
34     }
35     
36     //添加一条数据,通过HTable Put 为已经存在的表来添加数据
37     public static void put(String tablename,String row,String columnFamily,String column,String data) throws Exception{
38         HTable table = new HTable(cfg,tablename);
39         Put p1 = new Put(Bytes.toBytes(row));
40         p1.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(data));
41         table.put(p1);
42         System.out.println("put '"+row+"','"+columnFamily+":"+column+"','"+data+"'");
43     }
44     
45     public static void get(String tablename,String row) throws IOException{
46         HTable table = new HTable(cfg,tablename);
47         Get g = new Get(Bytes.toBytes(row));
48         Result result = table.get(g);
49         System.out.println("Get: "+result);
50     }
51     
52     //显示所有数据,通过HTable Scan来获取已有表的信息
53     public static void scan(String tablename) throws Exception{
54         HTable table = new HTable(cfg,tablename);
55         Scan s = new Scan();
56         ResultScanner rs = table.getScanner(s);
57         for(Result r:rs){
58             System.out.println("Scan: "+r);
59         }
60     }
61     
62     public static boolean delete(String tablename) throws IOException{
63         HBaseAdmin admin = new HBaseAdmin(cfg);
64         if(admin.tableExists(tablename)){
65             try{
66                 admin.disableTable(tablename);
67                 admin.deleteTable(tablename);
68             }catch(Exception ex){
69                 ex.printStackTrace();
70                 return false;
71             }
72         }
73         return true;
74     }
75     
76     public static void main(String [] args){
77         String tablename="hbase_tb";
78         String columnFamily="cf";
79         
80         try{
81             HBaseTestCase.creat(tablename, columnFamily);
82             HBaseTestCase.put(tablename, "row2", columnFamily, "cl1", "data");
83             HBaseTestCase.get(tablename, "row1");
84             HBaseTestCase.scan(tablename);
85             if(true==HBaseTestCase.delete(tablename)) System.out.println("Delete table:"+tablename+"success!");
86         }
87         catch (Exception e){
88             e.printStackTrace();
89         }
90     }
91 }

 

package com.hbase.examples;

import java.io.IOException;       
import java.util.ArrayList;       
import java.util.List;       
        
import org.apache.hadoop.conf.Configuration;       
import org.apache.hadoop.hbase.HBaseConfiguration;       
import org.apache.hadoop.hbase.HColumnDescriptor;       
import org.apache.hadoop.hbase.HTableDescriptor;       
import org.apache.hadoop.hbase.KeyValue;       
import org.apache.hadoop.hbase.MasterNotRunningException;       
import org.apache.hadoop.hbase.ZooKeeperConnectionException;       
import org.apache.hadoop.hbase.client.Delete;       
import org.apache.hadoop.hbase.client.Get;       
import org.apache.hadoop.hbase.client.HBaseAdmin;       
import org.apache.hadoop.hbase.client.HTable;       
import org.apache.hadoop.hbase.client.Result;       
import org.apache.hadoop.hbase.client.ResultScanner;       
import org.apache.hadoop.hbase.client.Scan;       
import org.apache.hadoop.hbase.client.Put;       
import org.apache.hadoop.hbase.util.Bytes;       
        
public class HBaseTest {         
           
    private static Configuration conf =null;    
     /**  
      * 初始化配置  
     */    
     static {    
         conf = HBaseConfiguration.create();    
     }    
         
    /**    
     * 创建一张表    
     */      
    public static void creatTable(String tableName, String[] familys) throws Exception {       
        HBaseAdmin admin = new HBaseAdmin(conf);       
        if (admin.tableExists(tableName)) {       
            System.out.println("table already exists!");       
        } else {       
            HTableDescriptor tableDesc = new HTableDescriptor(tableName);       
            for(int i=0; i<familys.length; i++){       
                tableDesc.addFamily(new HColumnDescriptor(familys[i]));       
            }       
            admin.createTable(tableDesc);       
            System.out.println("create table " + tableName + " ok.");       
        }        
    }       
           
    /**    
     * 删除表    
     */      
    public static void deleteTable(String tableName) throws Exception {       
       try {       
           HBaseAdmin admin = new HBaseAdmin(conf);       
           admin.disableTable(tableName);       
           admin.deleteTable(tableName);       
           System.out.println("delete table " + tableName + " ok.");       
       } catch (MasterNotRunningException e) {       
           e.printStackTrace();       
       } catch (ZooKeeperConnectionException e) {       
           e.printStackTrace();       
       }       
    }       
            
    /**    
     * 插入一行记录    
     */      
    public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value)       
            throws Exception{       
        try {       
            HTable table = new HTable(conf, tableName);       
            Put put = new Put(Bytes.toBytes(rowKey));       
            put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));       
            table.put(put);       
            System.out.println("insert recored " + rowKey + " to table " + tableName +" ok.");       
        } catch (IOException e) {       
            e.printStackTrace();       
        }       
    }       
        
    /**    
     * 删除一行记录    
     */      
    public static void delRecord (String tableName, String rowKey) throws IOException{       
        HTable table = new HTable(conf, tableName);       
        List list = new ArrayList();       
        Delete del = new Delete(rowKey.getBytes());       
        list.add(del);       
        table.delete(list);       
        System.out.println("del recored " + rowKey + " ok.");       
    }       
            
    /**    
     * 查找一行记录    
     */      
    public static void getOneRecord (String tableName, String rowKey) throws IOException{       
        HTable table = new HTable(conf, tableName);       
        Get get = new Get(rowKey.getBytes());       
        Result rs = table.get(get);       
        for(KeyValue kv : rs.raw()){       
            System.out.print(new String(kv.getRow()) + " " );       
            System.out.print(new String(kv.getFamily()) + ":" );       
            System.out.print(new String(kv.getQualifier()) + " " );       
            System.out.print(kv.getTimestamp() + " " );       
            System.out.println(new String(kv.getValue()));       
        }       
    }       
            
    /**    
     * 显示所有数据    
     */      
    public static void getAllRecord (String tableName) {       
        try{       
             HTable table = new HTable(conf, tableName);       
             Scan s = new Scan();       
             ResultScanner ss = table.getScanner(s);       
             for(Result r:ss){       
                 for(KeyValue kv : r.raw()){       
                    System.out.print(new String(kv.getRow()) + " ");       
                    System.out.print(new String(kv.getFamily()) + ":");       
                    System.out.print(new String(kv.getQualifier()) + " ");       
                    System.out.print(kv.getTimestamp() + " ");       
                    System.out.println(new String(kv.getValue()));       
                 }       
             }       
        } catch (IOException e){       
            e.printStackTrace();       
        }       
    }       
           
    public static void  main (String [] agrs) {       
        try {       
            String tablename = "scores";       
            String[] familys = {"grade", "course"};       
            HBaseTest.creatTable(tablename, familys);       
                    
            //add record zkb       
            HBaseTest.addRecord(tablename,"zkb","grade","","5");       
            HBaseTest.addRecord(tablename,"zkb","course","","90");       
            HBaseTest.addRecord(tablename,"zkb","course","math","97");       
            HBaseTest.addRecord(tablename,"zkb","course","art","87");       
            //add record  baoniu       
            HBaseTest.addRecord(tablename,"baoniu","grade","","4");       
            HBaseTest.addRecord(tablename,"baoniu","course","math","99");       
                    
            System.out.println("===========get one record========");       
            HBaseTest.getOneRecord(tablename, "zkb");       
                    
            System.out.println("===========show all record========");       
            HBaseTest.getAllRecord(tablename);       
                    
            System.out.println("===========del one record========");       
            HBaseTest.delRecord(tablename, "baoniu");       
            HBaseTest.getAllRecord(tablename);       
                    
            System.out.println("===========show all record========");       
            HBaseTest.getAllRecord(tablename);       
        } catch (Exception e) {       
            e.printStackTrace();       
        }       
    }       
}

 

posted on 2013-10-28 15:19  jingyunyb  阅读(5220)  评论(0编辑  收藏  举报