Hbase集群搭建
1、官网下载Hbase解压值指定目录(在master机器上)
2、配置hbase环境
cd /home/master/hbase-2.2.5/conf
(1)修改hbase-env.sh配置
export JAVA_HOME=/usr/local/java/jdk8
export HBASE_MANAGES_ZK=false
(2)修改hbase-site.xml配置
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://master:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.master.port</name>
<value>16000</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>master:2181,slave1:2181,slave2:2181</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/master/zookeeper-3.4.14/zkdata</value>
</property>
</configuration>
(3) regionservers添加集群主机
master
slave1
slave2
(4)软连接hadoop配置文件到hbase:
[root@master master]#
ln -s /home/master/hadoop-2.9.2/etc/hadoop/core-site.xml /home/master/hbase-2.25/conf/core-site.xml
[root@master master]#
ln -s /home/master/hadoop-2.9.2/etc/hadoop/hdfs-site.xml /home/master/hbase-2.25/conf/hdfs-site.xml
(5)将hbase-2.2.5分发到集群其他机器上
cp hbase-2.2.5 root@slave1:/home/master
cp hbase-2.2.5 root@slave2:/home/master
(6)启动hbase集群,由于hbase依赖hdfs和zookeeper,需要先启动hdfs和zookeeper
方式1:
bin/hbase-daemon.sh start master
bin/hbase-daemon.sh start regionserver
方式2:
hbase-2.2.5/bin/start-hbase.sh
启动成功后访问hbase管理页面 http://master:16010
3、脚本操作hbase
进入脚本
bin/hbase shell
(1) 创建表
创建表名为country,列簇名为china的表
hbase(main):001:0> create ‘country’,‘china’
(2)插入数据
put 'country','中国','china:河南','郑州'
put 'country','中国','china:广东','广州'
put 'country','中国','china:江苏','南京'
(3)扫描表
scan 'country'
(4)更新字段
put 'country','中国','china:广东','深圳'
(5)查看指定列
get 'country','河南'
4、javaApi 操作hbase
package org.cn.fcw;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
/**
* Hello world!
*/
public class HBaseApp {
public static Connection connection;
static {
try {
//使用HBaseConfiguration的单例方法实例化
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master");
conf.set("hbase.zookeeper.property.clientPort", "2181");
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
String tbName="country";
boolean exist = isTableExist(tbName);
if(exist){
System.out.println("向"+tbName+"中添加数据");
String columnFamily="china";
String columnName="河南";
String rowKey=tbName+"#"+columnFamily+"#"+columnName+"#"+System.currentTimeMillis();
insertTable(tbName,rowKey,columnFamily,columnName,"信阳");
}else{
System.out.println(tbName+"-------表不存在{}");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static HBaseAdmin getAdmin() throws IOException {
System.out.println("连接HBase集群");
return (HBaseAdmin) connection.getAdmin();
}
//判断表是否存在
public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
//在HBase中管理、访问表需要先创建HBaseAdmin对象
//HBaseAdmin admin = new HBaseAdmin(conf);
TableName table = TableName.valueOf(tableName);
return getAdmin().tableExists(table);
}
//创建表
public static void createTable(String tbName, String[] columnFamilies) throws IOException {
if (isTableExist(tbName)) {
System.out.println("表已经存在");
return;
}
TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor = new TableDescriptorBuilder.ModifyableTableDescriptor(TableName.valueOf(tbName));
for (String columnFamily : columnFamilies) {
ColumnFamilyDescriptor columnFamilyDescriptor = new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(columnFamily.getBytes());
tableDescriptor.setColumnFamily(columnFamilyDescriptor);
}
getAdmin().createTable(tableDescriptor);
}
//刪除表
public static void deleteTable(String tbName) throws IOException {
if (isTableExist(tbName)) {
HBaseAdmin admin = getAdmin();
admin.disableTable(TableName.valueOf(tbName));
admin.deleteTable(TableName.valueOf(tbName));
return;
}
System.out.println(tbName + "表不存在");
}
/**
* 插入数据
*
* @param tbName 表名
* @param rowKey 行键(来检索记录的主键)
* @param columnFamily 列簇
* @param column 列名
* @param value 列值
*/
public static void insertTable(String tbName, String rowKey, String columnFamily, String column, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tbName));
Put put = new Put(rowKey.getBytes());
put.addColumn(columnFamily.getBytes(), column.getBytes(), value.getBytes());
table.put(put);
}
}
还没有评论,来说两句吧...