Java操作HBase

环境:HBase0.95.2     Hadoop1.2.1     CentOS6.5     Eclipse

package HbaseTest;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Durability;
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;
import org.junit.Test;

public class HbaseTest
{
//声明全局静态配置 HBaseConfiguration
static Configuration cfg=HBaseConfiguration.create();

//创建一张表,通过HBaseAdmin HTableDescriptor来创建
public static void creat(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 ok.");  
     }  
     admin.close();
    System.out.println("SUCCESS  CREATE!!!");
}

//添加一条数据,通过HTable Put为已经存在的表来添加数据
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+" ' ");
	table.close();
}

//得到某一rowkey的行,这里可以添加过滤的一些条件
public static void get(String tablename,String row) throws IOException
{
	HTable table =  new  HTable(cfg,  tablename);
    Get get =  new  Get(Bytes. toBytes (row));
    Result r = table.get(get);
    for  (Cell cell : r.raw()  )
    {
        System. out .println(
                 "Rowkey :  " +Bytes. toString (r.getRow())+
                 "   Infor :  " +Bytes. toString (CellUtil.getQualifierArray(cell))+
                 "   Value :  " +Bytes. toString (CellUtil.getValueArray(cell))
                );
    }
    table.close();  
}

//显示所有数据,通过HTable Scan来获取已有表的信息
public static void scan(String tablename) throws Exception
{
    //Configuration conf = HBaseConfiguration.create();
    HTable table = new HTable(cfg,tablename);
    
    Scan s = new Scan();
    ResultScanner rs = table.getScanner(s);
    for (Result r : rs) 
    {
        for (Cell cell : r.raw()) 
        {
            System.out.println(
                    "Rowkey : "+Bytes.toString(r.getRow())+
                    "   INFOR : "+Bytes.toString(CellUtil.getQualifierArray(cell))+
                    "   Value : "+Bytes.toString(CellUtil.getValueArray(cell))+
                    "   Time : "+cell.getTimestamp()
                    );
        }
        System.out.println("--------------------------------------------");
    }
    table.close();  
}

//删除表格
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();
			admin.close();
			return false;
		}
	}
	admin.close();
	return true;
}

public static void main (String [] agrs) 
{
	String tablename="hot_table";
	String columnFamily="INFOR";
	try 
	{
		//HbaseTest.creat(tablename,columnFamily);
		/*
		HbaseTest.put(tablename, "keyword2", columnFamily, "Time", "2015-04-10");
		HbaseTest.put(tablename, "keyword2", columnFamily, "Title", "广州地震");
		HbaseTest.put(tablename, "keyword2", columnFamily, "From", "百度热点");
		HbaseTest.put(tablename, "keyword2", columnFamily, "URL", "https://www.baidu.com/s?cl=3&tn=baidutop10&fr=top1000&wd=%E5%B9%BF%E5%B7%9E%E5%9C%B0%E9%9C%87&rsv_idx=2");
                                                                 //\/\/www.baidu.com\/s?cl=3&tn=baidutop10&fr=top1000&wd=高劲松违法被查&rsv_idx=2"
		*/
		//HbaseTest.get(tablename, "keyword2");
		HbaseTest.scan(tablename);
		/*
 		if(true==HbaseTest.delete(tablename))
		System.out.println("Delete table:"+tablename+"success!");
		 */
	}
	catch (Exception e) 
	{
		e.printStackTrace();
	}
} //END MAIN


}//END CLASS

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值