HBase Shell API 操作

创建工程

本实验的环境实在ubuntu18.04下完成,首先在改虚拟机中安装开发工具eclipse。

然后创建Java项目名字叫hbase-test


配置运行环境

在src下创建HBaseDemo类

然后编写init方法和close方法,一个创建与HBASE的连接,一个关闭连接。

/**
* 创建连接返回admin
*/
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "file:///usr/local/hbase/hbase-tmp");
// configuration.set("hbase.zookeeper.quorum", "hadoop02");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 连接关闭
*/
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

建表操作

	/**
* 创建表方法
*
* @param myTableName
* @param colFamily
* @throws IOException
*/
public static void createTable(String myTableName, String[] colFamily) throws IOException {
TableName tableName = TableName.valueOf(myTableName);
if (admin.tableExists(tableName)) {
System.out.println("talbe is exists!");
} else {
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
for (String str : colFamily) {
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
tableDescriptor.setColumnFamily(family);
}
admin.createTable(tableDescriptor.build());
}
}

测试:创建student表,列族有score

	public static void main(String[] args) throws IOException {
init();
System.out.print("==================分割线===================");
//创建student表
createTable("student",new String[]{"score"}); close();
}

查看执行结果

查看现有表的名称

	 /**
* 查看已有表
* @throws IOException
*/
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}

测试:

public static void main(String[] args) throws IOException {
init();
System.out.println("==================分割线===================");
listTables();
System.out.println("==================分割线===================");
close();
}

删除表操作

代码:

	/**
* 删除指定表
* @param tableName 表名
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}

测试:

public static void main(String[] args) throws IOException {
init();
System.out.println("==================分割线===================");
deleteTable("student");
System.out.println("==================分割线===================");
close();
}

结果

删除指定列操作

代码

    /**
* 删除指定列数据
* @param tableName 表名
* @param rowKey 行键
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
//删除指定列族的所有数据
//delete.addFamily(colFamily.getBytes());
//删除指定列的数据
delete.addColumn(colFamily.getBytes(), col.getBytes()); table.delete(delete);
table.close();
close();
}

测试

public static void main(String[] args) throws IOException {
init();
System.out.println("==================分割线===================");
deleteRow("student", "zhangsan", "score", "Math");
System.out.println("==================分割线===================");
close();
}

结果 zhangsan行,score列族中的Math列被删除

添加数据

	/**
* 向指定表中插入数据
*
* @param tableName
* @param rowKey
* @param colFamily
* @param col
* @param val
* @throws IOException
*/
public static void insertData(String tableName, String rowKey, String colFamily, String col, String val)
throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
}

向student表中添加三条数据,分别是zhangsan的英语成绩,数学成绩和计算机成绩。

	public static void main(String[] args) throws IOException {
init();
System.out.print("==================分割线==================="); //插入三条数据
insertData("student","zhangsan","score","English","69");
insertData("student","zhangsan","score","Math","86");
insertData("student","zhangsan","score","Computer","77"); close();
}

测试结果

查看数据

/**
* 获取指定表中ceil数据
*
* @param tableName
* @param rowKey
* @param colFamily
* @param col
* @throws IOException
*/
public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(), col.getBytes());
Result result = table.get(get);
System.out.println(tableName+"表,"+colFamily+"列族,"+col+"的值是:"+new String(result.getValue(colFamily.getBytes(), col == null ? null : col.getBytes())));
table.close();
}

查看张三英语的 成绩

	public static void main(String[] args) throws IOException {
init();
System.out.println("==================分割线==================="); getData("student","zhangsan", "score","English"); close();
}

控制台输出结果:

最新文章

  1. 使用struct处理二进制
  2. Java语言的安全性的体现
  3. Struts2 之 对xwork的理解
  4. Visual Studio 2013启用AnkSVN
  5. GT 940M 到底怎么样! 768的可以 1080的不要用了
  6. ZT “樱花小萝莉”走红网络 网友:好想生个女儿
  7. springmvc之interceptor(拦截器)
  8. 第六十八篇、OC_按照某一字段对数值进行排序
  9. Jquery花园
  10. mysql实现高可用架构之MHA
  11. C# Log4net记录日志
  12. typedef 使用
  13. iOS开发之四:常用控件--UIButton的使用
  14. Netty(三) 什么是 TCP 拆、粘包?如何解决?
  15. 初识Python,简单初学代码
  16. vs2017 创建虚拟目录失败 http//locXXXXXX,vs2015 无法访问IIS源数据
  17. 页面商城总结(一)——HTML部分
  18. flutter 控制台快捷键
  19. cordova app 监听物理返回键
  20. 【PAT】1052 Linked List Sorting (25)(25 分)

热门文章

  1. 【题单】最近遇见的 SHIT DP题 三连
  2. sql 语句使用和转换json数据
  3. JavaSE12-内部类&API
  4. Go语言(1)——程序结构
  5. js下 Day13、面向对象
  6. Spring Data JPA 整合Spring
  7. matplotlib的学习1-为什么学他
  8. vue API 知识点(3) --- 实例 总结
  9. Windows 上安装 PostgreSQL
  10. WPF应用程序管理