您好,登錄后才能下訂單哦!
說明:本人是用vm搭建的4個節點的服務,分別是node01,node02,node03,node04; node02,node03,node04上分別安裝的zk, node01是hadoop的主節點,node02,node03,node04是hadoop的子節點。本人是把hbase安裝在node04上為hmaster,node03為備hmater,node01,node02,node03為HregionServer
注意:每個節點上的時間要保持一致,否則在寫入hbase時候會出現問題
同步時間方式,百度上還有其他方式
用ntpdate從時間服務器更新時間
如果linux系統沒有ntpdate這個命令,可以輸入以下代碼進行安裝
yum install ntp
安裝完了之后,你不要做什么配置,也不需要,直接測試一下
vi /etc/ntp.conf
add below:
server 1.cn.pool.ntp.org
server 3.asia.pool.ntp.org
server 2.asia.pool.ntp.org
restart service and sync the time
[root@localhost ~]# ntpdate time.nist.gov
22 Oct 21:11:43 ntpdate[5014]: adjust time server 207.200.81.113 offset -0.018788 sec
如果顯示上面的內容說明同步成功了,然后在crontab里面加上以下內容。
/10 * ntpdate time.nist.gov #域名或IP
每隔十分鐘同步一次。推薦幾個時間服務器。
time.nist.gov
time.nuri.net
0.asia.pool.ntp.org
1.asia.pool.ntp.org
2.asia.pool.ntp.org
3.asia.pool.ntp.org
1.上傳hbase安裝包并解壓, 配置環境變量
修改$HBASE_HOME/conf下的hbase_env.sh里的JAVA_HOME路徑以及關掉Hbase自帶得zookeeper(export HBASE_MANAGES_ZK=false)
2.修改$HBASE_HOME/conf下的hbase-site.xm文件
<property>,
<name>hbase.rootdir</name>
<!-mycluster是hadoop里面hdfs-site.xml里配置得服務名稱->
<value>hdfs://mycluster/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop2,hadoop3,hadoop4</value>
</property>
3.修改$HBASE_HOME/conf下的regionserverse文件,配置regionserver
node01
node02
node03
4.修改$HBASE_HOME/conf下的backup-masters(備用節點)里配置:
node03
完成以上步驟把hadoop下面得hdfs-site.xml拷貝到hbase/conf下
把node04上配置好的hbase包分發到node01,node02,node03上注意配置環境變量。如果node01,node02,node03 hbase/conf路徑下沒有hadoop的hdfs-site.xml 在啟動時候啟動不起來。
啟動前hadoop要先起來。在node04 上$HBASE_HOME/bin下執行./start-hbase.sh
這里我在hbase-site.xml中沒有配置端口用的默認端口 頁面默認端口16010,后臺默認端口16000.高版本支持配置端口
<!-- 0.98后的新變動,之前版本沒有.port,默認端口為60000 -->
<property>
<name>hbase.master.port</name>
<value>16000</value>
</property>
hbase.master.maxclockskew設置更大的值
<property>
<name>hbase.master.maxclockskew</name>
<value>180000</value>
<description>Time difference of regionserver from master</description>
</property>
每個節點都可以用命令開啟運行hbase,切換到$HBASE_HOM/bin下 命令:hbase shell
查看幫助命令:
hbase(main):001:0> help
查看表命令:
hbase(main):002:0> list
查看namespace命令:(namespace可以理解為數據庫)
hbase(main):003:0> list_namespace
創建一個新的namespace:
hbase(main):007:0> create_namespace 'test_ns'
在test_ns下創建表:
hbase(main):009:0> create 'test_ns:test_table','info' //創建表要指定表面和列族
如果不指定namespace的話創建的表默認在default 的namespace中
查看namespace的描述:
hbase(main):013:0> describe_namespace 'test_ns'
修改namespace的名稱:
hbase(main):015:0> alter_namespace 'test_ns',{METHOD => 'set', 'name' => 'test_ns2'}
修改前查看:
修改后查看:
刪除namespace的name:
hbase(main):018:0> alter_namespace 'test_ns',{METHOD => 'unset',NAME => 'name'}
刪除表:
hbase(main):022:0> disable 'test_ns:test_table'
hbase(main):023:0> drop 'test_ns:test_table'
刪除namespace:
hbase(main):025:0> drop_namespace 'test_ns' //注意如果namespace里有表,要先刪表再刪namespace,否則直接刪namespace報錯
創建分片表:
hbase(main):013:0> create 'test_ns:teacher','info',SPLITS => ['2','4','6']
往表里插入數據:
說明: 1 是指rowkey, name,sex,age是列名,hbase建議列族即info,不要多.put一次不能插入多條
hbase(main):029:0> put 'test_ns:student','1','info:name','zhangsan'
hbase(main):030:0> put 'test_ns:student','1','info:sex','male'
hbase(main):031:0> put 'test_ns:student','1','info:age','22'
查看表結構:
hbase(main):002:0> describe 'test_ns:student'
添加一個列族:
hbase(main):009:0> alter 'test_ns:student',{NAME => 'info1'}
刪除一個列族:
hbase(main):011:0> alter 'test_ns:student',{'delete' => 'info'}
注意:但表里只有一個列族時,不能刪除,直接刪除報錯
掃描表:
hbase(main):008:0> scan 'test_ns:student'
通過指定版本掃描表:
hbase(main):010:0> scan 'test_ns:student',{RAW => true, VERSIONS => 10}
通過指定列查詢數據:
hbase(main):014:0> scan 'test_ns:student',{COLUMNS => 'info:name'}
分頁查詢:
hbase(main):016:0> scan 'test_ns:student',{COLUMNS => ['info:name'],LIMIT => 2, STARTROW => '1'}
get查詢:
hbase(main):017:0> get 'test_ns:student','1'
hbase(main):018:0> get 'test_ns:student','1','info:name'
根據時間戳查詢 是一個范圍,包頭不包尾
hbase(main):035:0> get 'test_ns:student','1', {TIMERANGE => [1574231458967, 1574234043781]}
更新數據:
hbase(main):037:0> put 'test_ns:student','1','info:age','30'
incr計數器
hbase(main):044:0> incr 'test_ns:student','1','info:name'
刪除一列:
hbase(main):002:0> delete 'test_ns:student','1','info:age'
刪除一行:
hbase(main):004:0> deleteall 'test_ns:student','1'
刪除指定版本:
hbase(main):013:0> delete 'test_ns:student','3','info:name',TIMESTAMP => 1574232778235
判斷表是否存在:
hbase(main):001:0> exists 'test_ns:123'
統計表行數:
hbase(main):002:0> count 'test_ns:student'
表生效和失效
hbase(main):085:0> enable 'myns:user_info'
hbase(main):086:0> disable 'myns:user_info'
清空表數據:
hbase(main):003:0> truncate 'test_ns:student'
=======代碼示例=============
pom.xml的配置
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
代碼示例:
package com.test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.;
import org.apache.hadoop.hbase.client.;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class testHbaseApi {
public static Connection connection = null;
public static Admin admin = null;
static {
try {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","node02,node03,node04");
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
//連接得到用戶
public static void getAdmin() throws IOException {
if(admin == null){
admin = connection.getAdmin();
}
}
//關閉
public static void closeConn() throws IOException {
if(admin != null) {
admin.close();
}
if(connection != null){
connection.close();
}
}
//判斷表是否存在
public static boolean isExistTable(String tableName) throws IOException {
boolean isboolean = admin.tableExists(TableName.valueOf(tableName));
return isboolean;
}
//創建表
public static void createTable(String tableName, String... cfs) throws IOException {
if (tableName == null || tableName.trim().length() == 0) {
System.out.println("tableName isn't null");
return;
}
if (isExistTable(tableName)) {
System.out.println("tableName is exist");
return;
}
if (cfs.length == 0) {
System.out.println("cloumn family isn't null");
return;
}
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
for(String c : cfs){
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.of(c);
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
}
TableDescriptor build = tableDescriptorBuilder.build();
admin.createTable(build);
}
//刪除表
public static void deleteTable(String tablename) throws IOException {
if(!isExistTable(tablename)){
System.out.println("tableName isn't exist");
return;
}
admin.disableTable(TableName.valueOf(tablename));
admin.deleteTable(TableName.valueOf(tablename));
}
//刪除表數據
public static void deleteTableData(String tablename, String rowkey, String columnFamily, String cloumnname)throws IOException {
if(!isExistTable(tablename)){
System.out.println("tableName isn't exist");
return;
}
Table table = connection.getTable(TableName.valueOf(tablename));
Delete delete = new Delete(Bytes.toBytes(rowkey));
delete.addColumns(Bytes.toBytes(columnFamily),Bytes.toBytes(cloumnname));
// delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(cloumnname)); //此方法慎用,如果同一個rowkey第一次put,然后flush,再put一次,執行此方法最近一次會刪掉但第一次put進去的會展現
table.delete(delete);
table.close();
System.out.println("delete is success!");
}
//創建namespace
public static void createNs(String nspace){
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(nspace);
NamespaceDescriptor nsDescriptor = builder.build();
try {
admin.createNamespace(nsDescriptor);
} catch (NamespaceExistException e1){
System.out.println(nspace + " is exist");
} catch (IOException e) {
e.printStackTrace();
}
}
//刪除namespace
public static void deleteNs(String ns) throws IOException {
admin.deleteNamespace(ns);
}
//插入數據
public static void insterData(String tablename, String rowkey, String columnFamily, String cloumnname, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tablename));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(cloumnname), Bytes.toBytes(value));
table.put(put);
table.close();
}
//獲取數據
public static void getData(String tablename, String rowkey, String columnFamily, String cloumnname) throws IOException {
Table table = connection.getTable(TableName.valueOf(tablename));
Get get = new Get(Bytes.toBytes(rowkey));
//獲取指定的列族
// get.addFamily(Bytes.toBytes(columnFamily));
//獲取指定的列族的列
//get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(cloumnname));
//獲取指定的版本
//get.readAllVersions(); //所有版本
//get.readVersions(5); //指定版本
Result result = table.get(get);
for(Cell cell : result.rawCells()){
System.out.println("columnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
",cloumnname: " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
",value: " + Bytes.toString(CellUtil.cloneValue(cell))
);
}
table.close();
}
//scan掃描表數據
public static void scanData(String tablename) throws IOException {
Table table = connection.getTable(TableName.valueOf(tablename));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for(Result r : scanner){
System.out.println("=======" + r);
for(Cell cell : r.rawCells()){
System.out.println("columnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
",cloumnname: " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
",value: " + Bytes.toString(CellUtil.cloneValue(cell))
);
}
}
table.close();
}
public static void main(String[] args) throws IOException {
getAdmin();
System.out.println(isExistTable("test_ns:student"));
System.out.println("==================下面創建表========================");
System.out.println(isExistTable("test_ns:teacher"));
createTable("test_ns:teacher","info","info1");
System.out.println(isExistTable("test_ns:teacher"));
System.out.println("==================下面刪除表========================");
deleteTable("test_ns:teacher");
System.out.println("==================下面創建ns========================");
createNs("test_ns");
System.out.println("==================下面刪除ns========================");
deleteNs("test_ns1");
System.out.println("==================下面插入表數據========================");
insterData("test_ns:student","001","info","name","zhangsan");
System.out.println("==================下面獲取表數據========================");
getData("test_ns:student","001",null,null);
System.out.println("==================下面遍歷表數據========================");
scanData("test_ns:student");
System.out.println("==================下面刪除具體表數據========================");
deleteTableData("test_ns:student","003","","");
System.out.println("==================下面刪除具體表數據 按列族 列名刪========================");
deleteTableData("test_ns:student","006","info","name");
closeConn();
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。