码迷,mamicode.com
首页 > Windows程序 > 详细

HBase API 操 作

时间:2019-06-13 00:57:32      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:形式   student   settime   writable   ali   module   import   info   long   

6 HBase API

6.1 环境准备

 
   

新建项目后在pom.xml 中添加依赖:

 

6.2 HBaseAPI

6.2.1 

 
   

获取 Configuration 对象

6.2.2 判断表是否存在

 

6.2.3 

 
   

创建表

 

—————————————————————————————

 

 

 

 

 

 

 

 

 

 

 

 

 

6.2.4 

 
   

删除表

6.2.5 向表中插入数据

 

6.2.6 

 
   

删除多行数据

 

 

6.2.7 

 
   

获取所有数据

6.2.8 获取某一行数据

 

public static void getRow(String tableName, String rowKey) throws IOException{

HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(rowKey));

//get.setMaxVersions();显示所有版本

//get.setTimeStamp();显示指定时间戳的版本Result result = table.get(get);

for(Cell cell : result.rawCells()){

System.out.println(" Bytes.toString(result.getRow()));

:"

+

System.out.println("

"

+

Bytes.toString(CellUtil.cloneFamily(cell)));

System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));

System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));

System.out.println("时间戳:" + cell.getTimestamp());

}

}

 

6.2.9 获取某一行指定“列族:”的数据

 

public static void getRowQualifier(String tableName, String rowKey,

String family, String qualifier) throws IOException{

HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(rowKey));

get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); Result result = table.get(get);

for(Cell cell : result.rawCells()){

System.out.println(" Bytes.toString(result.getRow()));

:"

+

System.out.println("

"

+

Bytes.toString(CellUtil.cloneFamily(cell)));

System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));

System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));

}

}

6.3 MapReduce

通过 HBase 的相关 JavaAPI,我们可以实现伴随 HBase 操作的 MapReduce 过程,比如使用MapReduce 将数据从本地文件系统导入到 HBase 的表中,比如我们从 HBase 中读取一些原始数据后使用 MapReduce 做数据分析。

6.3.1 官方 HBase-MapReduce

1. 查看 HBase  MapReduce 任务的执行

 

 

  1. 环境变量的导入

(1) 

 
   

执行环境变量的导入(临时生效,在命令行执行下述操作)

 

(2) 

 
   

永久生效:在/etc/profile 配置

并在 hadoop-env.sh 中配置:(注意:在 for 循环之后配)

