HBase Java API

        1. createTable1 创建表 create 'tableName','columnFamily'
        2. descTable  表的描述 -- 打印表的列族的名字
        3. modifyTable 替换该表tableName的所有列簇
        4. getAllTables 获取HBase中所有的表
        ------------------------------------------------
        5. putData 更新、插入数据
        ------------------------------------------------
        6. addData 添加数据
        ------------------------------------------------
        7. getResult1 根据rowkey进行查询
        8. getResult2 查询指定的某列
        9. getResultScann 遍历一个表中的所有数据
        10. getResultByVersion 查询某列数据的某个版本信息
        -------------------------------------------------
        11. deleteColumn1 删除指定列
        12. deleteColumn2 删除指定的某个rowkey
        13. disableTable 让表失效
        14. dropTable 删除表

package HbaseDome;
 
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
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.TableName;
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.protobuf.generated.ZooKeeperProtos.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class Hbasedome implements HBaseDemoInterface{
	
	//连接HBase集群
	static Configuration conf =null;
    private static final String ZKconnect="192.168.123.212:2181,192.168.123.213:2181,192.168.123.214:2181";
    static{
        conf=HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", ZKconnect);
    }
	
	public static void main(String[] args) {
	
		Hbasedome a =new Hbasedome();
		
		Result result = a.getResult("table03", "usr001");
        List<Cell> cells = result.listCells();
        for (int i = 0; i < cells.size(); i++) {
            Cell cell = cells.get(i);
			System.out.println( Bytes.toString(cell.getRow()) + "\t" + 
								Bytes.toString(cell.getFamily()) + "\t" + 
								Bytes.toString(cell.getQualifier()) + "\t" + 
								Bytes.toString(cell.getValue()) + "\t" +                 
                                cell.getTimestamp());

        }
         
        List<KeyValue> list = result.list();
        for (int i = 0; i < list.size(); i++) {
            KeyValue kv = list.get(i);
            System.out.println( Bytes.toString(kv.getRow()) + "\t" + 
								Bytes.toString(kv.getFamily()) + "\t" + 
								Bytes.toString(kv.getQualifier()) + "\t" + 
								Bytes.toString(kv.getValue()) + "\t" + 
								kv.getTimestamp());
        }
        
		
	}
	
}
具体的方法见下面,有详述!
------------------------------------------------------------------------------------
	//创建表方式一 (参数是表名、列族。和hbase的命令:create 'hbase','familiy' 一致)
	public void createTable1(String tableName, String[] family) throws Exception {
	
        HBaseAdmin admin=new HBaseAdmin(conf);
        HTableDescriptor desc =new HTableDescriptor(tableName);
        
        for(int i=0;i<family.length;i++){
            desc.addFamily(new HColumnDescriptor(family[i]));
        }
        if(admin.tableExists(tableName)){
            System.out.println("表已经存在,别瞎输行吗");
        }else{
            admin.createTable(desc);
            System.out.println("表创建成功");
        }
    }
	
	------------------------------------------------------------------------------------
	//表的描述 -- 打印表的列族的名字
    public void descTable(String tableName) throws Exception {
        HBaseAdmin admin=new HBaseAdmin(conf);
        HTable table=new HTable(conf, tableName);
        HTableDescriptor desc =table.getTableDescriptor();
        HColumnDescriptor[] columnFamilies = desc.getColumnFamilies();
     
        for(HColumnDescriptor t:columnFamilies){
            System.out.println(Bytes.toString(t.getName()));
        }
         
    }
	
	------------------------------------------------------------------------------------
	//这种方式是替换该表tableName的所有列簇
    public void modifyTable(String tableName) throws Exception {
        HBaseAdmin admin=new HBaseAdmin(conf);
        HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
        htd.addFamily(new HColumnDescriptor(Bytes.toBytes("cf3")));
        htd.addFamily(new HColumnDescriptor(Bytes.toBytes("cf2")));
        admin.modifyTable(tableName, htd);
 
        //删除该表tableName当中的特定的列簇
        admin.deleteColumn(tableName, "cf3");
        System.out.println("修改成功");
         
    }
	
	------------------------------------------------------------------------------------
	// 获取HBase中所有的表
    public void getAllTables() throws Exception {
	
        HBaseAdmin admin =new HBaseAdmin(conf);
         
        String[] tableNames = admin.getTableNames();
        for(int i=0;i<tableNames.length;i++){
            System.out.println(tableNames[i]);
        }
    }
	
	------------------------------------------------------------------------------------
	//更新数据  插入数据
    public void putData(String tableName, String rowKey, String familyName, String columnName, String value)
            throws Exception {
        HTable htable=new HTable(conf, Bytes.toBytes(tableName));
        Put put=new Put(Bytes.toBytes(rowKey));
        put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));
        htable.put(put);
         
    }
	
	------------------------------------------------------------------------------------
	//为表添加数据 --- 向列族lie01 添加某几列数据,列族lie02 添加某几列数据
    public void addData(String tableName, String rowKey, String[] column1, String[] value1, String[] column2,
            String[] value2) throws Exception {
			
        HTable htable=new HTable(conf, Bytes.toBytes(tableName));
        Put put=new Put(Bytes.toBytes(rowKey));
        HColumnDescriptor[] columnFamilies = htable.getTableDescriptor().getColumnFamilies();
		
        for(int i=0;i<=columnFamilies.length;i++){
            String nameAsString = columnFamilies[i].getNameAsString();
            if(nameAsString.equals("lie01")){
                for(int j=0;j<column1.length;j++){
                    put.add(Bytes.toBytes(nameAsString), Bytes.toBytes(column1[j]),Bytes.toBytes(value1[j]));
                }
            }
            if(nameAsString.equals("lie02")){
                for(int j=0;j<column2.length;j++){
                    put.add(Bytes.toBytes(nameAsString), Bytes.toBytes(column2[j]),Bytes.toBytes(value2[j]));
                }
            }
             
        }
        htable.put(put);
        System.out.println("addData ok!");
    }
	
	------------------------------------------------------------------------------------
	//根据rowkey 查询
    public Result getResult1(String tableName, String rowKey) throws Exception {
	
		HTable htable=new HTable(conf, Bytes.toBytes(tableName));
        Get get=new Get(Bytes.toBytes(rowKey));
        Result result=htable.get(get);
		
      for(KeyValue k:result.list()){
          System.out.println(Bytes.toString(k.getFamily()));
          System.out.println(Bytes.toString(k.getQualifier()));
          System.out.println(Bytes.toString(k.getValue()));
          System.out.println(k.getTimestamp());
      }
        return result;
    }
	
	------------------------------------------------------------------------------------
	//查询指定的某列 
    public Result getResult2(String tableName, String rowKey, String familyName, String columnName) throws Exception {
	
		HTable htable=new HTable(conf, Bytes.toBytes(tableName));
        Get get=new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(familyName),Bytes.toBytes(columnName));
        Result result=htable.get(get);
		
        for(KeyValue k:result.list()){
            System.out.println(Bytes.toString(k.getFamily()));
            System.out.println(Bytes.toString(k.getQualifier()));
            System.out.println(Bytes.toString(k.getValue()));
            System.out.println(k.getTimestamp());
        }
        return result;
    }
	
	------------------------------------------------------------------------------------
	//遍历查询表
    public ResultScanner getResultScann(String tableName) throws Exception {
     
        Scan scan=new Scan();
        ResultScanner rs =null;
        HTable htable=new HTable(conf, tableName);
		
        try{
            rs=htable.getScanner(scan);
            for(Result r: rs){
                for(KeyValue kv:r.list()){
                    System.out.println(Bytes.toString(kv.getRow()));
                    System.out.println(Bytes.toString(kv.getFamily()));
                    System.out.println(Bytes.toString(kv.getQualifier()));
                    System.out.println(Bytes.toString(kv.getValue()));
                    System.out.println(kv.getTimestamp());
                }
            }
        }finally{
            rs.close();
        }
        return rs;
    }
	
	------------------------------------------------------------------------------------
	//查询某列数据的某个版本
    public Result getResultByVersion(String tableName, String rowKey, String familyName, String columnName,
            int versions) throws Exception {
     
        HTable htable=new HTable(conf, tableName);
        Get get =new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
        get.setMaxVersions(versions);
        Result result=htable.get(get);
         
        for(KeyValue kv: result.list()){
 
            System.out.println(Bytes.toString(kv.getFamily()));
            System.out.println(Bytes.toString(kv.getQualifier()));
            System.out.println(Bytes.toString(kv.getValue()));
            System.out.println(kv.getTimestamp());
             
        }
 
        return result;
    }
	
	------------------------------------------------------------------------------------
	//删除指定某列
    public void deleteColumn1(String tableName, String rowKey, String falilyName, String columnName) throws Exception {
 
        HTable htable=new HTable(conf, tableName);
      Delete delete1=new Delete(Bytes.toBytes(rowKey));
        Delete de =new Delete(Bytes.toBytes(rowKey));
        de.deleteColumn(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
        htable.delete(de);
    }
 
    ------------------------------------------------------------------------------------
    //删除指定的某个rowkey
    public void deleteColumn2(String tableName, String rowKey) throws Exception {
        HTable htable=new HTable(conf, tableName);
 
        Delete de =new Delete(Bytes.toBytes(rowKey));
         htable.delete(de);
         
    }
	
	------------------------------------------------------------------------------------
    //让该表失效
    public void disableTable(String tableName) throws Exception {
        HBaseAdmin admin=new HBaseAdmin(conf);
        admin.disableTable(tableName);
         
    }
	
	------------------------------------------------------------------------------------
    //删除表
    public void dropTable(String tableName) throws Exception {
         
        HBaseAdmin admin=new HBaseAdmin(conf);
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
         
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值