Hbase数据库是在linux系统下开发的,首先 要安装一个虚拟机,再装上linux
一.安装Hbase单机模式
1.官方下载Hbase
http://apache.claz.org/hbase/,本人下载1.2.6版本 hbase-1.2.6-bin.tar.gz
2.解压操作 我把他解压到/usr/Hbase 路径下 Hbase为新建的文件夹
3
Hbase配置 在conf文件夹中
配置hbase-env.sh
编辑JAVA_HOME环境变量,改变路径到当前JAVA_HOME变量:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
java 可以使用linux自带的jdk 也可以自己安装配置,因为我自带的jdk是1.8版本的,所以就直接使用了自带的jdk.主要是看jdk的版本。
将这两个export删去。否则会报错
配置hbase-site.xml
设置数据保存的目录
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:/usr/Hbase/hbase-1.2.6/database</value>
</property>
</configuration>
到此 HBase 的安装配置已成功完成。
4.运行Hbase
可以通过使用 HBase 的 bin 文件夹中提供 start-hbase.sh 脚本启动 HBase。
在bin目录中打开终端,运行下面这个命令
sudo ./start-hbase.sh
出现下面就okl
starting master, logging to /usr/Hbase/hbase-1.2.6/bin/../logs/hbase-root-master-shi-virtual-machine.out
打开浏览器 在网址输入框 输入 localhost:16010 弹出下图即可
进入hbase shell 界面
在bin界面 输入
./hbase shell
单机模式的Hbase就安装完成了
二.java连接Hbase
我是在win10系统中连接的虚拟机上linux系统里的Hbase
1.创建java工程,
2.导入jar包
jar包最好使用的是hbase中lib里面的jar将这些jar包导入工程中,他对版本的要求比较高,所以使用hbasez中的jar包最好。
3.贴入代码
package
com.sjy.Hbasetest;
import
java.io.IOException;
import
java.util.HashMap;
import
java.util.Map;
import
java.util.ArrayList;
import
java.util.List;
import
org.apache.hadoop.conf.Configuration;
import
org.apache.hadoop.hbase.HBaseConfiguration;
import
org.apache.hadoop.hbase.HColumnDescriptor;
import
org.apache.hadoop.hbase.HTableDescriptor;
import
org.apache.hadoop.hbase.KeyValue;
import
org.apache.hadoop.hbase.client.Delete;
import
org.apache.hadoop.hbase.client.Get;
import
org.apache.hadoop.hbase.client.HBaseAdmin;
import
org.apache.hadoop.hbase.client.HTable;
import
org.apache.hadoop.hbase.client.Put;
import
org.apache.hadoop.hbase.client.Result;
import
org.apache.hadoop.hbase.client.ResultScanner;
import
org.apache.hadoop.hbase.client.Scan;
import
org.apache.hadoop.hbase.util.Bytes;
public
class
Test
{
static
final
String
rowKey
=
"row1"
;
static
HBaseAdmin
hBaseAdmin
;
static
Configuration
conf
;
static
{
conf
= HBaseConfiguration.
create
();
//linux的ip
conf
.set(
"hbase.zookeeper.quorum"
,
"192.168.175.134"
);
try
{
hBaseAdmin
=
new
HBaseAdmin
(
conf
)
;
}
catch
(IOException
e
)
{
e
.printStackTrace();
}
}
public
static
void
createTable(String
tableName
, String[]
columns
)
throws
Exception
{
dropTable
(
tableName
);
HTableDescriptor
hTableDescriptor
=
new
HTableDescriptor
(
tableName
)
;
for
(String
columnName
:
columns
)
{
HColumnDescriptor
column
=
new
HColumnDescriptor(
columnName
);
hTableDescriptor
.addFamily(
column
);
}
hBaseAdmin
.createTable(
hTableDescriptor
);
System.
out
.println(
"create table successed"
);
}
public
static
void
dropTable(String
tableName
)
throws
Exception
{
if
(
hBaseAdmin
.tableExists(
tableName
))
{
hBaseAdmin
.disableTable(
tableName
);
hBaseAdmin
.deleteTable(
tableName
);
}
System.
out
.println(
"drop table successed"
);
}
public
static
HTable getHTable(String
tableName
)
throws
Exception
{
return
new
HTable
(
conf
,
tableName
)
;
}
public
static
void
insert(String
tableName
, Map<String, String>
map
)
throws
Exception
{
HTable
hTable
=
getHTable
(
tableName
);
byte
[]
row1
= Bytes.
toBytes
(
rowKey
);
Put
p1
=
new
Put(
row1
);
for
(String
columnName
:
map
.keySet())
{
byte
[]
value
= Bytes.
toBytes
(
map
.get(
columnName
));
String[]
str
=
columnName
.split(
":"
);
byte
[]
family
= Bytes.
toBytes
(
str
[0]);
byte
[]
qualifier
=
null
;
if
(
str
.
length
> 1)
{
qualifier
= Bytes.
toBytes
(
str
[1]);
}
p1
.
add
(
family
,
qualifier
,
value
)
;
}
hTable
.put(
p1
);
Get
g1
=
new
Get(
row1
);
Result
result
=
hTable
.get(
g1
);
System.
out
.println(
"Get: "
+
result
);
System.
out
.println(
"insert successed"
);
}
public
static
void
delete(String
tableName
, String
rowKey
)
throws
Exception
{
HTable
hTable
=
getHTable
(
tableName
);
List<Delete>
list
=
new
ArrayList<Delete>();
Delete
d1
=
new
Delete(Bytes.
toBytes
(
rowKey
));
list
.add(
d1
);
hTable
.delete(
list
);
Get
g1
=
new
Get(Bytes.
toBytes
(
rowKey
));
Result
result
=
hTable
.get(
g1
);
System.
out
.println(
"Get: "
+
result
);
System.
out
.println(
"delete successed"
);
}
public
static
void
selectOne(String
tableName
, String
rowKey
)
throws
Exception
{
HTable
hTable
=
getHTable
(
tableName
);
Get
g1
=
new
Get(Bytes.
toBytes
(
rowKey
));
Result
result
=
hTable
.get(
g1
);
foreach
(
result
);
System.
out
.println(
"selectOne end"
);
}
private
static
void
foreach(Result
result
)
throws
Exception
{
for
(KeyValue
keyValue
:
result
.
raw
()
)
{
StringBuilder
sb
=
new
StringBuilder();
sb
.append(Bytes.
toString
(
keyValue
.
getRow
()
)).append(
"\t"
);
sb
.append(Bytes.
toString
(
keyValue
.
getFamily
()
)).append(
"\t"
);
sb
.append(Bytes.
toString
(
keyValue
.
getQualifier
()
)).append(
"\t"
);
sb
.append(
keyValue
.getTimestamp()).append(
"\t"
);
sb
.append(Bytes.
toString
(
keyValue
.
getValue
()
)).append(
"\t"
);
System.
out
.println(
sb
.toString());
}
}
public
static
void
selectAll(String
tableName
)
throws
Exception
{
HTable
hTable
=
getHTable
(
tableName
);
Scan
scan
=
new
Scan();
ResultScanner
resultScanner
=
null
;
try
{
resultScanner
=
hTable
.getScanner(
scan
);
for
(Result
result
:
resultScanner
)
{
foreach
(
result
);
}
}
catch
(Exception
e
)
{
e
.printStackTrace();
}
finally
{
if
(
resultScanner
!=
null
)
{
resultScanner
.close();
}
}
System.
out
.println(
"selectAll end"
);
}
public
static
void
main(String[]
args
)
throws
Exception
{
String
tableName
=
"member3"
;
String[]
columns
=
new
String[] {
"column_A"
,
"column_B"
};
createTable
(
tableName
,
columns
);
Map<String, String>
map
=
new
HashMap<String, String>();
map
.put(
"column_A"
,
"AAA"
);
map
.put(
"column_B:1"
,
"b1"
);
map
.put(
"column_B:2"
,
"b2"
);
insert
(
tableName
,
map
);
System.
out
.println(
"---------------"
);
selectOne
(
tableName
,
rowKey
);
System.
out
.println(
"---------------"
);
selectAll
(
tableName
);
//delete(tableName, rowKey);
//dropTable(tableName);
}
}
4.bug
1
2
5解决bug
bug1
解决方法
大多是你的hbase没有启动成功
bug2
解决方法
打开linux 中的 /etc/hosts文件,更改
127.0.0.1 localhost
127.0.1.1 shi-virtual-machine
为
本机ip
shi-virtual-machine
更改完成重新运行hbase
win10系统下找到
更改hosts文件 添加
运行代码 ok!!!!!!!!