1. Java API
Java API包括了对HBase的各种操作,本节主要对Java API中的基本操作进行简要介绍,诸如批量处理以及过滤器的使用等高级API,感兴趣的可以进一步了解。Java API中的CRUD主要通过HTable类提供的方法实现,而管理和创建HBase表,则通过HBaseAdmin类实现。
1.1 Get 操作
Get操作包括单次Get请求和批量Get请求,我们以单次Get请求为例:1
2
3
4
5
6
7
8Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "testtable");
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
Result result = table.get(get);
byte[] val = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
table.close();
System.out.println("Value: " + Bytes.toString(val));
1.2 Put 操作
1 | Configuration conf = HBaseConfiguration.create(); |
1.3 Delete 操作
1 | Configuration conf = HBaseConfiguration.create(); |
1.4 Scan
1 | Configuration conf = HBaseConfiguration.create(); |
1.5 HBaseAdmin 操作
HBaseAdmin提供了建表、创建列簇、检查表是否存在、修改表结构和列簇结构和删除表等功能。1
2
3
4
5
6
7
8Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor desc = new HTableDescriptor(Bytes.toBytes(testtable));
HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("colfam1"));
desc.addFamily(coldef);
admin.createTable(desc);
boolean avail = admin.isTableAvailable(Bytes.toBytes("testtable"));
System.out.println("Table available: " + avail);
2. Shell 基本命令
在HBase安装目录下输入./bin/hbase shell
即可进入HBase的Shell命令行模式,在模式下可以完成对HBase的一系列操作。
status
查看集群状态信息version
查看HBase版本信息create 't1', {NAME=>'f1', VERSION=>5}
创建表alter 't1', NAME=>'f1', VERSION=>5
修改表describe 't1'
获取表的元数据信息和是否可用的状态disable 't1'
下线表enable 't1'
上线表drop 't1'
删除表exist 't1'
判断某个表是否存在list
罗列所有表名称count 't1'
统计表的总行数delete 't1', 'r1', 'c1', ts1
删除特点单元格get 't1', 'r1', {COLUMN=>{'c1','c2','c3'}}
获取某几行数据put 't1', 'r1', 'c1', 'value', ts1
写入数据scan 't1', {COLUMNS=>['c1','c2'], LIMIT=>10, STARTROW=>'xyz'}
根据特定条件扫描表truncate 't1'
清空表
以上为Shell经常用到的命令,还有工具命令(compact、flush等)、复制命令等,可以进一步了解。3. Spark读写HBase
Spark操作HBase有两种方式,一种方式是在Spark架构中调用HBase Java API的方式,该方式下要遵循Spark分布式计算的特点来编程。
由于HBase提供了对Hadoop MapReduce框架的支持,因此在Spark中我们可以使用另一种方式,即利用NewAPIHadoop接口,实现对HBase的读写。3.1 Spark 写HBase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37import org.apache.hadoop.hbase.client.{Put, Result}
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.mapreduce.Job
import org.apache.spark._
object HBaseWrite {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("HBaseTest")
sparkConf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
sparkConf.registerKryoClasses(Array(classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],classOf[org.apache.hadoop.hbase.client.Result]))
val sc = new SparkContext(sparkConf)
val tablename = "test1"
sc.hadoopConfiguration.set("hbase.zookeeper.quorum", "hadoop001,hadoop002,hadoop003")
sc.hadoopConfiguration.set("hbase.zookeeper.property.clientPort", "2181")
sc.hadoopConfiguration.set(TableOutputFormat.OUTPUT_TABLE, tablename)
val job = new Job(sc.hadoopConfiguration)
job.setOutputKeyClass(classOf[ImmutableBytesWritable])
job.setOutputValueClass(classOf[Result])
job.setOutputFormatClass(classOf[TableOutputFormat[ImmutableBytesWritable]])
val indataRDD = sc.makeRDD(Array("1,jack,15", "2,Lily,16", "3,mike,16"))
val rdd = indataRDD.map(_.split(',')).map { arr => {
val put = new Put(Bytes.toBytes(arr(0)))
put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes(arr(1)))
put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("age"), Bytes.toBytes(arr(2).toInt))
(new ImmutableBytesWritable, put)
}
}
rdd.saveAsNewAPIHadoopDataset(job.getConfiguration)
sc.stop();
}
}
3.2 Spark 读HBase
1 | import org.apache.hadoop.hbase.client.HBaseAdmin |
3.3 Spark Scan HBase
1 | import org.apache.hadoop.hbase.HBaseConfiguration |