HBASE操作
JAVAAPI操作HBASE
添加maven依賴:
<!-->
hbase連接依賴
</-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-protocol</artifactId>
<version>2.0.0</version>
</dependency>
通過(guò)相應(yīng)的語(yǔ)法進(jìn)行操作:
一、連接:
1、通過(guò)創(chuàng)建connection,創(chuàng)建configuration進(jìn)行連接:
Configuration hBaseConfiguration = HBaseConfiguration.create();
hBaseConfiguration.setInt("hbase.client.retries.number", 1);
hBaseConfiguration.set("hbase.zookeeper.property.clientPort","2181");//連接端口
hBaseConfiguration.set("hbase.zookeeper.quorum","集群節(jié)點(diǎn)1:2181,集群節(jié)點(diǎn)2:2181,集群節(jié)點(diǎn)3:2181,集群節(jié)點(diǎn)4:2181,集群節(jié)點(diǎn)5:2181");
hBaseConfiguration.set("zookeeper.session.timeout", "5000");/ZK連接超時(shí)時(shí)間
hBaseConfiguration.set("hbase.zookeeper.property.maxclientcnxns", "300");
hBaseConfiguration.set("hbase.ipc.client.socket.timeout.connect", "10000");//HBASE連接超時(shí)時(shí)間
hBaseConfiguration.set("hbase.regionserver.handler.count", "500");
connection = ConnectionFactory.createConnection(hBaseConfiguration);
2、通過(guò)導(dǎo)入配置連接:
hBaseConfiguration.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
hBaseConfiguration.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
connection = ConnectionFactory.createConnection(hBaseConfiguration);
PS!!!依賴包的版本一定要配套
二、操作:
1、SCAN:通過(guò)上邊連接獲取到的連接(connection),我們可以獲取到操作用戶
Admin admin = connection.getAdmin();
通過(guò)admin我們可以獲取到表屬性和列簇屬性
TableName[] tableNames = admin.listTableNames();//查看所有表名
HTableDescriptor[] hTableDescriptors = admin.listTables();//查看所有表屬性
Scan scan = new Scan();//創(chuàng)建一個(gè)SCAN操作對(duì)象
Table table = connection.getTable(TableName.valueOf("表名"));//通過(guò)獲取到的連接和表名獲取到表操作對(duì)象
System.out.println(table.getName());
ResultScanner scanner = table.getScanner(scan);//執(zhí)行scan操作
for (Result result : scanner){ //每一個(gè)result都是查詢出來(lái)的一行數(shù)據(jù)結(jié)果
System.out.println(new String(result.getRow()));//獲取到表中的所有行鍵[行鍵建后文 PS行鍵]
byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("id"));//可以通過(guò)result獲取到具體的數(shù)據(jù)
System.out.println(new String(value));//HBASE中的所有事物都是以二進(jìn)制存放在文件內(nèi)存區(qū)中的,所以取出時(shí)需要進(jìn)行轉(zhuǎn)換
}
2、GET操作:
Get get = new Get(Bytes.toBytes("表名"));//創(chuàng)建GET操作對(duì)象
get.addFamily(Bytes.toBytes("info"));//添加列簇篩選條件
Result result = table.get(get);//執(zhí)行GET操作,獲取到結(jié)果
Cell[] cells = result.rawCells();//獲取屬性操作,這個(gè)可以獲取到result中的列簇,屬性,行鍵等
byte[] row = result.getRow();//獲取行鍵
System.out.println(new String(row));
byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("id"));//獲取到info列簇下對(duì)應(yīng)id列的值
System.out.println(new String(value));
3、DELETE刪除操作:
// 創(chuàng)建一個(gè)刪除請(qǐng)求
Delete delete = new Delete(Bytes.toBytes("行鍵"));
// 可以自定義一些篩選條件
delete.addFamily(Bytes.toBytes("列簇名"));
table.delete(delete);
// 停用表
admin.disableTable(tableName);
// 刪除列族
admin.deleteColumnFamily(tableName, "列簇名".getBytes());
// 刪除表
admin.deleteTable(tableName);
4、PUT新增操作:
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TableName.valueOf("表名")).build();
admin.createTable(tableDescriptor);//新增表,這個(gè)是1.3之后版本的用法
TableName tableName = TableName.valueOf("表名");
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
// 新建一個(gè)列族名為test的列族
HColumnDescriptor test= new HColumnDescriptor("test");
// 將列族添加到表中
tableDescriptor.addFamily(test);
admin.createTable(tableDescriptor);//老版本API用法
// 創(chuàng)建一個(gè)put請(qǐng)求,用于添加數(shù)據(jù)或者更新數(shù)據(jù)
Put put = new Put(Bytes.toBytes("行鍵"));
put.addColumn(Bytes.toBytes("列簇"), Bytes.toBytes("數(shù)據(jù)列名"), Bytes.toBytes("數(shù)據(jù)"));
table.put(put);
// 創(chuàng)建一個(gè)append請(qǐng)求,用于在數(shù)據(jù)后面添加內(nèi)容
Append append = new Append(Bytes.toBytes("行鍵"));
append.add(Bytes.toBytes("列簇"), Bytes.toBytes("數(shù)據(jù)列名"), Bytes.toBytes("數(shù)據(jù)"));
table.append(append);
HBASE shell命令:
hbase shell //進(jìn)入HBASE的shell命令客戶端,exit為退出
create_namespace '表空間名' //創(chuàng)建一個(gè)表空間
drop_namespace '表空間名' //刪除一個(gè)表空間
describe_namespace '表空間名' //查看一個(gè)表空間
list_namespace //查看所有表空間
create '表空間名:表名', '列簇名' //在對(duì)應(yīng)表空間下創(chuàng)建表
list_namespace_tables '表名' //查看一個(gè)表空間下的某個(gè)表
create '表名','列簇名','列簇名' //創(chuàng)建表
list //列出所有表
disable '表名' //禁用表
alter '表名', '列族名' //修改列簇
alter '表名', {NAME=> '列族名', METHOD=> 'delete'} //刪除列簇
# 修改f1列族的版本為5
alter 't1', NAME => 'f1', VERSIONS => 5
# 修改多個(gè)列族,修改f2為內(nèi)存,版本號(hào)為5
alter 't1', 'f1', {NAME => 'f2', IN_MEMORY => true}, {NAME => 'f3', VERSIONS => 5}
describe '表名' //獲取表的描述
exists '表名' //判斷表是否存在
enable '表名' //啟用表,和禁用表對(duì)應(yīng)
drop '表名' 表的刪除需要先禁用,啟用的表是不允許刪除的
put '表名', '行鍵', '列族名', '列值'
put '表名', '行鍵', '列族名:列名', '列值' //插入數(shù)據(jù)語(yǔ)法
scan '表名' //全表掃描
scan '表名', {COLUMN=>'列族名:列名'} //掃描某個(gè)列簇對(duì)應(yīng)的列
get '表名', '行鍵', '列族名'
get '表名', '行鍵' //查詢數(shù)據(jù)
delete '表名', '行鍵', '列族名:列名' //刪除某個(gè)列簇下的列
deleteall '表名', '行鍵' //刪除某行數(shù)據(jù)
truncate '表名' //清空表數(shù)據(jù),底層為先禁用再刪除再重新創(chuàng)建
count '表名' //查詢有多少行數(shù)據(jù)
scan '表名', { LIMIT => 行數(shù)} //指定行數(shù)掃描
簡(jiǎn)單錯(cuò)誤排查。
有問(wèn)題建議先檢查節(jié)點(diǎn)和端口是否都正常,然后查看ZK運(yùn)轉(zhuǎn)是否正常
RIT問(wèn)題:一般可以先使用 hbase hbck進(jìn)行錯(cuò)誤掃描,一般出現(xiàn)這個(gè)問(wèn)題,都是由于數(shù)據(jù)讀寫(xiě)異?;蛘呤莾?nèi)存異?;虼罅慷踢B接造成的,一般來(lái)說(shuō)重啟節(jié)點(diǎn)就能解決問(wèn)題。還是沒(méi)有解決,建議手動(dòng)排查一下引用文件是否出了問(wèn)題,如果是的話,刪除異常的引用文件
hbase2.0以下的hbase可以使用hbase hbck -fix進(jìn)行自我修復(fù)。
hbase2.03以上版本可以使用hbck2工具進(jìn)行修復(fù),如果是hbase2.0的話,目前我還沒(méi)有找到可以使用的。