代码示例
有很多的代码是重复的,只是为形成记忆,见谅,
另外需要将 hbase-site.xml,hdfs-site.xml,core-site.xml三个文件放到Resources上目录中
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.Batch;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;
import java.util.List;public class HbaseApi_test_1 {private static Connection hbaseConn;private static Configuration hbaseConf;//HBaseAdmin 提供了一个接口来管理 HBase 数据库的表信息private static Admin hbaseAdmin;/*** 静态构造,在调用静态方法前运行, 初始化连接对象 * */static {hbaseConf = HBaseConfiguration.create();try {hbaseConn = ConnectionFactory.createConnection(hbaseConf);System.out.println("连接上了?" + !hbaseConn.isClosed());hbaseAdmin = hbaseConn.getAdmin();} catch (java.lang.Exception e) {e.printStackTrace();}}//****************************根据rowkey读取一行数据****************************public static void getRow() throws java.lang.Exception {TableName tableName = TableName.valueOf("ns1:mytest_4");//取得一个要操作的表Table table = hbaseConn.getTable(tableName);//设置要查询的行的rowkeyGet wangwu = new Get(Bytes.toBytes("wangwu"));//设置显示多少个版本的数据wangwu.setMaxVersions(3);//取得指定时间戳的数据//wangwu.setTimeStamp(1);//限制要显示的列族//wangwu.addFamily(Bytes.toBytes("grade"));//限制要显示的列//wangwu.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"));Result result = table.get(wangwu);List<Cell> cells = result.listCells();for (Cell c : cells) {//注意这里 CellUtil类的使用System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(c)) + " ");System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(c)) + " ");System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(c)) + " ");System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(c)) + " ");System.out.println();}//关闭资源table.close();//hbaseConn.close();}//****************************根据rowkey,读取多行数据****************************public static void getMultiRows() throws java.lang.Exception {TableName tableName = TableName.valueOf("ns1:mytest_4");//取得一个要操作的表Table table = hbaseConn.getTable(tableName);ArrayList<Get> getArrayList = new ArrayList<>();Get wangwu = new Get(Bytes.toBytes("wangwu"));//限制要显示的列族wangwu.addFamily(Bytes.toBytes("grade"));//限制要显示的列wangwu.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"));Get lishi = new Get(Bytes.toBytes("lishi"));getArrayList.add(wangwu);getArrayList.add(lishi);Result[] results = table.get(getArrayList);for (int i = 0; i < results.length; i++) {Result result = results[i];List<Cell> cells = result.listCells();for (Cell c : cells) {//注意这里 CellUtil类的使用System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(c)) + " ");System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(c)) + " ");System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(c)) + " ");System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(c)) + " ");System.out.println();}}//关闭资源table.close();//hbaseConn.close();}//****************************scan全表扫描查询****************************public static void scan_easy() throws java.lang.Exception {TableName tableName = TableName.valueOf("ns1:mytest_4");//取得一个要操作的表Table table = hbaseConn.getTable(tableName);/*** Scan的构造函数有多种*///例 1//Scan scan = new Scan();//例 2,指定rowkey的开始和结束Scan scan = new Scan(Bytes.toBytes("person-1007"), Bytes.toBytes("person-1021"));ResultScanner scanner = table.getScanner(scan);//源码 interface ResultScanner extends Closeable, Iterable<Result>,所以可以如下操作for (Result result : scanner) {List<Cell> cells = result.listCells();for (Cell c : cells) {//注意这里 CellUtil类的使用System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(c)) + " ");System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(c)) + " ");System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(c)) + " ");System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(c)) + " ");System.out.println();}}//关闭资源scanner.close();table.close();//hbaseConn.close();}//****************************scan全表扫描查询,指定列族,列,哪个rowkey开始,哪个rowkey结束****************************public static void scan_startRowKey_endRowKey() throws java.lang.Exception {long begin = System.currentTimeMillis();TableName tableName = TableName.valueOf("ns1:mytest_4");//取得一个要操作的表Table table = hbaseConn.getTable(tableName);Scan scan = new Scan();/*** scan常见的设置*///1、只查询列族 column family为grade的数据scan.addFamily(Bytes.toBytes("grade"));//1、只查询列column为course:yuwen的数据scan.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"));//2、开始的rowkey行键(开始行包含),结束的rowkey行键(结束行不包含)//scan.setStartRow(Bytes.toBytes("lishi"));//scan.setStopRow(Bytes.toBytes("zhangshan"));//3、batch和caching和hbase table column size共同决定了rpc的次数。//scan可以通过setCaching与setBatch方法提高速度(以空间换时间);//scan.setCaching(500); //每次rpc的请求记录数,默认是1;cache大可以优化性能,但是太大了会花费很长的时间进行一次传输。//scan.setBatch(2); //设置每次取的column size;有些row特别大,所以需要分开传给client,就是一次传一个row的几个column。scan.setCaching(500);scan.setBatch(2);ResultScanner scanner = table.getScanner(scan);//源码 interface ResultScanner extends Closeable, Iterable<Result>,所以可以如下操作for (Result result : scanner) {List<Cell> cells = result.listCells();for (Cell c : cells) {//注意这里 CellUtil类的使用System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(c)) + " ");System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(c)) + " ");System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(c)) + " ");System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(c)) + " ");System.out.println();}}long end = System.currentTimeMillis();System.out.println("运行时间:" + (end - begin));//关闭资源scanner.close();table.close();//hbaseConn.close();}}