hbase java api

hbase java api

前期准备

  • 在src/main下新建resource文件夹
  • 在pom.xml添加下面内容
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hbase.version>0.98.6-hadoop2</hbase.version>
  </properties>


 <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>0.98.6-hadoop2</version>
 </dependency>
 <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>0.98.6-hadoop2</version>
 </dependency>
 ```
 - 将hadoop下的core-site.xml和hdfs-site.xml以及hbase下的hbase-site.xml复制到resource中
### api基础操作

package weice.gao.hbase;

import java.io.IOException;

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.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.util.Bytes;
import org.apache.hadoop.io.IOUtils;

public class Hbase {
public static void get(String tablename,String row) throws IOException{
Configuration configuration = HBaseConfiguration.create();
HTable table = new HTable(configuration, tablename);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
Cell[] cells = result.rawCells();
for(Cell cell:cells)
{
System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+”:”);
System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell))+”->”);
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();

}
public static void put(String tablename,String row,String info,String key,String value) throws IOException{
    Configuration configuration = HBaseConfiguration.create();
    HTable table = new HTable(configuration, tablename);
    Put put = new Put(Bytes.toBytes(row));
    put.add(Bytes.toBytes(info),
            Bytes.toBytes(key),
            Bytes.toBytes(value));
    table.put(put);
    table.close();


    }
public static void del(String tablename,String row,String info,String field) throws IOException{
    Configuration configuration = HBaseConfiguration.create();
    HTable table = new HTable(configuration, tablename);
    Delete delete = new Delete(Bytes.toBytes(row));
    delete.deleteColumn(Bytes.toBytes(info), Bytes.toBytes(field));
    table.delete(delete);

    table.close();


    }
public static void scan(String tablename) throws IOException{
    Configuration configuration = HBaseConfiguration.create();
    HTable table = new HTable(configuration, tablename);
    Scan scan = new Scan();
    ResultScanner resultScanner = table.getScanner(scan);
    for (Result result : resultScanner) {
        System.out.println(Bytes.toString(result.getRow()));
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+":");
            System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell))+"->");
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
        }System.out.println("--------------------");
    }
    IOUtils.closeStream(resultScanner);
    IOUtils.closeStream(table);



    }
public static void create(String tablename) throws IOException{
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum", "node-1:2181");
    HBaseAdmin admin = new HBaseAdmin(configuration);
    HTableDescriptor tbl = new HTableDescriptor(TableName.valueOf(tablename));
    HColumnDescriptor  hcd = new HColumnDescriptor("info");
    hcd.setValue("VERSIONS", "3");
    tbl.addFamily(hcd);
    admin.createTable(tbl);

    admin.close();



    }
public static void delTable(String tablename) throws IOException{
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum", "node-1:2181");
    HBaseAdmin admin = new HBaseAdmin(configuration);
    admin.disableTable(tablename);
    admin.deleteTable(tablename);
    admin.close();



    }
public static void main(String[] args) throws IOException {
    Hbase hbase = new Hbase();
    hbase.put("people", "10001", "info1", "name", "gao");
    hbase.get("people", "10001");
    hbase.del("people", "10001", "info1","age");
    hbase.get("people", "10001");
    hbase.scan("people");
    hbase.delTable("people1");
}

}
“`

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值