export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/opt/module/hbase/lib/*

 

3. 运行官方的 MapReduce 任务

 
   

--  案例一:统计 Student 表中有多少行数据

 

-- 案例二:使用 MapReduce 将本地数据导入到 HBase 1)在本地创建一个 tsv 格式的文件:fruit.tsv

 

—————————————————————————————

 

 

2) 

 
   

创建HBase 表

 

3) 

 
   

在HDFS 中创建 input_fruit 文件夹并上传 fruit.tsv 文件

 

4) 

 
   

执行 MapReduce 到HBase  fruit 表中

 

5) 

 
   

使用 scan 命令查看导入后的结果

6.3.2 自定义HBase-MapReduce1

目标:将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中。分步实现:

1. 构建 ReadFruitMapper 类,用于读取fruit 表中的数据

 

 

—————————————————————————————

 

 

 

 

 

 

 

 

 

 

 

 

  1. 构建 WriteFruitMRReducer 类,用于将读取到的 fruit 表中的数据写入到 fruit_mr

表中

 

  1. 构建 Fruit2FruitMRRunner extends Configured implements Tool 用于组装运行 Job

任务

 

 

—————————————————————————————

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. 主函数中调用运行该 Job 任务

 

  1. 打包运行任务

 

提示:运行任务前,如果待数据导入的表不存在,则需要提前创建。

提示:maven 打包命令:-P local clean package 或-P dev clean package install(将第三方 jar 包一同打包,需要插件:maven-shade-plugin)

6.3.3 自定义HBase-MapReduce2

目标:实现将HDFS 中的数据写入到 HBase 表中。分步实现:

1. 构建 ReadFruitFromHDFSMapper 于读取 HDFS 中的文件数据

 

 

—————————————————————————————

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

 

public class ReadFruitFromHDFSMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {

@Override

protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

// HDFS 中读取的数据

String lineValue = value.toString();

//读取出来的每行数据使用\t 进行分割,存于 String 数组

String[] values = lineValue.split("\t");

 

//根据数据中值的含义取值String rowKey = values[0]; String name = values[1]; String color = values[2];

 

//初始化 rowKey

ImmutableBytesWritable rowKeyWritable = new ImmutableBytesWritable(Bytes.toBytes(rowKey));

 

//初始化 put 对象

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

 

//参数分别:列族、列、值

put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(name));

put.add(Bytes.toBytes("info"), Bytes.toBytes("color"), Bytes.toBytes(color));

 

context.write(rowKeyWritable, put);

}

}

2. 构建 WriteFruitMRFromTxtReducer 类

 

 

  1. 创建Txt2FruitRunner 组装 Job

public int run(String[] args) throws Exception {

//得到 Configuration

Configuration conf = this.getConf();

 

//创建 Job 任务

Job job = Job.getInstance(conf, this.getClass().getSimpleName()); job.setJarByClass(Txt2FruitRunner.class);

Path inPath = new Path("hdfs://hadoop102:9000/input_fruit/fruit.tsv"); FileInputFormat.addInputPath(job, inPath);

 

// Mapper job.setMapperClass(ReadFruitFromHDFSMapper.class); job.setMapOutputKeyClass(ImmutableBytesWritable.class); job.setMapOutputValueClass(Put.class);

 

// Reducer TableMapReduceUtil.initTableReducerJob("fruit_mr", WriteFruitMRFromTxtReducer.class, job);

 

//设置 Reduce 数量,最少 1 个

job.setNumReduceTasks(1);

 

boolean isSuccess = job.waitForCompletion(true); if(!isSuccess){

throw new IOException("Job running with error");

}

 

return isSuccess ? 0 : 1;

}

4. 调用执行 Job

 

  1. 打包运行

 

提示:运行任务前,如果待数据导入的表不存在,则需要提前创建之。

提示:maven 打包命令:-P local clean package 或-P dev clean package install(将第三方 jar 包一同打包,需要插件:maven-shade-plugin)

6.4  Hive 的集成

6.4.1 HBase  Hive 的对比

1. Hive

(1) 数据仓库

Hive 的本质其实就相当于将 HDFS 中已经存储的文件在 Mysql 中做了一个双射关系,以

 

方便使用HQL 去管理查询。

(2) 用于数据分析、清洗

Hive 适用于离线的数据分析和清洗,延迟较高。

(3) 基于HDFS、MapReduce

Hive 存储的数据依旧在DataNode 上,编写的HQL 语句终将是转换为MapReduce 代码执行。

2. HBase

(1) 数据库

是一种面向列存储的非关系型数据库。

(2) 用于存储结构化和非结构化的数据

适用于单表非关系型数据的存储,不适合做关联查询,类似JOIN 等操作。

(3) 基于HDFS

数据持久化存储的体现形式是Hfile,存放于 DataNode 中,被 ResionServer region 的形式进行管理。

(4) 延迟较低,接入在线业务使用

面对大量的企业数据,HBase 可以直线单表大量数据的存储,同时提供了高效的数据访问速度。

6.4.2 HBase  Hive 集成使用

尖叫提示:HBase Hive 的集成在最新的两个版本中无法兼容。所以,我们只能含着泪勇敢的重新编译:hive-hbase-handler-1.2.2.jar!!好气!!

环境准备

因为我们后续可能会在操作 Hive 的同时对 HBase 也会产生影响,所以 Hive 需要持有操作

 

 
   

HBase 的Jar,那么接下来拷贝Hive 所依赖的Jar 包(或者使用软连接的形式)。

同时在 hive-site.xml 中修改 zookeeper 的属性,如下:

<property>

 

 

 

 

 

 

 

 

 

 

 

 

  1. 案例一

目标:建立 Hive 表,关联 HBase 表,插入数据到Hive 表的同时能够影响HBase 表。分步实现:

(1) 

 
   

在Hive 中创建表同时关联HBase

提示:完成之后,可以分别进入 Hive 和HBase 查看,都生成了对应的表

 

(2) 在Hive 中创建临时中间表,用于 load 文件中的数据

 

 
   

提示:不能将数据直接 load 进Hive 所关联 HBase 的那张表中

(3) 向Hive 中间表中 load 数据

 

(4) 通过 insert 命令将中间表中的数据导入到Hive 关联 HBase 的那张表中

 

(5) 查看Hive 以及关联的HBase 表中是否已经成功的同步插入了数据

Hive:

 

 

HBase:

 

 

2. 案例二

目标: HBase 中已经存储了某一张表 hbase_emp_table,然后在 Hive 中创建一个外部表来关联 HBase 中的 hbase_emp_table 这张表,使之可以借助 Hive 来分析 HBase 这张表中的数据。

注:该案例 2 紧跟案例 1 的脚步,所以完成此案例前,请先完成案例 1。分步实现:

(1) 

 
   

在Hive 中创建外部表

(2) 关联后就可以使用Hive 函数进行一些分析操作了

 

HBase API 操 作

标签:形式   student   settime   writable   ali   module   import   info   long   

原文地址:https://www.cnblogs.com/LXL616/p/11013545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!