2、增加Hbase的Maven依赖
想找对应版本的maven https://mvnrepository.com/
pom.xml 文件内容
3、hbase-site.xml 在HBase集群的 目录下下载到本地,放到resources资源目录下:4.0.0 org.example HBaseStudy1.0-SNAPSHOT org.apache.hadoop hadoop-client2.10.1 org.apache.hbase hbase-server2.4.12 org.apache.hbase hbase-client2.4.12 17 17
4、core-site.xml 在/home/bigdata/Opt/hadoop-2.10.1/etc/hadoop 目录下hbase.rootdir hdfs://192.168.232.140:9000/hbase hbase.cluster.distributed true hbase.tmp.dir ./tmp hbase.master.port 16000 hbase.zookeeper.quorum hb1: 2181,hb2: 2181,hb3: 2181 hbase.zookeeper.property.dataDir /home/bigdata/Opt/apache-zookeeper-3.7.0-bin/zkData hbase.unsafe.stream.capability.enforce false
5、hdfs-site.xml 在Hadoop 集群的 /home/bigdata/Opt/hadoop-2.10.1/etc/hadoop 目录下fs.defaultFS hdfs://hb1:9000 hadoop.tmp.dir file:/home/bigdata/Opt/hadoop-2.10.1/tmp
6、修改本地 C:WindowsSystem32driversetc 下的hosts文件dfs.replication 1 dfs.namenode.name.dir file:/home/bigdata/Opt/hadoop-2.10.1/tmp/dfs/name dfs.datanode.data.dir file:/home/bigdata/Opt/hadoop-2.10.1/tmp/dfs/data dfs.namenode.secondary.http-address hb2:50090
在最后增加如下内容:
192.168.209.128 hb1 192.168.209.129 hb2 192.168.209.130 hb3
------------到目前项目文件目录是这样的:------------
7、在该项目下的 src/main/下新建如下内容①先新建 com.demo.hbase 包
②在该包下新建一个 HbaseAPI 类
③ HbaseAPI内容如下:
导包的时候需要注意是哪里的包
package com.demo.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;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class HbaseAPI {
//配置信息
public static Configuration conf;
//获取配置信息
static {
conf = HBaseConfiguration.create();
}
//判断一张表是否存在
public static boolean isExist(String tableName) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
}
//在Hbase集群创建表
public static void createTable(String tableName, String... columnFamily) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
if (isExist(tableName)) {
System.out.println("####表已存在,请输入其他表名");
} else {
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : columnFamily) {
htd.addFamily(new HColumnDescriptor(cf));
}
admin.createTable(htd);
System.out.println("####表已创建成功");
}
}
//删除HBase中的表
public static void deleteTable(String tableName) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
if (isExist(tableName)) {
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
} else {
System.out.println("####表不存在,请重新输入表名");
}
}
//添加数据 put 'user','rowKey'
public static void addRow(String tableName, String rowkey, String cf, String column, String value) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(TableName.valueOf(tableName));
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
Put p = new Put(Bytes.toBytes(rowkey));
p.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value));
t.put(p);
}
//删除表中一行数据
public static void deleteRow(String tableName, String rowkey, String cf) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(TableName.valueOf(tableName));
Delete d = new Delete(Bytes.toBytes(rowkey));
t.delete(d);
}
//删除表中多行数据
public static void deleteAll(String tableName, String... rowkeys) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(TableName.valueOf(tableName));
List list = new ArrayList();
for (String row : rowkeys) {
Delete d = new Delete(Bytes.toBytes(row));
list.add(d);
}
t.delete(list);
}
//扫描全表数据
public static void scanAll(String tableName) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(TableName.valueOf(tableName));
Scan s = new Scan();
ResultScanner rs = t.getScanner(s);
for (Result r : rs) {
Cell[] cells = r.rawCells();
for (Cell c : cells) {
System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c)));
System.out.println("列簇为:" + Bytes.toString(CellUtil.cloneFamily(c)));
System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c)));
}
}
}
//扫描指定数据
public static void getRow(String tableName, String rowkey) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(TableName.valueOf(tableName));
Get g = new Get(Bytes.toBytes(rowkey));
g.addFamily(Bytes.toBytes("info"));
Result rs = t.get(g);
Cell[] cells = rs.rawCells();
for (Cell c : cells) {
System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c)));
System.out.println("列簇为:" + Bytes.toString(CellUtil.cloneFamily(c)));
System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c)));
}
}
public static void main(String[] args) throws IOException{
System.out.println(isExist("emp20"));
createTable("zhaowu","hengao","henshuai");
createTable("zhaowu","info");
deleteTable("zhaowu");
createTable("yangyang","info");
addRow("yangyang","201901","info","age","18");
deleteRow("yangyang","2019001","2019002");
deleteAll("emp","2019001","2019002");
scanAll("yangyang");
getRow("lili","2019102");
}
}



