Hbase(五) JavaApi操作Hbase

Hbase(五): JavaApi操作Hbase

依赖
<dependencies>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>0.98.17-hadoop2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>0.98.17-hadoop2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

依赖的版本应该和Hbase版本一致

Hbase版本可以通过连接Hbase client使用version命令查看

定义静态配置变量
public static Configuration conf;
static{
    conf = HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum","hadoop1:2181");
}

由于配置变量都是一样的,可以定义成静态变量方便使用

需要注意的是需要在本地做主机ip映射,mac的hostname文件在etc目录下

创建表
//创建表
@Test
public void createTable() throws IOException {
    HBaseAdmin admin = new HBaseAdmin(conf);
    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("users"));
    HColumnDescriptor c1 = new HColumnDescriptor("c1");
    c1.setMaxVersions(3);
    //HColumnDescriptor c2 = new HColumnDescriptor("c2");
    htd.addFamily(c1);
    //htd.addFamily(c2);
    admin.createTable(htd);
    admin.close();
}
插入数据
/**
 * 插入数据
 */
@Test
public void insertData() throws IOException {
    HTable table = new HTable(conf,"table");
    Put put = new Put(Bytes.toBytes("key_1"));
    put.add(Bytes.toBytes("c1"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
    table.put(put);
    table.close();
}
单例模式插入大量数据
/**
 * 单例模式插入大量数据
 * @throws IOException
 */
@Test
public void insertDatas() throws IOException {
    Long state = System.currentTimeMillis();
    HTable table = new HTable(conf, "table2");
    List<Put> puts = new ArrayList<Put>();
    for (int i = 1;i <= 1000000; i++){
        Put put = new Put(Bytes.toBytes("key_" + i));
        put.add(Bytes.toBytes("c1"),Bytes.toBytes("name"+i),Bytes.toBytes("zhangsan"+i));
        puts.add(put);
        if ( i % 10000 == 0){
            table.put(puts);
            puts = new ArrayList<Put>();
        }
    }
    table.put(puts);
    table.close();
    Long over = System.currentTimeMillis();
    System.out.println("用时"+(over-state)/1000+"秒");
}
获取数据
/**
 * 获取数据
 */
@Test
public void selectData() throws IOException {
    HTable table = new HTable(conf, "table");
    Get get = new Get(Bytes.toBytes("key_1"));
    Result result = table.get(get);
    byte[] bytes = result.getValue(Bytes.toBytes("c1"),Bytes.toBytes("name"));
    String str = Bytes.toString(bytes);
    System.out.println(str);
    table.close();
}
获取数据集
/**
 * 获取数据集
 */
@Test
public void selectDatas() throws IOException {
    int i=1;
    HTable table = new HTable(conf, "table");

        Scan scan = new Scan(Bytes.toBytes("key_1"));
        ResultScanner scanner = table.getScanner(scan);
        Iterator it = scanner.iterator();
        while (it.hasNext()){
            Result result = (Result) it.next();
            byte[] value = result.getValue(Bytes.toBytes("c1"), Bytes.toBytes("name"));
            String str = Bytes.toString(value);
            System.out.println(str);
            i++;
        }

    table.close();
}
删除数据
/**
 * 删除表数据
 * @throws IOException
 */
@Test
public void removeData() throws IOException {
    HTable table = new HTable(conf, "table");
    Delete key_1 = new Delete(Bytes.toBytes("key_1"));
    table.delete(key_1);
    table.close();
}
删除表
/**
 * 删除表
 * @throws IOException
 */
@Test
public void deleteTable() throws IOException{
    HBaseAdmin admin = new HBaseAdmin(conf);
    admin.disableTable(Bytes.toBytes("table1"));
    admin.deleteTable(Bytes.toBytes("table1"));
    admin.close();


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值