使用Eclipse连接hbase1.1 hadoop2.6

配置项目环境:

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

  其它文章介绍将hbase-site.xml拷贝过来就可以了,我是将hadoop下的两个文件也拷贝过来了。

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

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


3.测试通过的代码:


package use.hbase.api;  
  
import java.io.IOException;  
  
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.client.Get;  
import org.apache.hadoop.hbase.client.HBaseAdmin;  
import org.apache.hadoop.hbase.client.HTable;  
import org.apache.hadoop.hbase.client.Put;  
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.util.Bytes;  
  
public class HBaseTest {  
  
    static Configuration cfg = HBaseConfiguration.create();  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        String tablename = "hbase_table";  
        String columnFamily = "cf";  
        try {  
            HBaseTestCase.create(tablename, columnFamily);  
            HBaseTestCase.put(tablename, "row1", columnFamily, "cl1", "hello world!");  
            HBaseTestCase.get(tablename, "row1");  
            HBaseTestCase.scan(tablename);  
            if(true==HBaseTestCase.delete(tablename)) {  
                System.out.println("Delete table:"+tablename+" success!");  
            }  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
          
    }  
      
    /** 
     * create a table :table_name(columnFamily)  
     * @param tablename 
     * @param columnFamily 
     * @throws Exception 
     */  
    public static void create(String tablename, String columnFamily) throws Exception {  
        HBaseAdmin admin = new HBaseAdmin(cfg);  
        if(admin.tableExists(tablename)) {  
            System.out.println("table exists!");  
            System.exit(0);  
        }  
        else {  
            HTableDescriptor tableDesc = new HTableDescriptor(tablename);  
            tableDesc.addFamily(new HColumnDescriptor(columnFamily));  
            admin.createTable(tableDesc);  
            System.out.println("create table success!");  
        }  
    }  
  
    /** 
     * put a row data into table  
     * @param tablename 
     * @param row 
     * @param columnFamily 
     * @param column 
     * @param data 
     * @throws Exception 
     */  
    public static void put(String tablename, String row, String columnFamily, String column, String data) throws Exception{  
        HTable table = new HTable(cfg, tablename);  
        Put p1 = new Put(Bytes.toBytes(row));  
        p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));  
        table.put(p1);  
        System.out.println("put '"+row+"', '"+columnFamily+":"+column+"', '"+data+"'");  
          
    }  
      
    /** 
     * get a row data from a table 
     * @param tablename 
     * @param row 
     * @throws Exception 
     */  
    public static void get(String tablename, String row) throws Exception {  
        HTable table = new HTable(cfg, tablename);  
        Get get = new Get(Bytes.toBytes(row));  
        Result result = table.get(get);  
        System.out.println("Get: "+result);  
    }  
      
    /** 
     * show all data from a table 
     * @param tablename 
     * @throws Exception 
     */  
    public static void scan(String tablename) throws Exception {  
        HTable table = new HTable(cfg, tablename);  
        Scan s =new Scan();  
        ResultScanner rs = table.getScanner(s);  
        for(Result r:rs) {  
            System.out.println("Scan: "+r);  
        }  
    }  
  
    /** 
     * delete a table's data 
     * @param tablename 
     * @return 
     * @throws IOException 
     */  
    public static boolean delete(String tablename) throws IOException {  
        HBaseAdmin admin = new HBaseAdmin(cfg);  
        if(admin.tableExists(tablename)) {  
            try {  
                admin.disableTable(tablename);  
                admin.deleteTable(tablename);  
            } catch (Exception e) {  
                // TODO: handle exception  
                e.printStackTrace();  
                return false;  
            }  
        }  
        return true;  
    }  
}  
在hdfs中可以看到



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值