一、创建maven工程,配置pom.xml
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<hadoop.version>2.7.6</hadoop.version>
<hbase.version>1.4.6</hbase.version>
<!-- 根据自己的hadoop和hbase版本配置 -->
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.4.6</version>
</dependency>
</dependencies>
配置完刷新下载就不会报红!
二、DDL
前期准备
private static Connection connection=null;
private static Admin admin = null;
//关于表的增删改查都要用到admin,所以把这部分代码弄成静态代码块
static{
try {
//1、获取配置文件信息
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","虚拟机的ip");
conf.set("hbase.zookeeper.property.clientPort","端口号");
//如果我不加hbase会报错。。不知道为什么,看有些人是可以的
//2、创建连接对象
connection = ConnectionFactory.createConnection(conf);
//3、创建管理员
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
关闭资源
//关闭资源方法,在最后调用
public static void close() throws IOException {
if(admin != null){
admin.close();
}
if(connection != null){
connection.close();
}
}
1、判断表是否存在
public static boolean isTableExists(String tableName) throws IOException {
boolean exists = admin.tableExists(TableName.valueOf(tableName));
return exists;
}
2、创建表
public static void createTable(String tableName,String ...cfs) throws IOException {
//1、判断有无列族信息
if(cfs.length<=0){
System.out.println("请添加列族信息");
return;
}
//2、判断要创建的表是否已经存在
if(isTableExists(tableName)){
System.out.println("该表已经存在");
return;
}
//3、创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//4、循环添加列族信息
for (String cf : cfs) {
//5、创建列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//6、添加具体的列族信息
hTableDescriptor.addFamily(hColumnDescriptor);
}
//7、创建表
admin.createTable(hTableDescriptor);
}
3、删除表
public static void dropTable(String tableName) throws IOException {
//1、判断表是否存在
if(!isTableExists(tableName)){
System.out.println(tableName+"不存在");
}
//2、使表禁用
admin.disableTable(TableName.valueOf(tableName));
//3、删除表
admin.deleteTable(TableName.valueOf(tableName));
}
4、创建命名空间
public static void createNameSpace(String nameSpace){
//1、获得命名空间描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(nameSpace).build();
try {
//2、创建命名空间
admin.createNamespace(namespaceDescriptor);
} catch(NamespaceExistException e){
System.out.println(nameSpace+"命名空间已经存在");
//抓异常NamespaceExistException
//当命名空间已经存在的条件下,再次创建不会报错,不会影响后序程序的运行
}
catch (IOException e) {
e.printStackTrace();
}
}
5、在指定命名空间创建表
creatTable("20220409:stu","info1","info2");
二、DML
1、put增加数据
public static void putData(String tableName,String rowKey,String cf,String cn,String value) throws IOException {
//1、连接表对象
Table table = connection.getTable(TableName.valueOf(tableName));
//2、创建put对象
Put put = new Put(Bytes.toBytes(rowKey));
//3、为put赋值
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn),Bytes.toBytes(value));
//对同一个rowKey添加数据====>为put赋值多次
put.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("sex"),Bytes.toBytes("female"));
put.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("phone"),Bytes.toBytes("13512341234"));
//添加不同rowKey的值======>
//①创建多个put对象 ②将put对象放在一个集合list ③table.put(list<>)
//4、给表put数据
table.put(put);
//5、关闭表
//connection为重处理,不能到左后再关表
table.close();
}
批量导入本机数据student.txt
public static void putAllData(String tableName,String dataPath) throws IOException {
//2001,zhangsan,0
//2002,lisi,1
//2003,wangwu,1
//格式 学号,名字,性别
Table table = connection.getTable(TableName.valueOf(tableName));
BufferedReader bf = new BufferedReader(new FileReader(dataPath));
//dataPath为该txt文件在本机的路径
String line = null;
while((line = bf.readLine() )!= null){
String[] split = line.split(",");
Put put = new Put(Bytes.toBytes(split[0]));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(split[1]));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes(split[2]));
table.put(put);
}
table.close();
}
2、 get获取数据
//获取表数据 至少要有两个参数 tableName,rowKey
//测试一:两个参数tableName,rowKey
public static void getData(String tableName,String rowKey) throws IOException {
//1、连接table表
Table table = connection.getTable(TableName.valueOf(tableName));
//2、创建get对象
Get get = new Get(Bytes.toBytes(rowKey));
//3、获取rowKey对应数据
Result result = table.get(get);
//4、解析result
for (Cell cell : result.rawCells()) {
//5、打印输出各部分
System.out.println("rowKey="+Bytes.toString(CellUtil.cloneRow(cell))
+",列族="+Bytes.toString(CellUtil.cloneFamily(cell))
+",列名="+ Bytes.toString(CellUtil.cloneQualifier(cell))
+",value="+Bytes.toString(CellUtil.cloneValue(cell)));
}
//6、关闭表
table.close();
}
//测试二:三个参数 tableName,rowKey,cf
public static void getData(String tableName,String rowKey,String cf) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
//指定获取的列族
get.addFamily(Bytes.toBytes(cf));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("rowKey="+Bytes.toString(CellUtil.cloneRow(cell))
+",列族="+Bytes.toString(CellUtil.cloneFamily(cell))
+",列名="+ Bytes.toString(CellUtil.cloneQualifier(cell))
+",value="+Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
//测试三:四个参数 tableName,rowKey,cf,cn
public static void getData(String tableName,String rowKey,String cf,String cn) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
//指定要获取的列族和列,获取列必须要获取列族,所以有两个参数
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("rowKey="+Bytes.toString(CellUtil.cloneRow(cell))
+",列族="+Bytes.toString(CellUtil.cloneFamily(cell))
+",列名="+ Bytes.toString(CellUtil.cloneQualifier(cell))
+",value="+Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
3、get获取数据时指定版本数
public static void getData(String tableName,String rowKey,String cf,String cn) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
//设置获取数据的版本数
get.setMaxVersions(3);
//如果创建表时没有指定版本数,则默认版本数为1
//在这里不管get.setMaxVersions(n)的n为多大,dou只能输出一条(最新)数据
//所以要在shell修改版本数 alter 'stu',{NAME=>'info1',VERSIONS=>num}
//这样就可以输出多条数据
//1、n>num输出num条
//2、n<num输出n条
//3、前提是修改了num次数据
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("rowKey="+Bytes.toString(CellUtil.cloneRow(cell))
+",列族="+Bytes.toString(CellUtil.cloneFamily(cell))
+",列名="+ Bytes.toString(CellUtil.cloneQualifier(cell))
+",value="+Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
4、scan扫描数据
public static void scanData(String tableName) throws IOException {
//1、获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
//2、创建Scan对象
Scan scan = new Scan();
//Scan scan = new Scan(Bytes.toBytes("1001"),Bytes.toBytes("1003"));
//左闭右开,只显示1001和1002的数据
//3、扫描表
ResultScanner scanner = table.getScanner(scan);
//4、解析resultScanner
for (Result result : scanner) {
//5、解析result并打印
for (Cell cell : result.rawCells()) {
System.out.println("rowKey="+Bytes.toString(CellUtil.cloneRow(cell))
+",列族="+Bytes.toString(CellUtil.cloneFamily(cell))
+",列名="+ Bytes.toString(CellUtil.cloneQualifier(cell))
+",value="+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
//6、关闭表连接
table.close();
}
5、delete删除数据
public static void deleteData(String tableName,String rowKey,String cf,String cn) throws IOException {
//1、获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
//2、创建delete对象
Delete delete = new Delete(Bytes.toBytes(rowKey));
//只传rowKey相当于命令行的deleteAll
//标记:DeleteFamily 删除所有版本
//2.1指定到列族的删除(只会把这个列族的数据都删除)
// delete.addFamily(Bytes.toBytes(cf));
//标记:DeleteFamily 删除所有版本
//2.2指定到列的删除
//2.2.1 delete.addColumn( , )
//delete.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
//删除这个列的最新时间戳版本
//删除了最新的一条数据,scan后会显示前一条被覆盖的数据
//标记:Delete (如果指定时间戳,就只删除这个时间戳的版本
//2.2.2 delete.addColumns( , )
//delete.addColumns(Bytes.toBytes(cf),Bytes.toBytes(cn));
//删除这个列的所有时间戳版本
//标记:DeleteColumn (如果加了时间戳,就删除小于等于这个时间戳的所有版本
//3、执行删除操作
table.delete(delete);
//4、关闭表连接
table.close();
}