连接到HBase

  1. 创建配置对象并从本地载入配置文件
  2. 使用配置对象作为参数创建连接对象conn
  3. conn.getAdmin()返回管理HBase集群的Admin对象
  4. 此时已连接到HBase
  5. 可以调用Admin对象中的listTableNames()返回TableName[],包含HBase中的所有表
package demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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 java.io.IOException;


public class Main {
	public static void main(String[] args) throws IOException {
		// load configuration and connect to HBase
		Configuration config = HBaseConfiguration.create();
		config.addResource("/hbase-site.xml");
		Connection conn = ConnectionFactory.createConnection(config);
		Admin admin = conn.getAdmin();
		// print all tables
		for (TableName tableName : admin.listTableNames()) {
			System.out.println(tableName.getNameAsString());
		}
		conn.close(); // close connection
	}
}

列出所有表

上面已经演示过了,调用Admin对象中的listTableNames()返回TableName[]

HBaseAPI.lsTbs()返回HBase中的所有表

package demo;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

class HBaseAPI {
	private final Connection conn;
	private final Admin admin;

	public HBaseAPI() throws IOException {
		Configuration conf = HBaseConfiguration.create();
		conf.addResource("hbase-site.xml");
		conn = ConnectionFactory.createConnection(conf);
		admin = conn.getAdmin();
	}

	public TableName[] lsTbs() throws IOException {
		return admin.listTableNames();
	}

	// destructor
	public void close() throws IOException {
		admin.close();
		conn.close();
	}
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		TableName[] tables = hbase.lsTbs();
		for (TableName table : tables) {
			System.out.println(table);
		}
		hbase.close();
	}
}

判断表是否存在

tbExist()调用Admin对象中的tableExists()方法判断表是否存在,返回boolean

package demo;

import ...

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

	public boolean tbExist(String tbName) throws IOException {
        // TableName.valueOf(tbName)将字符串转换为TableName对象
		return admin.tableExists(TableName.valueOf(tbName));
	}

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		System.out.println(hbase.tbExist("scores")); // true
		System.out.println(hbase.tbExist("not_exist")); // false
		hbase.close();
	}
}

获取表和列族的描述

TableDescriptor可打印出表名、所有列族名的描述

ColumnFamilyDescriptor是从TableDescriptor中提取的一部分,可打印出列族名、列的描述

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

	public void tbColFamDesc(String tb, String col) throws IOException {
        // TableName.valueOf(tb)将字符串转换为TableName对象
		TableName tbName = TableName.valueOf(tb);
        // TableDescriptor封装了TableName和列族的描述信息ColumnFamilyDescriptor
		TableDescriptor td = conn.getTable(tbName).getDescriptor();
		System.out.println("Table : " + tb + "\n Desc :" + td + "\n");
        // cfd从td中提取列族描述信息
		ColumnFamilyDescriptor cfd = td.getColumnFamily(col.getBytes());
		System.out.println("Column Family: " + col + "\n Desc :" + cfd);
	}

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		hbase.tbColFamDesc("scores", "grade");
		hbase.close();
	}
}

tb_colfam_desc

新增列族

新建列族之前需要使用列族描述器来创建描述,最后调用Admin对象中的addColumnFamily()方法

import ...
import org.apache.hadoop.hbase.util.Bytes;

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

	public void newCol(String tb, String col) throws IOException {
		// TableName.valueOf(tb)将字符串转换为TableName对象
		TableName tableName = TableName.valueOf(tb);
		// 构造列族描述器, Bytes.toBytes(col)将字符串转换为字节数组
		ColumnFamilyDescriptorBuilder builder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(col));
		// 构造列族描述
		ColumnFamilyDescriptor desc = builder.build();
		// 创建列族使用addColumnFamily(tbn,desc)方法
		admin.addColumnFamily(tableName, desc);
	}

	public void tbColFamDesc(String tb, String col) throws IOException { ... }

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		hbase.newCol("scores", "new_col");
		hbase.tbColFamDesc("scores", "new_col");
		hbase.close();
	}
}

newcol

修改列族属性

  1. 首先构造列族描述器
  2. 使用描述器中的setMaxVersions()设置列族版本
  3. 更改完成后重新构造一个列族描述
  4. 使用admin对象保存更改
class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

	public void modifyCol(String tb, String cf, int new_version) throws IOException {
		TableName tbName = TableName.valueOf(tb);
		// 首先构造列族描述器
		ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(cf.getBytes());
		// 使用描述器中的setMaxVersions()设置列族版本
		cfdb.setMaxVersions(new_version);
		// 更改完成后重新构造一个列族描述
		ColumnFamilyDescriptor cfd = cfdb.build();
		// 并使用admin对象保存更改
		admin.modifyColumnFamily(tbName, cfd);
	}

	public void tbColFamDesc(String tb, String col) throws IOException { ... }

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		hbase.tbColFamDesc("scores", "new_col");
		hbase.modifyCol("scores", "new_col", 6);
		hbase.tbColFamDesc("scores", "new_col");
		hbase.close();
	}
}

modify_col 可以看到版本变成了6

删除列族

删除列族所需的步骤较少,只需要将tb转换成TableName对象,将col名称转换为Bytes数组即可

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

	public void delCol(String tb, String col) throws IOException {
		// 将tb转换为TableName对象
		TableName tbName = TableName.valueOf(tb);
		// 需要将col名称转换为Bytes数组
		admin.deleteColumnFamily(tbName, Bytes.toBytes(col));
	}

	public void tbColFamDesc(String tb, String col) throws IOException { ... }

	// destructor
	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		hbase.tbColFamDesc("scores", "new_col");
		hbase.delCol("scores", "new_col");
		hbase.tbColFamDesc("scores", "new_col");
		hbase.close();
	}
}

del_cf 删除后再次查看new_col的属性可以发现已经变为null

清空表

在对表进行清空操作时需要先禁用该表,否则无法清空

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

	public boolean truncateTb(String tb) throws IOException {
		TableName tableName = TableName.valueOf(tb);
		if (tbExist(tb)) {
			// 清空前需要禁用该表
			admin.disableTable(tableName);
			// 清空表使用admin对象中的truncateTable(tbName)方法
			admin.truncateTable(tableName, true);
			return true;
		}
		return false;
	}
	
	public boolean tbExist(String tbName) throws IOException { ... }

	public void tbColFamDesc(String tb, String col) throws IOException { ... }

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		hbase.tbColFamDesc("scores", "grade");
		if (hbase.truncateTb("scores")) {
			System.out.println("表scores已清空");
		} else {
			System.out.println("表scores不存在");
		}
		hbase.tbColFamDesc("scores", "grade");
		hbase.close();
	}
}

truncate_tb 表中无数据,已清空

删除表

同清空表,删除表之前也需要先禁用该表。

class HBaseAPI {
	....

	public HBaseAPI() throws IOException { ... }

	public boolean deleteTb(String tb) throws IOException {
		TableName tableName = TableName.valueOf(tb);
		if (tbExist(tb)) {
			// 删除前需要禁用该表
			admin.disableTable(tableName);
			// 删除表使用admin对象中的deleteTable(tbName)方法
			admin.deleteTable(tableName);
			return true;
		}
		return false;
	}

	public boolean tbExist(String tbName) throws IOException { ... }

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		System.out.println("scores 是否存在: " + hbase.tbExist("scores"));
		if (hbase.deleteTb("scores")) {
			System.out.println("表scores已删除");
		} else {
			System.out.println("表scores不存在");
		}
		System.out.println("scores 是否存在: " + hbase.tbExist("scores"));
		hbase.close();
	}
}