1.任务:

  • 列出HBase所有的表的相关信息,例如表名;

3. 编程完成以下指定功能(教材P92下):

(1)createTable(String tableName, String[] fields)创建表。

(2)addRecord(String tableName, String row, String[] fields, String[] values)

(3)scanColumn(String tableName, String column)

(4)modifyData(String tableName, String row, String column)

(5)deleteRow(String tableName, String row)

代码如下:

package com.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;

import java.util.ArrayList;
import java.util.List;
class HBaseTest1 {
private static final String TABLE_NAME = "stu";
private static final String FAMILY_NAME = "f1";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_AGE = "age";
private static final String ROW_KEY1 = "r1";
private static final String ROW_KEY2 = "r2";
public static void main(String[] args) throws Exception {
//构造能够访问HBase的configuration对象
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://h2single:9000/hbase");
conf.set("hbase.zookeeper.quorum", "h2sliver113:2181");
//HBaseAdmin是对HBase进行ddl操作的核心类
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
if(!hBaseAdmin.tableExists(TABLE_NAME)){
HTableDescriptor htableDescriptor = new HTableDescriptor(TABLE_NAME);
htableDescriptor.addFamily(new HColumnDescriptor(FAMILY_NAME));
hBaseAdmin.createTable(htableDescriptor);
System.out.println("table create success");
}else{
System.out.println("table exists");
}
//使用HTable可以对HBase的表中的数据进行增删改查
HTable hTable = new HTable(conf, TABLE_NAME);
// 增加数据
List<Put> putList = new ArrayList<Put>();
Put put1 = new Put(ROW_KEY1.getBytes());
put1.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), "zhangsan".getBytes());
put1.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), "23".getBytes());
putList.add(put1);
Put put2 = new Put(ROW_KEY2.getBytes());
put2.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), "lisi".getBytes());
put2.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), "24".getBytes());
putList.add(put2);
hTable.put(putList);
// 根据rowkey得到记录后 获取此记录对应的列信息
Get get = new Get(ROW_KEY1.getBytes());
Result get1 = hTable.get(get);
String name1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));
String age1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));
//System.out.println(get1+"\t"+name1+"\t"+age1);
// 指定行范围来查询多条记录
Scan scan = new Scan();
scan.setStartRow(ROW_KEY1.getBytes());
scan.setStopRow(ROW_KEY2.getBytes());
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
String rowKey = new String(result.getRow());
String name = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));
String age = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));
System.out.println(rowKey+"\t"+name+"\t"+age1);
}
// 根据rowkey删除记录
Delete delete = new Delete(ROW_KEY1.getBytes());
hTable.delete(delete);
// 删除表
hBaseAdmin.disableTable(TABLE_NAME);
hBaseAdmin.deleteTable(TABLE_NAME);
}

Hbase操作与编程使用的更多相关文章

  1. HBase & thrift & C++编程

    目录 目录 1 1. 前言 1 2. 启动和停止thrift2 1 2.1. 启动thrift2 1 2.2. 停止thrift2 1 2.3. 启动参数 2 3. hbase.thrift 2 3. ...

  2. 实验3- 熟悉常用的 HBase 操作

        石家庄铁道大学信息科学与技术学院               实验报告 2018年----2019年  第一学期                       题目:  熟悉常用的 HBase ...

  3. iOS多线程拾贝------操作巨人编程

    iOS多线程拾贝------操作巨人编程 多线程 基本 实现方案:pthread - NSThread - GCD - NSOperation Pthread 多平台,可移植 c语言,要程序员管理生命 ...

  4. HBase操作(Shell与Java API)

    版权声明:本文为博主原创文章,未经博主允许不得转载.     转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...

  5. Oracle数据迁移至HBase操作记录

    Oracle数据迁移至HBase操作记录 @(HBase) 近期需要把Oracle数据库中的十几张表T级别的数据迁移至HBase中,过程中遇到了许多苦难和疑惑,在此记录一下希望能帮到一些有同样需求的兄 ...

  6. Storm实时计算:流操作入门编程实践

    转自:http://shiyanjun.cn/archives/977.html Storm实时计算:流操作入门编程实践   Storm是一个分布式是实时计算系统,它设计了一种对流和计算的抽象,概念比 ...

  7. 常用HBase操作

    HBase是一个分布式.面向列的数据库,可以用来存储非结构化和半结构化的松散数据,具有高可靠.高性能.面向列.可伸缩的特性.通过行键(RowKey).列族(ColumnFamily).列(Column ...

  8. hbase操作(shell 命令,如建表,清空表,增删改查)以及 hbase表存储结构和原理

    两篇讲的不错文章 http://www.cnblogs.com/nexiyi/p/hbase_shell.html http://blog.csdn.net/u010967382/article/de ...

  9. HBase篇--HBase操作Api和Java操作Hbase相关Api

    一.前述. Hbase shell启动命令窗口,然后再Hbase shell中对应的api命令如下. 二.说明 Hbase shell中删除键是空格+Ctrl键. 三.代码 1.封装所有的API pa ...

  10. 熟悉常用的HBase操作,编写MapReduce作业

    1. 以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据: 学生表(Student) 学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S_Age) 201 ...

随机推荐

  1. SQL Server性能优化工具Profiler

    SQL Server Profiler是什么 SQL Server Profiler是一个界面,用于创建和管理跟踪并分析和重播跟踪结果. 这些事件保存在一个跟踪文件中,稍后试图诊断问题时,可以对该文件 ...

  2. (一)从路由器和IP地址开始折腾

    我们应当知道的一点是,由于IP地址只有32bit, 所以很快就面临着不够用的情况,现在之所以大家还在正常使用IPv4, 就是因为采用了公有地址和私有地址的概念:所谓的私有地址是从当时公有地址中还没有分 ...

  3. C# RGB转Brush

    C#中自定义一个Brush,使用Color赋RGB值给Brush: dataGrid2.HorizontalGridLinesBrush = new SolidColorBrush(System.Wi ...

  4. AVL tree rotate

    AVL tree single rotate /** * Rotate binary tree node with left child. * For AVL trees, this is a sin ...

  5. 面试官:MySQL一千万数据,怎么快速查询?

    前言 面试官:来说说,一千万的数据,你是怎么查询的? me:直接分页查询,使用limit分页. 面试官:有实操过吗? me:肯定有呀 此刻献上一首<凉凉> 也许有些人没遇过上千万数据量的表 ...

  6. centos7升级内核 ,wireguard优化

    一.centos7升级内核 uname -r 查看内核版本 升级前 升级后 参考链接: https://www.cnblogs.com/rick-zhang/p/14944510.html # 启用 ...

  7. WPF 轨迹动画

    1.后台 public MainWindow() { InitializeComponent(); /// <summary> /// Window2.xaml 的交互逻辑 /// < ...

  8. 打包exe

    2.要打包的文件为多个py文件 这种情况一般你的代码较多,项目较大,可能你写了一个GUI界面py文件这个文件调用了其他文件的函数什么的,这个时候你需要生成spec文件来打包,这里假设你的要打包的主文件 ...

  9. Json数组转List

    List<Person> list = obj.list.ToObject<List<Person>>()

  10. kubectl 补全报错:-bash: _get_comp_words_by_ref: command not found

    1.kubectl自动补全设置 yum -y install bash-completion source <(kubectl completion bash) # 在 bash 中设置当前 s ...