Hbase的JavaApi和工具类

来源:互联网 发布:mysql终端查看数据库 编辑:程序博客网 时间:2024/06/05 16:00

1.连接Hbase,使用单例模式。修改自己的主机名

package oracle.demo.oow.bd.util.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

public class HbaseConn {
private Configuration configuration = null;
private Connection connection = null;
    
    private static class SingletonHolder{
    private static final HbaseConn INSTANCE = new HbaseConn();
    }
    
    private HbaseConn(){
    try {
    configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop");
        configuration.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");
        connection = ConnectionFactory.createConnection(configuration);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static final Connection getConn() {
    return SingletonHolder.INSTANCE.connection;
    }
    
    public static void main(String[] args) {
System.out.println(HbaseConn.getConn());
}
}

2.封装一些工具类

(1)计数器   参数:表名 行键 列族 列 增加的数字

  public long incr(String tableName, String rowKey,  String family, String column, long range) {
    long count = 0l;
try {
Table table = HbaseConn.getConn().getTable(TableName.valueOf(tableName));
count = table.incrementColumnValue(Bytes.toBytes(rowKey), Bytes.toBytes(family), 
Bytes.toBytes(column), range);
table.close();
} catch (IOException e) {
e.printStackTrace();
}
return count;
    }
(2)插入或修改一条数据,针对列族中的一列 long型  其他类型也可以 修改雷清即可
  public void put(String tableName, String rowKey, String family, String column, long value) {
try {
Table table = HbaseConn.getConn().getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
    put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
       table.put(put);
       table.close();
} catch (IOException e) {
e.printStackTrace();
}
    }
(3)查询数据  根据行键查询 这里举例 行键为long型
public Result getResultByRow(String tableName, long rowKey, String family) {
    Result result = null;
    try {
       Table table = HbaseConn.getConn().getTable(TableName.valueOf(tableName));
       Get get = new Get(Bytes.toBytes(rowKey));
       if(family!=null) {
        get.addFamily(Bytes.toBytes(family));
       }
       result = table.get(get);
       table.close();
    } catch (IOException e) {
e.printStackTrace();
}
return result;
    }
    返回值是一个Result,get方法 可以添加列族 行键 
(4)扫描表  使用scan方法
public ResultScanner getResultScannerByFilter(String tableName, Filter filter, String family) {
    ResultScanner resultScanner = null;
    try {
    Table table = HbaseConn.getConn().getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
if (filter != null) {
scan.setFilter(filter);
}
if(family != null) {
scan.addFamily(Bytes.toBytes(family));
}
    resultScanner = table.getScanner(scan);
    table.close();
    } catch (IOException e) {
e.printStackTrace();
}
    return resultScanner;
    }
    返回值是一个ResultScanner,我们可以闯入一个过滤器来进行扫描。
(5)删除数据
  public void deleteDataByRow(String tableName, String rowKey) {
    try {
       Table table = HbaseConn.getConn().getTable(TableName.valueOf(tableName));
       Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
       table.delete(deleteAll);
       table.close();
    } catch (IOException e) {
e.printStackTrace();
}
    }

原创粉丝点击