Table of Contents
Java 版的
Hbase 工具類,配置 zookeeper 的地址
public class HbaseUtil {
public static Configuration getHbaseConfig() {
Configuration config = HBaseConfiguration.create();
// 顯式配置 hbase zookeeper 地址
config.set("hbase.zookeeper.quorum", "192.168.78.135");
// config.set("hbase.table.sanity.checks", "false");
// hdfs 配置和 hbase 配置 (hbase-site.xml, core-site.xml)
// config.addResource(new Path(CommUtils.getBasicPath() + "/config/hbase-site.xml"));
// config.addResource(new Path(CommUtils.getBasicPath() + "/config/core-site.xml"));
return config;
}
}
建表操作
private static void createSchemaTables(Admin admin) throws IOException {
// 設置 table name
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
// 設置 列族,壓縮
table.addFamily(new HColumnDescriptor(CF_DEFAULT)
.setCompressionType(Compression.Algorithm.NONE));
// 預設 region 數量 和 region 算法
table.setRegionReplication(2);
table.setRegionSplitPolicyClassName(RegionSplitter.UniformSplit.class.getName());
System.out.print("Creating table Start");
// 如果表存在,先 disable 表,在 delete 表
if (admin.tableExists(table.getTableName())) {
admin.disableTable(table.getTableName());
admin.deleteTable(table.getTableName());
}
// 實際的創建表函數
admin.createTable(table);
System.out.println("Create table Done");
}
修改表結構
private static void modifySchema(Admin admin) throws IOException {
TableName tableName = TableName.valueOf(TABLE_NAME);
// 修改表之前,判斷表是否存在,防止報異常
if (!admin.tableExists(tableName)) {
System.out.println("Table does not exist.");
System.exit(-1);
}
// 創建表對象
HTableDescriptor table = admin.getTableDescriptor(tableName);
// 創建列族對象
HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
// 設置壓縮格式
newColumn.setCompactionCompressionType(Compression.Algorithm.GZ);
// 執行最大版本號
newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
// 新增表列族
admin.addColumn(tableName, newColumn);
// 修改存在 column family 的壓縮格式和版本
HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT);
existingColumn.setCompactionCompressionType(Compression.Algorithm.GZ);
existingColumn.setMaxVersions(HConstants.ALL_VERSIONS);
table.modifyFamily(existingColumn);
admin.modifyTable(tableName, table);
// Disable an existing table
admin.disableTable(tableName);
// Delete an existing column family
admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8"));
// Delete a table (Need to be disabled first)
admin.deleteTable(tableName);
}
列出所有表
private static void listTable(Admin admin) throws IOException {
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
System.err.println("tableName is : " + tableName);
}
}
刪表
private static void dropTable(Admin admin, String tableName) throws IOException {
TableName table = TableName.valueOf(tableName);
admin.disableTable(table);
System.err.println("disable table is success");
admin.deleteTable(table);
System.err.println("drop table is success");
}
寫表
private static void insertData(HTable table) throws IOException {
// rowKey 前綴
String rowKey = "rowKey";
// 列族名稱
String family = "f1";
// 造一批假數據
for (int i = 0; i < 50; i++) {
Put put = new Put(Bytes.toBytes(rowKey + i));
for (int j = 0; j < 10; j++) {
put.add(family.getBytes(), Bytes.toBytes(rowKey + i + "-key" + j), Bytes.toBytes(rowKey + i + "-value" + j));
}
table.put(put);
}
}
根據 rowKey 刪數據
private static void deleteData(HTable table) throws IOException {
Delete delete = new Delete("rowKey1".getBytes());
// 指定 列族 名稱,刪除整個列族
delete.addFamily("f1".getBytes());
// 指定列族下的一個列,刪除列
delete.addColumn("f1".getBytes(), "key1".getBytes());
// 什麼也不指定,則刪除整個 rowKey 下的數
table.delete(delete);
}
查表,所有數據
private static void scanData(HTable table) throws IOException {
// 初始化 startRow 和 endRow
Scan scan = new Scan("123".getBytes(), "rowKey11".getBytes());
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
// 獲取 rowKey
String rowKey = new String(result.getRow());
// 獲取 key
NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap("f1".getBytes());
for (Map.Entry<byte[], byte[]> entry : familyMap.entrySet()) {
String key = new String(entry.getKey());
String value = new String(entry.getValue());
// 打印結果
System.err.println("rowKey is : " + rowKey + ", key is : " + key + ", value is : " + value);
}
}
}
根據 rowKey 查數據
private static void selectDataByRowKey(HTable table) throws IOException {
Get get = new Get("rowKey1".getBytes());
// 獲取單個 rowKey 下整個結果
Result result = table.get(get);
System.err.println("result is : " + result.toString());
// 根據列族,獲取 key
NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap("f1".getBytes());
for (Map.Entry<byte[], byte[]> entry : familyMap.entrySet()) {
System.err.println("key is : " + new String(entry.getKey()) + ", value is : " + new String(entry.getValue()));
}
// 獲取 value
byte[] value = result.getValue("f1".getBytes(), "key1".getBytes());
System.err.println("value is : " + new String(value));
}
Scala 版的
Hbase 工具類
object HbaseUtil {
// 已過時
// val conf = new HBaseConfiguration()
val conf = HBaseConfiguration.create()
conf.set("hbase.zookeeper.quorum", "192.168.78.135")
val connection = ConnectionFactory.createConnection(conf)
}
列出所有表
def listTables(admin: Admin) = {
val names = admin.listTableNames()
names.foreach(println(_))
}
建表
def createTable(admin: Admin) = {
if (admin.tableExists(this.tb)) {
println("表已存在,請換個表名重新建表")
} else {
val descriptor = new HTableDescriptor
descriptor.setName(TableName.valueOf(tableName))
descriptor.addFamily(new HColumnDescriptor("f1"))
admin.createTable(descriptor)
println("create table done")
}
}
查看錶結構
def describeTable(admin: Admin) = {
val descriptor = admin.getTableDescriptor(TableName.valueOf(tableName))
val families = descriptor.getFamilies
println(families)
}
刪表
def dropTable(admin: Admin) = {
if (admin.tableExists(this.tb)) {
admin.disableTable(this.tb)
admin.deleteTable(this.tb)
} else
println("表不存在,請確認表名")
}
寫數據
def insertData(tableName: String): Unit = {
// 新建表對象
val table = new HTable(conf, tableName)
// 造假數據開始
for (a <- 51 to 150) {
// 創建 put 對象,用來存放數據,構造方法裏放 rowKey 的值
val put = new Put(("rowKey" + a).getBytes())
for (b <- 1 to 10) {
val key = ("rowKey" + a + "-key" + b).getBytes()
val value = ("rowKey" + a + "-value" + b).getBytes()
// addColumn 方法參數:列族,key,value
put.addColumn("f1".getBytes(), key, value)
}
// put 數據
table.put(put)
}
}
掃描整個表
def scanData(tableName: String): Unit = {
// 創建 scan 對象
val scan = new Scan
// 創建表對象,構造方法:配置對象,表名
val table = new HTable(conf, tableName)
// 獲取 scanner 對象
val scanner: ResultScanner = table.getScanner(scan)
// 從 scanner 對象中取所有結果
val result = scanner.next()
// 遍歷全部結果
while (result != null) {
// 獲取 rowKey
val row = new String(result.getRow)
// 獲取 rowKey 下所有數據,返回的結構是 NavigableMap<byte[], byte[]>
val map = result.getFamilyMap("f1".getBytes)
val iter = map.entrySet().iterator()
// 開始遍歷 map 結果數據
while (iter.hasNext) {
val entry = iter.next()
val key = new String(entry.getKey)
val value = new String(entry.getValue)
println("rowKey is : " + row + ", key is : " + key + ", value is : " + value)
}
}
}
根據 rowKey 查數據
def scanData(tableName: String): Unit = {
// 創建 scan 對象
val scan = new Scan
// 創建表對象,構造方法:配置對象,表名
val table = new HTable(conf, tableName)
// 獲取 scanner 對象
val scanner: ResultScanner = table.getScanner(scan)
// 從 scanner 對象中取所有結果
val result = scanner.next()
// 遍歷全部結果
while (result != null) {
// 獲取 rowKey
val row = new String(result.getRow)
// 獲取 rowKey 下所有數據,返回的結構是 NavigableMap<byte[], byte[]>
val map = result.getFamilyMap("f1".getBytes)
val iter = map.entrySet().iterator()
// 開始遍歷 map 結果數據
while (iter.hasNext) {
val entry = iter.next()
val key = new String(entry.getKey)
val value = new String(entry.getValue)
println("rowKey is : " + row + ", key is : " + key + ", value is : " + value)
}
}
}
根據 rowKey 刪數據
def deleteDataByRowKey(tableName: String, row: String): Unit = {
val table = new HTable(conf, tableName)
val delete = new Delete(row.getBytes())
table.delete(delete)
}
查詢所有記錄數 count*
def countData(tableName: String): Unit = {
val table = new HTable(conf, tableName)
val descriptor = table.getTableDescriptor
// 定義協調處理器名稱
val aggre = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation"
// 如果表沒有該協同處理器,先關表,addCoprocessor,開啓表
if (!descriptor.hasCoprocessor(aggre)) {
val tbn = TableName.valueOf(tableName)
admin.disableTable(tbn)
descriptor.addCoprocessor(aggre)
admin.modifyTable(TableName.valueOf(tableName), descriptor)
admin.enableTable(tbn)
}
// 創建處理器客戶端
val client = new AggregationClient(conf)
// 計算 count 時間
val watch: StopWatch = new StopWatch()
watch.start()
// 開始 rowCount 操作
val count = client.rowCount(table, new LongColumnInterpreter, new Scan())
watch.stop()
// 打印
println(watch.getTime)
println(tableName + " count si : " + count)
}
Scala 操作表代碼
class TableSchemaDemo1(val tableName: String) {
val tb = TableName.valueOf(tableName)
def dropTable(admin: Admin) = {
if (admin.tableExists(this.tb)) {
admin.disableTable(this.tb)
admin.deleteTable(this.tb)
} else
println("表不存在,請確認表名")
}
def describeTable(admin: Admin) = {
val descriptor = admin.getTableDescriptor(TableName.valueOf(tableName))
val families = descriptor.getFamilies
println(families)
}
def createTable(admin: Admin) = {
if (admin.tableExists(this.tb)) {
println("表已存在,請換個表名重新建表")
} else {
val descriptor = new HTableDescriptor
descriptor.setName(TableName.valueOf(tableName))
descriptor.addFamily(new HColumnDescriptor("f1"))
admin.createTable(descriptor)
println("create table done")
}
}
def listTables(admin: Admin) = {
val names = admin.listTableNames()
names.foreach(println(_))
}
def listTables(): Unit = {
}
override def toString = s"TableSchemaDemo1($tableName)"
}
object TableSchemaDemo1 {
def main(args: Array[String]): Unit = {
// 獲取 hbase 配置
val conf = HbaseUtil.conf
// 獲取連接
val connection = HbaseUtil.connection
// 獲取管理員
val admin = connection.getAdmin()
val demo = new TableSchemaDemo1("tbs")
demo.listTables(admin)
demo.createTable(admin)
demo.describeTable(admin)
// demo.dropTable(admin)
println(demo.toString)
}
}