Java类实现HBase表的创建、添加、扫面等操作

package day1010;

import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.junit.*;
import org.apache.hadoop.hbase.util.Bytes;
public class HBase_1 {

    private Configuration conf=null;


    @Before
    public void init(){
         conf=HBaseConfiguration.create();
         conf.set("hbase.zookeeper.quorum", "master");//zookeeper
    }
    /**
     * 插入数据
     * @throws IOException
     * */
    //输入单行数据
    @Test
    public void testPut() throws IOException{
        HTable table =new HTable(conf, "people_5");//获取操作表
        Put put =  new Put(Bytes.toBytes("kr01"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("cuiyushun"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("25"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("height"),Bytes.toBytes("175cm"));
        table.put(put);
        table.close();
    }
    //输入多行数据
    @Test
    public void  testPutAll() throws IOException{
    //@SuppressWarnings("deprecation")
    //HTablePool  pool =      new HTablePool(conf,100);
        HTable table =new HTable(conf, "people_5");//获取操作表
        ArrayList<Put> puts = new ArrayList<Put>();
       for (int i = 0; i < 1000001; i++) {
           Put put =  new Put(Bytes.toBytes("kr01"+i));
           put.add(Bytes.toBytes("info"),Bytes.toBytes("money"),Bytes.toBytes("5870000"));
            puts.add(put);
            if(i%10000==0){
                table.put(puts);
                puts=new ArrayList<>(10000);
            }
        }
        table.put(puts);
        table.close();
    }
    //单行数据查询
    @Test
    public void testGet() throws IOException{
        HTable table = new HTable(conf, "people_5");
        Get get = new Get(Bytes.toBytes("rk011"));
        Result result = table.get(get);
        String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
        
        table.close();

    }

//删除表

    @Test
    public void TestDel() throws IOException{
        HTable table = new HTable(conf, "people_5");
        Delete delete = new Delete(Bytes.toBytes("rk011"));
        table.delete(delete);
        table.close();
    }
    //全表扫描
    @Test
    public void testScan() throws IOException{
        HTable table = new HTable(conf, "people_5");
        Scan scan = new Scan(Bytes.toBytes("kr0120"),Bytes.toBytes("kr0130"));
        ResultScanner scanner =table.getScanner(scan);
        for(Result result:scanner){
            System.out.println(result);
        }
        table.close();
    }
    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        Configuration conf =HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "master");//zookeeper
        HBaseAdmin admin = new HBaseAdmin(conf);//创建表的对象
        HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people_5"));//表的描述器
        
        HColumnDescriptor hcd_info= new HColumnDescriptor("info");//表的info列族类
        hcd_info.setMaxVersions(3);//设置列族的其他属性如VERSIONS
        HColumnDescriptor hcd_address= new HColumnDescriptor("addr");//表的address列族类
        hcd_info.setMaxVersions(5);//设置列族的其他属性如VERSIONS  
        HColumnDescriptor hcd_age= new HColumnDescriptor("age");//表的age列族类
        //VERSIONS默认为1
        htd.addFamily(hcd_info);
        htd.addFamily(hcd_address);
        htd.addFamily(hcd_age);
        
        admin.createTable(htd);
        admin.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值