單機版: (hbase內嵌有zookeeper)
解壓hbase軟件,到達conf目錄下
配置hbase-site.xml文件
<configuration>
<property>
<name>hbase.rootdir</name> 配置hbase存在位置
<value>file:///home/testuser/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name> 配置hbase文件保存路徑
<value>/home/testuser/zookeeper</value>
</property>
</configuration>
配置hbase-env.sh文件:把29行的註釋取消,配置虛擬機上面的Java地址
28 # The java implementation to use. Java 1.6 required.
29 export JAVA_HOME=/usr/java/default
30
31 # Extra Java CLASSPATH elements. Optional.
32 # export HBASE_CLASSPATH
然後使用start-hbase.sh命令啓動hbase
使用hbase shell 進入hbase的命令提示界面,進行hbase語句操作練習
hbase集羣:(高可用)
配置hbase-site.xml文件
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://bx/hbase</value> 此處的bx爲以前搭建hdfs集羣時,使用的那個集羣代號
</property>
<property>
<name>hbase.zookeeper.quorum</name> 指定集羣的幾臺zookeeper主機名
<value>CentOS8,CentOS9,CentOS10,CentOS15</value>
</property>
<property>
<name>hbase.cluster.distributed</name> 設置使用zookeeper集羣,true爲使用
<value>true</value>
</property>
</configuration>
配置hbase-env.sh文件:把124行的註釋取消,把true改爲false,代表不使用自帶的zookeeper
121 # export HBASE_SLAVE_SLEEP=0.1
122 #
123 # Tell HBase whether it should manage it's own instance of Zookeeper or not.
124 export HBASE_MANAGES_ZK=false 默認爲true
125 #
設置regionservers文件
vi regionservers
CentOS8 設置四個regionserver節點
CentOS9
CentOS10
CentOS15
創建backup-masters文件
vi backup-masters
CentOS8 指定兩臺master主機
CentOS9
然後再把hadoop2.6.5/etc/hadoop/hdfs-site.xml文件拷貝到該目錄下
到此,配置文件準備完畢。
首先每臺機器啓動zkServer.start.sh
然後在配了免密鑰登錄的機器上執行start-dfs.sh ,啓動hdfs集羣
最後,在start-hbase.sh 啓動hbase數據庫
安裝完畢後就開始可以使用了
創建命名空間:
create_namespace 'benxi'
創建region:
create 't_user','f1',f2'
插入數據:
put 't_user','001','f1:name','張三' 第一個爲表名,第二個爲Row Key,第三個爲列族名:屬性名,第四個爲屬性的value值
put 't_user','001','f1:age','13'
查看錶結構:desc 't_user'
顯示結果如下:(部分參數解釋)
Table t_user is ENABLED
t_user
COLUMN FAMILIES DESCRIPTION
{NAME=>’f1’, 列族名
DATA_BLOCK_ENCODING=>’NONE’,
BLOOMFILTER=>’ROW’,
REPLICATION_SCOPE => ‘0’,
VERSIONS => ‘1’, 最大保存數爲1
COMPRESSION => ‘NONE’,
MIN_VERSIONS => ‘0’, 最小保存數爲0
TTL=>FOREVER’, 保存時間爲永久有效
KEEP_DELETED_CELLS => ‘FALSE’,
BLOCKSIZE=>’65536’, 一個塊大小,單位字節,64k
IN_MEMORY=>’false’,
BLOCKCACHE => ‘true’} 默認爲true,開啓查詢保存
禁用命名空間
disabled ‘t_user’
刪除命名空間 首先要禁用表才能刪除。
drop 't_user'
接下來使用Java代碼進行操作:
public class create_test1 {
/**
* 案例1:
* 存放電信通話數據
* 表:t_phone_recode --> t_cdr
* rowkey:phone+Long.maxvalue-date
* 列族:cf1:
*/
String tableName = "t_phone_recode";
Random r = new Random();
@Test
public void createTable() throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表的對象
HBaseAdmin admin = new HBaseAdmin(config);
//判斷該表是否存在
if(admin.tableExists(tableName)){
//使表失效,然後才能進行刪除操作
admin.disableTable(tableName);
//刪除表操作
admin.deleteTable(tableName);
}
//創建一個表對象
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
//創建列族,需要創建多個列族,就多寫幾個
HColumnDescriptor columnfamily = new HColumnDescriptor("cf1");
//設置列族的最大cell保存數量(默認爲2),和設置列族最小保存數量(默認爲0)
columnfamily.setMaxVersions(2);
columnfamily.setMinVersions(1);
//把列族設置添加進表中
table.addFamily(columnfamily);
//把表對象添加到HBaseAdmin對象中去
admin.createTable(table);
//關閉連接
admin.close();
}
/**
* 列族裏面的列:當前手機號,目標手機號,通話類型,通話時間
*/
@Test
public void insertDates() throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表操作對象,前面一個爲configuration對象,後面爲表名
HTable table = new HTable(config, tableName);
for(int i = 0;i<100;i++){
//隨機生成一個手機號
String phone = getPhone("151");
//創建put集合對象
List<Put> puts = new ArrayList<Put>();
for(int j = 0;j<10;j++){
//隨機生成一個日期,雖然生成的可能不準確,可能會出現20190230這樣的日期
String date = getDate("2019");
//把手機號和日期拼接在一起作爲rowkey
String rowkey = phone + "_"+(Long.MAX_VALUE-Long.parseLong(date));
//put對象用來進行對HBase操作
Put put = new Put(rowkey.getBytes());
//把rowkey放到Put對象中去
put.add("cf1".getBytes(),"phone".getBytes(),phone.getBytes());
put.add("cf1".getBytes(),"dest".getBytes(),getPhone("151").getBytes());
put.add("cf1".getBytes(),"type".getBytes(),(r.nextInt(2)+"").getBytes());
put.add("cf1".getBytes(),"date".getBytes(),date.getBytes());
//把put對象添加到集合中
puts.add(put);
}
//把生成的put對象放到HTable對象中去
table.put(puts);
}
table.close();
}
/**
* 已知rowkey查詢一行數據
* @throws Exception
*/
@Test
public void findOne() throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表操作對象,前面一個爲configuration對象,後面爲表名
HTable table = new HTable(config, tableName);
String rowkey = "15198932957_9223351846730721367";
//通過Get獲取rowkey爲15198518559_9223351846450735的對象
Get get = new Get(rowkey.getBytes());
//設置訪問攔截後,設置這個繼續訪問
get.setAttribute("user", "zs".getBytes());
//把rowkey關聯表,Result對象代表hbase中一行數據
Result re = table.get(get);
// byte[] value = re.getValue("cf1".getBytes(), "dest".getBytes());
// System.out.println(new String(value));
//獲取最高版本的,最新的那條數據。獲取一個單元格
Cell destCell=re.getColumnLatestCell("cf1".getBytes(), "dest".getBytes());
String dest = new String(CellUtil.cloneValue(destCell));
String type = new String(CellUtil.cloneValue(re.getColumnLatestCell("cf1".getBytes(),"dest".getBytes())));
System.out.println("目標手機號:"+dest);
System.out.println("通話類型:"+(type.equals("1")?"主叫":"被叫"));
}
/**
* 查詢一個手機號15198518559第二個季度的所有通話記錄
*/
@Test
public void findRowsTwo() throws Exception{
Configuration config = new Configuration();
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
HTable table = new HTable(config, tableName);
//使用正則匹配條件,^15198932957_表示以15198932957_開頭的rowkey; $1234表示匹配以1234結尾的rowkey
RowFilter rf = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^15198932957_") );
//使用Scan查詢多條數據
Scan scan = new Scan();
scan.setFilter(rf);
//使用ResultScanner對象獲取表中所有數據
ResultScanner res = table.getScanner(scan);
for (Result result : res) {
System.out.println(new String(CellUtil.cloneValue(result.getColumnLatestCell("cf1".getBytes(),"dest".getBytes()))));
System.out.println(new String(CellUtil.cloneValue(result.getColumnLatestCell("cf1".getBytes(),"phone".getBytes()))));
System.out.println(new String(CellUtil.cloneValue(result.getColumnLatestCell("cf1".getBytes(),"date".getBytes()))));
System.out.println("-----------------");
}
}
@Test
public void findRows() throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表操作對象,前面一個爲configuration對象,後面爲表名
HTable table = new HTable(config, tableName);
//使用Scan對象查詢多個數據
Scan scan = new Scan();
String startRowkey ="15195946934_"+(Long.MAX_VALUE-20190630246060l);
String endRowkey ="15195946934_"+(Long.MAX_VALUE-20190331246060l);
scan.setStartRow(startRowkey.getBytes());
scan.setStopRow(endRowkey.getBytes());
ResultScanner res =table.getScanner(scan);
for(Result re :res){
Cell destCell=re.getColumnLatestCell("cf1".getBytes(), "dest".getBytes());//獲取一行裏面的其中一個單元格
String dest =new String(CellUtil.cloneValue(destCell));
String type =new String(CellUtil.cloneValue(re.getColumnLatestCell("cf1".getBytes(), "type".getBytes())));
String date =new String(CellUtil.cloneValue(re.getColumnLatestCell("cf1".getBytes(), "date".getBytes())));
String phone =new String(CellUtil.cloneValue(re.getColumnLatestCell("cf1".getBytes(), "phone".getBytes())));
System.out.println("通話時間:"+date);
System.out.println("目標手機號"+dest);
System.out.println("通話類型:"+(type.equals("1")?"主叫":"被叫"));
System.out.println("-------------------------------");
}
table.close();
}
/**
* 查詢手機號13899154250,的所有主叫記錄
*/
@Test
public void findRows2()throws Exception{
Configuration config =new Configuration();
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
HTable htable =new HTable(config, tableName);
PrefixFilter rowkeyPrefix =new PrefixFilter("15198932957".getBytes());
SingleColumnValueFilter f2 =new SingleColumnValueFilter("cf1".getBytes(), "type".getBytes(), CompareOp.EQUAL, "1".getBytes());//判斷一個列的值是否等於(大於,小於等)某個指定的value
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
list.addFilter(rowkeyPrefix);
list.addFilter(f2);
Scan scan =new Scan();
scan.setFilter(list);
ResultScanner res =htable.getScanner(scan);
for(Result re :res){
Cell destCell=re.getColumnLatestCell("cf1".getBytes(), "dest".getBytes());//獲取一行裏面的其中一個單元格
String dest =new String(CellUtil.cloneValue(destCell));
String type =new String(CellUtil.cloneValue(re.getColumnLatestCell("cf1".getBytes(), "type".getBytes())));
String date =new String(CellUtil.cloneValue(re.getColumnLatestCell("cf1".getBytes(), "date".getBytes())));
String phone =new String(CellUtil.cloneValue(re.getColumnLatestCell("cf1".getBytes(), "phone".getBytes())));
System.out.println("通話時間:"+date);
System.out.println("目標手機號"+dest);
System.out.println("通話類型:"+(type.equals("1")?"主叫":"被叫"));
System.out.println("-------------------------------");
}
htable.close();
}
@Test
public void findRowTwo2() throws Exception{
Configuration config = new Configuration();
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
HTable htable =new HTable(config, tableName);
//設置查詢條件,因爲條件比較多,使用過濾器集合對象存儲FilterList,MUST_PASS_ALL 設置多個條件,MUST_PASS_ONE 設置一個條件
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
//判斷一個列的值是否等於(大於,小於等)某個指定的value。 第一個指定列族,第二個參數指定屬性名,第三個指定比較方式(EQUAL爲相等),第四個參數代表比較對比的值
SingleColumnValueFilter f1 = new SingleColumnValueFilter("cf1".getBytes(), "type".getBytes(), CompareOp.EQUAL, "1".getBytes());
//設置前綴過濾器,查詢rowkey爲15198932957的所有數據
PrefixFilter rowkeyPrefix = new PrefixFilter("15198932957".getBytes());
//把過濾條件添加進入list集合中
list.addFilter(f1);
list.addFilter(rowkeyPrefix);
//使用Scan查詢多條數據
Scan scan = new Scan();
//給scan對象設置過濾器,可以設置一條,也可以是一個集合
scan.setFilter(list);
//把使用迭代器
ResultScanner res = htable.getScanner(scan);
for (Result result : res) {
System.out.println("通話時間:"+new String(CellUtil.cloneValue(result.getColumnLatestCell("cf1".getBytes(),"date".getBytes()))));
System.out.println("當前手機號:"+new String(CellUtil.cloneValue(result.getColumnLatestCell("cf1".getBytes(),"phone".getBytes()))));
System.out.println("目標手機號:"+new String(CellUtil.cloneValue(result.getColumnLatestCell("cf1".getBytes(),"dest".getBytes()))));
System.out.println("電話類型:"+((new String(CellUtil.cloneValue(result.getColumnLatestCell("cf1".getBytes(),"type".getBytes())))).equals("1")?"主叫":"被叫"));
}
htable.close();
}
//修改某個單元格的內容
@Test
public void update()throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表操作對象,前面一個爲configuration對象,後面爲表名
HTable table = new HTable(config, tableName);
//把手機號和日期拼接在一起作爲rowkey
String rowkey = "15198932957_9223351846730721367";
//put對象用來進行對HBase操作
Put put = new Put(rowkey.getBytes());
//把rowkey放到Put對象中去
put.add("cf1".getBytes(),"dest".getBytes(),getPhone("151").getBytes());
put.add("cf1".getBytes(),"type".getBytes(),(r.nextInt(2)+"").getBytes());
put.add("cf1".getBytes(),"date".getBytes(),getDate("2017").getBytes());
table.put(put);
table.close();
}
//刪除一個指定column(所有版本)
@Test
public void delete2() throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表操作對象,前面一個爲configuration對象,後面爲表名
HTable table = new HTable(config, tableName);
String rowkey = "15198932957_9223351846240714672";
//創建Delete對象,把rowkey傳遞過去
Delete del = new Delete(rowkey.getBytes());
//刪除最新的符合條件的數據
Delete delete = del.deleteColumn("cf1".getBytes(), "dest".getBytes());
table.delete(delete);
table.close();
}
@Test
public void delete() throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表操作對象,前面一個爲configuration對象,後面爲表名
HTable table = new HTable(config, tableName);
//把手機號和日期拼接在一起作爲rowkey
String rowkey = "15198932957_9223351846240714672";
//通過Get獲取rowkey爲15198518559_9223351846450735的對象
Get get = new Get(rowkey.getBytes());
//把rowkey關聯表,Result對象代表hbase中一行數據
Result re = table.get(get);
// byte[] value = re.getValue("cf1".getBytes(), "dest".getBytes());
// System.out.println(new String(value));
//獲取最高版本的,最新的那條數據。獲取一個單元格
// Cell cell=re.getColumnLatestCell("cf1".getBytes(), "dest".getBytes());
// Cell ce = CellUtil.createCell();
Delete del = new Delete(rowkey.getBytes());
del.deleteColumn("cf1".getBytes(), "dest".getBytes());
// del.addDeleteMarker(cell);
table.delete(del);
table.close();
}
//刪除指定的一行數據
@Test
public void deleteRowkey() throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表操作對象,前面一個爲configuration對象,後面爲表名
HTable table = new HTable(config, tableName);
String rowkey = "15198932957_9223351846240714672";
//創建Delete對象,把rowkey傳遞過去
Delete del = new Delete(rowkey.getBytes());
//刪除最新的符合條件的數據
// Delete delete = del.deleteColumn("cf1".getBytes(), "dest".getBytes());
table.delete(del);
table.close();
}
//刪除13899154250第3季度的通話記錄
@Test
public void deleteByPhone() throws Exception{
Configuration config = new Configuration();
//指定zookeeper集羣,和hbase-size.xml文件一樣
config.set("hbase.zookeeper.quorum", "CentOS8,CentOS9,CentOS10,CentOS15");
//創建表操作對象,前面一個爲configuration對象,後面爲表名
HTable table = new HTable(config, tableName);
Scan scan = new Scan();
//獲得第三季度,long的最大值減去季度最大日期,得到起始日期
String startdate = "15198902068_"+(Long.MAX_VALUE-20190930246060l);
String lastdate = "15198902068_"+(Long.MAX_VALUE-20190630246060l);
//把起始時間戳設置進Scan
scan.setStartRow(startdate.getBytes());
scan.setStopRow(lastdate.getBytes());
// Delete del = new Delete("".getBytes());
//往table對象中添加scan條件設置,創建ResultScanner對象,獲取當前表的所有數據
ResultScanner res =table.getScanner(scan);
//遍歷該表
for(Result re :res){
//獲取一行裏面的其中一個單元格
Cell destCell=re.getColumnLatestCell("cf1".getBytes(), "dest".getBytes());
//獲取這列數據的rowkey,得到的是內存地址。一般不使用
String str = re.getRow().toString();
System.out.println(str);
//取出當前單元格的電話號碼所屬
String phone =new String(CellUtil.cloneValue(re.getColumnLatestCell("cf1".getBytes(), "phone".getBytes())));
//如果當前電話號碼爲15198902068,則刪除這個數據
if("15198902068".equals(phone)){
//設置刪除條件
Delete del = new Delete(re.getRow());
//刪除數據
table.delete(del);
continue;
}
}
table.close();
}
//生成隨機電話號碼,傳入前三位數,後面八位數隨機生成
private String getPhone(String prefix){
return prefix+String.format("%08d", r.nextInt(99999999));
}
//隨機生成日期,年份需要傳入,月日時分秒隨機生成
private String getDate(String year){
return year+String.format("%02d%02d%02d%02d%02d", new Object[]{r.nextInt(12)+1,r.nextInt(31)+1,r.nextInt(24),r.nextInt(60),r.nextInt(60)});
}
}