HBase实验

介绍

该文为Hadoop课程的HBase的应用实验

实验题目

Hbase的应用

实验目的

1、掌握Hbase shell操作。

2、掌握Hbase API编程。

实验要求:

用Hbase shell操作 ,调用API创建一个student表,其结构如下表所示

Row Key address score
province city street Java Hadoop Math
zhangsan guangdong guangzhou yinglonglu 85 80 90
lisi guangxi guilin putuolu 87 82 78

查询zhangsan的地址(address)

查询 lisi 的Hadoop成绩。

二、 实验方案

(主要写Hbase shell命令 和 运用API 编写的程序及运行结果,此语句要删除)

1, 创建表student有两个列族address和score

 create'student', {NAME=>'address'}, {NAME=>'score'}

2, 向表中添加数据,

put'student','zhangsan','address:province','guangdon'

put'student', 'zhangsan','address:city','guangzhou'

put'student','zhangsan','address:street','yinglonglu'

put'student','zhangsan','score:Java','85' 

put'student','zhangsan','score:Hadoop','80'

put'student', 'zhangsan','score:Math','90'

put'student', 'lisi','address:province','guangxi'

put'student', 'lisi','address:city','guilin'

put'student','lisi','address:street','putuolu'

put'student','lisi','score:Java','87'

put'student','lisi','score:Hadoop','82'

put'student','lisi','score:Math','78'
  1. 查询“zhangsan”的地址(address)
get'student','zhangsan',’address’

4.查询“lisi”的“Hadoop”成绩

get'student','lisi',’ 'score:Hadoop'’

 

//创建表描述对象

    HTableDescriptor TableDescriptor = new HTableDescriptor(tableName);

   //通过表描述对象,添加列簇

   hTableDescriptor columnDesc1=new HColumnDescriptor("address ");

hTableDescriptor columnDesc1=new HColumnDescriptor("score");

   //通过admin创建表,需要传入表描述对象

   admin.createTable(hTableDescriptor);

   System.out.println("创建表");

 

public void initConnection() {

  try{

   connection = ConnectionFactory.createConnection(config);

  } catch (IOException e) {

   System.out.println("连接数据库");

  }

}

public void put() throws IOException {

  //1. 定义表的名称

  TableName tableName = TableName.valueOf("student");

  

  //2. 获取表对象

  Table table = connection.getTable(tableName);

 

  //3. 准备数据

  String rowkey = "rowkey_zhangsan";

  Put put= new Put(Bytes.toBytes(rowKey));

    put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("province"), Bytes.toBytes("guangdong"));

    put.addColumn(Bytes.toBytes("address "), Bytes.toBytes("city"), Bytes.toBytes("guangzhou"));

    put.addColumn(Bytes.toBytes("address "), Bytes.toBytes("street"), Bytes.toBytes("putuolu"));

    put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Java"), Bytes.toBytes("87"));

put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("Hadoop"), Bytes.toBytes("82"));

put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("Math"), Bytes.toBytes("78"));

 

 

String rowkey = "rowkey_lisi";

  Put put= new Put(Bytes.toBytes(rowKey));

    put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("province"), Bytes.toBytes("guangxi"));

    put.addColumn(Bytes.toBytes("address "), Bytes.toBytes("city"), Bytes.toBytes("guilin"));

    put.addColumn(Bytes.toBytes("address "), Bytes.toBytes("street"), Bytes.toBytes("yinglonglu"));

    put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Java"), Bytes.toBytes("85"));

put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("Hadoop"), Bytes.toBytes("80"));

put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("Math"), Bytes.toBytes("90"));

// 4. 添加数据

    table.put(put);

    table.close();

结论

因吹斯汀

参考资料

HBase Java API、连接HBase、创建表、添加数据put、获取数据get、全表扫描scan 06 : 啊策策

posted @ 2020-07-14 20:55  赤沙咀-菜虚坤  阅读(229)  评论(0编辑  收藏  举报