实验四 NoMysql和关系数据库操作 HBase篇(2)


实验环境:

1、操作系统:Windows10

2Hadoop版本:3.2.2

4HBase版本:2.7.2

7JDK版本:1.8或以上版本;

8Java IDEidea

(二)HBase数据库操作

学生表Student

     name

score

English

Math

Computer

zhangsan

69

86

77

lisi

55

100

88

根据上面给出的学生表Student的信息执行如下操作:

(1)Hbase Shell命令创建学生Student

create "bigdata:Student",{NAME => 'name'},{NAME => 'score'}

put "bigdata:Student","rk001","name:name","zhansan"
put "bigdata:Student","rk002","name:name","lisi"
put "bigdata:Student","rk001","score:English","69"
put "bigdata:Student","rk001","score:Math","86"
put "bigdata:Student","rk001","score:Computer","77"
put "bigdata:Student","rk002","score:English","55"
put "bigdata:Student","rk002","score:Math","100"
put "bigdata:Student","rk002","score:Computer","88"

 (2)用scan命令浏览Student表的相关信息

scan "bigdata:Student"

 (3)查询zhangsan的Computer成绩

get 'bigdata:Student','rk001','score:Computer'

4)修改lisi的Math成绩,95

put "bigdata:Student",'rk002','score:Math','95'
get "
bigdata:Student",'rk002','name','score'

2.根据上面已经设计出的Student表用HBase API编程实现以下操作:

1)添加数据:English:45  Math:89 Computer:100

scofield

45

89

100

package com.yc.hadoop.hbase;
 
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
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 org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
 
public class HbaseAPI03 {
    public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "slave01,slave02,slave03");
        ExecutorService pool = Executors.newCachedThreadPool();
        Connection con = ConnectionFactory.createConnection(conf, pool);
        Admin admin = con.getAdmin();  //管理者
 
        TableName tn = TableName.valueOf("Student");  //表对象
        if(admin.tableExists(tn)){
            Table t = con.getTable(tn,pool);
 
            //添加数据
            Put put = new Put(Bytes.toBytes("rk003"));  //根据rowkey(行键)值创建添加数据对象
            put.addColumn(Bytes.toBytes("name"), Bytes.toBytes("name"), Bytes.toBytes("scofield")).
            addColumn(Bytes.toBytes("score"), Bytes.toBytes("English"), Bytes.toBytes("45")).
            addColumn(Bytes.toBytes("score"), Bytes.toBytes("Math"), Bytes.toBytes("89")).
            addColumn(Bytes.toBytes("score"), Bytes.toBytes("Computer"), Bytes.toBytes("100"));
            t.put(put);   //写入数据是put
            System.out.println("添加数据成功。。。。");
 
 
        }
        admin.close();
        con.close();
        pool.shutdown();
    }
}

2获取scofieldEnglish成绩信息

public static void getRow(String tableMame,String rowKey,String colFamily,String col) throws IOException{
Table table = connection.getTable(TableName.value0f(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes();
Result rs = table.get(get);
System.out.println(newString(rs.getValue(colFamily.getBytes(), , col==null?null:col.getBytes())));table.close();
}

相关