HBase安装配置
①下载压缩包
官网下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/stable/
选择稳定版hbase-1.4.9-bin.tar.gz,在Windows里面下载。
②将压缩包从Windows传输到Linux当前目录下或也可以通过以下命令从官网下载Hbase:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/stable/hbase-1.4.9-bin.tar.gz
③解压:
tar -zxvf hbase-1.4.9-bin.tar.gz -C /root/Hbase/
④配置环境变量:
vi /etc/profile
export HBASE_HOME=/root/Hbase/hbase-1.4.9
export PATH=$HBASE_HOME/bin:$PATH
⑤测试HBase安装成功:
hbase
HBase配置(伪分布式模式)
配置文件位于HBase安装路径的conf目录(/opt/module/hbase/conf)下面
①配置 vi hbase-env.sh
设置Java安装路径
export JAVA_HOME=/usr/local/java/jdk1.8.0_202
设置HBase的配置文件路径(/opt/module/hbase/conf)
export HBASE_CLASSPATH=/opt/module/hbase/conf
采用HBase自带Zookeeper,设置参数true
export HBASE_MANAGES_ZK=true
②配置 vi hbase-site.xml
<!--hbase共享目录,持久化hbase数据-->
<!--配置为core-site.xml 中的fs.defaultFS -->
<property>
<name>hbase.rootdir</name>
<value>hdfs://bigdata128:9000/hbase</value>
</property>
<!--分布式运行模式,false(默认)为单机模式-->
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<!--Zookeeper集群的地址列表,伪分布式用默认localhost-->
<property>
<name>hbase.zookeeper.quorum</name>
<value>localhost</value>
</property>
③启动并运行HBase(之前启动Hadoop)
启动HBase,并jps查看
用完停止HBase运行(之后停止Hadoop)
stop-hbase.sh
java程序
package hdfs.file;
import java.io.IOException;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.util.Bytes;
public class HbaseTables {
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://47.106.78.4: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"};
HbaseTables.createTable("stu1", cf);
System.out.println("createTable success!!!");
HbaseTables.insertRow("stu1", "2016001", "cf1", "col", "val");
System.out.println("insertRow success!!!");
HbaseTables.getData("stu1", "2016001", "cf1", "col");
System.out.println("getData success!!!");
HbaseTables.deleteRow("stu1", "2016001", "cf1", "col");
System.out.println("deleteRow success!!!");
HbaseTables.listTables();
System.out.println("listTables success!!!");
}
}
在Eclipse中打成jar包,传到Linux下
运行jar包,访问hbase端口16010,可以看到结果。
也可以用命令进入HBase数据库
进入HBase的shell命令行模式
hbase shell
①创建表
create 'stu', 'name', 'age', 'bigdata'
②添加数据
put 'stu', '2016001', 'name', 'mark'
③查看数据
get 'stu', '2016001'
scan 'stu'
④删除数据
删除一个单元格
delete 'stu', '2016001', 'age'
删除一行
deleteall 'stu', '2016001'
⑤删除表
disable 'stu'
drop 'stu'