前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hbase(五) JavaApi操作Hbase

Hbase(五) JavaApi操作Hbase

作者头像
许喜朝
发布2020-11-24 15:45:44
3.1K0
发布2020-11-24 15:45:44
举报

Hbase(五): JavaApi操作Hbase

依赖
代码语言:javascript
复制
<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命令查看

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

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

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

创建表
代码语言:javascript
复制
//创建表
@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();
}
插入数据
代码语言:javascript
复制
/**
 * 插入数据
 */
@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();
}
单例模式插入大量数据
代码语言:javascript
复制
/**
 * 单例模式插入大量数据
 * @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+"秒");
}
获取数据
代码语言:javascript
复制
/**
 * 获取数据
 */
@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();
}
获取数据集
代码语言:javascript
复制
/**
 * 获取数据集
 */
@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();
}
删除数据
代码语言:javascript
复制
/**
 * 删除表数据
 * @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();
}
删除表
代码语言:javascript
复制
/**
 * 删除表
 * @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();


}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-11-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Hbase(五): JavaApi操作Hbase
    • 依赖
      • 定义静态配置变量
        • 创建表
          • 插入数据
            • 单例模式插入大量数据
              • 获取数据
                • 获取数据集
                  • 删除数据
                    • 删除表
                    相关产品与服务
                    TDSQL MySQL 版
                    TDSQL MySQL 版(TDSQL for MySQL)是腾讯打造的一款分布式数据库产品,具备强一致高可用、全球部署架构、分布式水平扩展、高性能、企业级安全等特性,同时提供智能 DBA、自动化运营、监控告警等配套设施,为客户提供完整的分布式数据库解决方案。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档