1.在单机模式的基础上进行配置,打开hbase-env.sh。
vim /usr/local/hbase/conf/hbase-env.sh
2.配置HBASE_CLASSPATH为hadoop安装目录下的conf目录,即 /usr/local/hadoop/conf。JAVA_HOME、HBASE_MANAGES_ZK之前已经配置好了。
export HBASE_CLASSPATH=/usr/local/hadoop/conf
3.打开hbase-site.xml文件,hbase.rootdir指定hbase数据在HDFS的存储路径;将hbase.cluter.distributed设置为true。
vim /usr/local/hbase/conf/hbase-site.xml
配置信息如下
<configuration><property><name>hbase.rootdir</name><value>hdfs://localhost:9000/hbase</value>#指定HBase的存储目录。</property><property><name>hbase.cluster.distributed</name>#设置集群处于分布式模式<value>true</value></property><property><name>hbase.unsafe.stream.capability.enforce</name>#避免出现启动错误。<value>false</value></property>
</configuration>
4.Hbase的启动测试,先启动hadoop,在启动hbase。
ssh localhost
cd /usr/local/hadoop
./sbin/start-dfs.sh
jps
cd /usr/local/hbase
bin/start-hbase.sh
jps
最终看到如下信息,则成功开启。
5.关闭, 先关闭hbase,在关闭hadoop。
bin/stop-hbase.sh
cd /usr/local/hadoop
./sbin/stop-dfs.sh
6.Hbase shell 命令
- 进入hbase的shell
hbase shell
在这里插入代码片
7.Java API编程
- 导入hbase的lib目录下的所有jar包。
package my.hbase;
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 HbaseCode {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void main(String[] args)throws IOException{init();createTable("student-info",new String[]{"score"});insertData("student-info","zhangsan","score","English","69");insertData("student-info","zhangsan","score","Math","86");insertData("student-info","zhangsan","score","Computer","77");getData("student-info", "zhangsan", "score","English");close();}public static void init(){configuration = HBaseConfiguration.create();//设置hbase数据存储的根路径,放在分布式文件系统hdfs目录下configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");try{//建立链接connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();}catch (IOException e){e.printStackTrace();}}public static void close(){try{if(admin != null){admin.close();}if(null != connection){connection.close();}}catch (IOException e){e.printStackTrace();}}public static void createTable(String myTableName,String[] colFamily) throws IOException {TableName tableName = TableName.valueOf(myTableName);if(admin.tableExists(tableName)){System.out.println("talbe is exists!");}else {//管理表的信息TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);for(String str:colFamily){//管理列族的类ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();tableDescriptor.setColumnFamily(family);}admin.createTable(tableDescriptor.build());} }public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName));Put put = new Put(rowKey.getBytes());put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());table.put(put);table.close(); }public static void getData(String tableName,String rowKey,String colFamily, String col)throws IOException{ Table table = connection.getTable(TableName.valueOf(tableName));Get get = new Get(rowKey.getBytes());get.addColumn(colFamily.getBytes(),col.getBytes());Result result = table.get(get);System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));table.close(); }
}
运行结果查看如下student-info表。
8.源链接
厦门大学数据库实验室