1.下载Hbase的安装包
(选择与自己安装的Hadoop版本的兼容版本,见后面附录)
官网下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/stable/
选择稳定版hbase-1.4.9-bin.tar.gz,在Windows里面下载。
2.上传到centos7中
可以通过secureCRT上传,也可以xshell上传
3.解压安装
tar -zxvf ~/hbase-1.4.9-bin.tar.gz -C /opt/module
4. 配置HBASE环境变量
vi /etc/profile
export HBASE_HOME=/opt/module/hbase-1.4.9
export PATH=$HBASE_HOME/bin:$PATH
5.测试Hbase是否安装成功
hbase
6.配置HBASE
1.进入到HBASE的安装目录
cd /opt/module/hbase-1.4.9/conf
2.配置hbase-env.sh
设置Java安装路径
设置HBase的配置文件路径(/opt/module/hbase/conf)
采用HBase自带Zookeeper,设置参数trueexport JAVA_HOME=/opt/module/jdk1.8.0_121 export HBASE_CLASSPATH=/opt/module/hbase/conf export HBASE_MAVAGES_ZK=true
3.配置hbase-site.xml
vi hbase-site.xml<configuration> <property> <name>hbase.rootdir</name> <value>hdfs://bigdata128:9000/hbase</value> </property> <!-- 指定hbase是分布式的 --> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.master</name> <value>hdfs://bigdata128:6000</value> </property> <!-- 指定zk的地址,多个用“,”分割 --> <property> <name>hbase.zookeeper.quorum</name> <value>bigdata128</value> </property> <property> <name>hbase.zookeeper.property.dataDir</name> <value>/bigdata/zookeeper/data</value> </property> </configuration>
7.启动HBASE
提示:在启动HBASE之前先启动Hadoop
start-hbase.sh
进入hbase数据库
hbase shell
hbase shell命令:
#查看有哪些表
list
# 创建表
# 语法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}
# 例如:创建表t1,有两个family name:f1,f2,且版本数均为2
hbase(main)> create 't1',{NAME => 'f1', VERSIONS => 2},{NAME => 'f2', VERSIONS => 2}
#插入字段
put 't1', 'r1', 'f1', 'v1'
put 't1', 'r2', 'f2', 'v2'
put 't1', 'r3', 'f3', 'v3'
3)删除表
分两步:首先disable,然后drop
例如:删除表t1
hbase(main)> disable 't1'
hbase(main)> drop 't1'
# 例如:查看表t1的结构
# 语法:describe <table>
hbase(main)> describe 't1'
进行Hbase端口访问
http://192.168.217.129:16010
利用java生成jar包实现文件的对HBase进行操作
maven项目中的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyy</groupId>
<artifactId>HBASE</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 此处指定main方法入口的class -->
<mainClass>hbase.Hbase</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package 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 Hbase {
public static Configuration conf;
public static Connection con;
public static Admin adm;
@SuppressWarnings("all")
public static void init() throws IOException {
conf=HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://192.168.217.129:9000/hbase");
con= ConnectionFactory.createConnection(conf);
adm = con.getAdmin();
System.out.println(adm);
}
public static void createTable(String myTableName, String[] colFamily) throws IOException {
init();
TableName tableName = TableName .valueOf(myTableName);
if (adm.tableExists(tableName)) {
System.out.println("table is exists!"); }else {
HTableDescriptor htd=new HTableDescriptor(tableName);
for(String str:colFamily) {
HColumnDescriptor hcd =new HColumnDescriptor(str);
htd.addFamily(hcd);
}
adm.createTable(htd);
}
close();
}
public static void close() {
try {
if (adm != null) { adm.close();
}
if (con != null) { con.close();
}
}catch (IOException e) {
e.printStackTrace();}
}
public static void deleteTable(String myTableName) throws IOException {
init();
TableName tableName = TableName .valueOf(myTableName);
if (adm.tableExists(tableName)) {
adm.disableTable(tableName);
adm.deleteTable(tableName);
}
close();
}
public static void listTables() throws IOException {
init();
HTableDescriptor htds[] =adm.listTables();
for(HTableDescriptor htd : htds) {
System.out.println(htd.getNameAsString());
}
close();
}
public static void insertRow(String myTableName, String rowKey, String colFamily, String col, String val) throws IOException {
init();
TableName tableName = TableName .valueOf(myTableName);
@SuppressWarnings("deprecation")
HTable table = new HTable(conf,tableName);
Put put=new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
close();
}
@SuppressWarnings("unused")
private static void deleteRow(String myTableName, String rowKey, String colFamily, String col) throws IOException {
init();
TableName tableName =TableName .valueOf(myTableName);
@SuppressWarnings("deprecation")
HTable table = new HTable(conf, tableName);
Delete delete=new Delete(rowKey.getBytes());
delete.addFamily(Bytes.toBytes(colFamily));
delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
table.delete(delete);
table.close();
close();
}
public static void getData(String myTableName, String rowKey, String colFamily, String col) throws IOException {
init();
TableName tableName = TableName .valueOf(myTableName);
@SuppressWarnings("deprecation")
HTable table = new HTable(conf, tableName);
Get get= new Get(rowKey.getBytes());
Result result = table.get(get);
showCell(result);
table.close();
close();
}
private static void showCell(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("Timetamp:" + cell.getTimestamp() + " ");
System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
}
}
public static void main(String[] args) throws IOException {
String[] cf = {"cf1"};
Hbase.createTable("xyy", cf);
System.out.println("数据表创建成功!!!");
Hbase.insertRow("xyy", "2016001", "cf1", "col", "value");
System.out.println("插入成功!!!");
Hbase.getData("xyy", "2016001", "cf1", "col");
System.out.println("数据查询成功!!!");
Hbase.deleteRow("xyy", "2016001", "cf1", "col");
System.out.println("删除成功!!!");
Hbase.listTables();
System.out.println("操作执行完成!!!");
}
}
打包上传到服务器远行即可