关于Hbase在javaAPI操作表的操作

2 篇文章 0 订阅
话不多说,直接看代码就能看清楚每个API是干嘛的。
package hbase;

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.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.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 {
	//声明静态配置 HBaseConfiguration
	   static Configuration cfg=HBaseConfiguration.create();
	 //创建一张表,通过HBaseAdmin HTableDescriptor来创建
	    public static void creat(String tablename,String [] cfs) 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);
	            for(int i=0;i<cfs.length;i++){
	            tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
	            }
	            admin.createTable(tableDesc);
	            System.out.println("create table success!");
	        }
	    }
		//插入数据
	    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));
	    	 for(int i=0;i<columnFamily.length;i++){
	    	p1.add(Bytes.toBytes(columnFamily[i]), 				
	    		Bytes.toBytes(column),
	    		Bytes.toBytes(data));
	    	table.put(p1);
	    	 }
	    	System.out.println("put '"+row+"','"+columnFamily+":"+column+"','"+data+"'");
	    	
	    }
		//查询
	    public static void get(String tablename, String row) throws Exception
	    {
	    	HTable table = new HTable(cfg, tablename);

	    	Get g = new Get(Bytes.toBytes(row));

	    	Result result = table.get(g);

	    	System.out.println("Get :" + result);

	    }
		//scan查询,可用于filter
	    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);
	    	}
	            rs.close();
	    }
		//删除数据
   public static void  delete(String tablename ,String rowkey) throws IOException{
            
		HTable table = new HTable(cfg, tablename);
		List list =new ArrayList();
		Delete dl =new Delete(rowkey.getBytes());
		list.add(dl);
		table.delete(list);
		System.out.println("delete to success");
    }
		//删除表
   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 ex){
                       ex.printStackTrace();
                       return false;
               }
               
       }
       return true;
}
   public static void  main (String [] agrs) {
       String tablename="hbase_tb";
       String [] columnFamily={"cf"};
     
       try {                     
    	   hbaseTest.creat(tablename, columnFamily);
    	   hbaseTest.put(tablename, "row1", columnFamily, "cl1", "data");
       	   hbaseTest.get(tablename, "row1");
    	   hbaseTest.scan(tablename);
/*           if(true==HBaseTestCase.delete(tablename))
               System.out.println("Delete table:"+tablename+"success!");
*/           
   }
   catch (Exception e) {
       e.printStackTrace();
   }    
}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值