HBase实验

介绍

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

实验题目

Hbase的应用

实验目的

1、掌握Hbase shell操作。

2、掌握Hbase API编程。

实验要求:

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

Row Keyaddressscore
provincecitystreetJavaHadoopMath
zhangsanguangdongguangzhouyinglonglu858090
lisiguangxiguilinputuolu878278

查询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 : 啊策策

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值