Hbase的安装,Hbaseshell以及测试代码
说明:本人是用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文件
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.高版本支持配置端口
hbase.master.maxclockskew设置更大的值
每个节点都可以用命令开启运行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的配置
org.apache.hbase
hbase-server
2.0.4
代码示例:
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();
}
}
文章名称:Hbase的安装,Hbaseshell以及测试代码
网站URL:http://azwzsj.com/article/iedspd.html