Hbase调用JavaAPI实现批量导入操作
将手机上网日志文件批量导入到Hbase中。操作步骤:
1、将日志文件(请下载附件)上传到HDFS中,利用hadoop的操作命令上传:hadoop fs -put input /
2、创建Hbase表,通过Java操作
- package com.jiewen.hbase;
- import java.io.IOException;
- 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.Get;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- import org.apache.hadoop.hbase.client.HTable;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.client.Result;
- import org.apache.hadoop.hbase.client.ResultScanner;
- import org.apache.hadoop.hbase.client.Scan;
- import org.apache.hadoop.hbase.util.Bytes;
- public class HbaseDemo {
- public static void main(String[] args) throws IOException {
- String tableName = "wlan_log";
- String columnFamily = "cf";
- HbaseDemo.create(tableName, columnFamily);
- // HbaseDemo.put(tableName, "row1", columnFamily, "cl1", "data");
- // HbaseDemo.get(tableName, "row1");
- // HbaseDemo.scan(tableName);
- // HbaseDemo.delete(tableName);
- }
- // hbase操作必备
- private static Configuration getConfiguration() {
- Configuration conf = HBaseConfiguration.create();
- conf.set("hbase.rootdir", "hdfs://hadoop1:9000/hbase");
- // 使用eclipse时必须加入这个,否则无法定位
- conf.set("hbase.zookeeper.quorum", "hadoop1");
- return conf;
- }
- // 创建一张表
- public static void create(String tableName, String columnFamily)
- throws IOException {
- HBaseAdmin admin = new HBaseAdmin(getConfiguration());
- if (admin.tableExists(tableName)) {
- System.out.println("table exists!");
- } else {
- HTableDescriptor tableDesc = new HTableDescriptor(tableName);
- tableDesc.addFamily(new HColumnDescriptor(columnFamily));
- admin.createTable(tableDesc);
- System.out.println("create table success!");
- }
- }
- // 加入一条记录
- public static void put(String tableName, String row, String columnFamily,
- String column, String data) throws IOException {
- HTable table = new HTable(getConfiguration(), tableName);
- Put p1 = new Put(Bytes.toBytes(row));
- p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes
- .toBytes(data));
- table.put(p1);
- System.out.println("put'" + row + "'," + columnFamily + ":" + column
- + "','" + data + "'");
- }
- // 读取一条记录
- public static void get(String tableName, String row) throws IOException {
- HTable table = new HTable(getConfiguration(), tableName);
- Get get = new Get(Bytes.toBytes(row));
- Result result = table.get(get);
- System.out.println("Get: " + result);
- }
- // 显示全部数据
- public static void scan(String tableName) throws IOException {
- HTable table = new HTable(getConfiguration(), tableName);
- Scan scan = new Scan();
- ResultScanner scanner = table.getScanner(scan);
- for (Result result : scanner) {
- System.out.println("Scan: " + result);
- }
- }
- // 删除表
- public static void delete(String tableName) throws IOException {
- HBaseAdmin admin = new HBaseAdmin(getConfiguration());
- if (admin.tableExists(tableName)) {
- try {
- admin.disableTable(tableName);
- admin.deleteTable(tableName);
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("Delete " + tableName + " 失败");
- }
- }
- System.out.println("Delete " + tableName + " 成功");
- }
- }
3、将日志文件导入Hbase表wlan_log中:
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
- import org.apache.hadoop.hbase.mapreduce.TableReducer;
- import org.apache.hadoop.hbase.util.Bytes;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.NullWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Counter;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- public class HbaseBatchImport {
- public static void main(String[] args) throws Exception {
- final Configuration configuration = new Configuration();
- // 设置zookeeper
- configuration.set("hbase.zookeeper.quorum", "hadoop1");
- // 设置hbase表名称
- configuration.set(TableOutputFormat.OUTPUT_TABLE, "wlan_log");
- // 将该值改大,防止hbase超时退出
- configuration.set("dfs.socket.timeout", "180000");
- final Job job = new Job(configuration, "HBaseBatchImport");
- job.setMapperClass(BatchImportMapper.class);
- job.setReducerClass(BatchImportReducer.class);
- // 设置map的输出,不设置reduce的输出类型
- job.setMapOutputKeyClass(LongWritable.class);
- job.setMapOutputValueClass(Text.class);
- job.setInputFormatClass(TextInputFormat.class);
- // 不再设置输出路径。而是设置输出格式类型
- job.setOutputFormatClass(TableOutputFormat.class);
- FileInputFormat.setInputPaths(job, "hdfs://hadoop1:9000/input");
- job.waitForCompletion(true);
- }
- static class BatchImportMapper extends
- Mapper<LongWritable, Text, LongWritable, Text> {
- SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyyMMddHHmmss");
- Text v2 = new Text();
- protected void map(LongWritable key, Text value, Context context)
- throws java.io.IOException, InterruptedException {
- final String[] splited = value.toString().split("\t");
- try {
- final Date date = new Date(Long.parseLong(splited[0].trim()));
- final String dateFormat = dateformat1.format(date);
- String rowKey = splited[1] + ":" + dateFormat;
- v2.set(rowKey + "\t" + value.toString());
- context.write(key, v2);
- } catch (NumberFormatException e) {
- final Counter counter = context.getCounter("BatchImport",
- "ErrorFormat");
- counter.increment(1L);
- System.out.println("出错了" + splited[0] + " " + e.getMessage());
- }
- };
- }
- static class BatchImportReducer extends
- TableReducer<LongWritable, Text, NullWritable> {
- protected void reduce(LongWritable key,
- java.lang.Iterable<Text> values, Context context)
- throws java.io.IOException, InterruptedException {
- for (Text text : values) {
- final String[] splited = text.toString().split("\t");
- final Put put = new Put(Bytes.toBytes(splited[0]));
- put.add(Bytes.toBytes("cf"), Bytes.toBytes("date"), Bytes
- .toBytes(splited[1]));
- // 省略其它字段,调用put.add(....)就可以
- context.write(NullWritable.get(), put);
- }
- };
- }
- }
4、查看导入结果:
Hbase调用JavaAPI实现批量导入操作的更多相关文章
- 批量导入数据到HBase
hbase一般用于大数据的批量分析,所以在很多情况下需要将大量数据从外部导入到hbase中,hbase提供了一种导入数据的方式,主要用于批量导入大量数据,即importtsv工具,用法如下: Us ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:hbase的javaAPI应用
实验目的 进一步了解hbase的操作 熟悉使用IDEA进行java开发 熟悉hbase的javaAPI 实验原理 前面已经了解通过hbase的shell操作hbase,确实比较难以使用,另外通过hiv ...
- python操作数据库之批量导入
python操作数据库之批量导入 Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进. Python具有丰富和强大的库.它常被昵称为胶水语言,能够把用其他语言制作的 ...
- HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)
HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ...
- Hbase笔记:批量导入
工作中可能会有对HBase的复杂操作,我们现在对HBase的操作太简单了.复杂操作一般用HBaseScan操作,还有用框架对HBase进行复杂操作,iparler,sharker.我们说HBase是数 ...
- ADO.NET 对数据操作 以及如何通过C# 事务批量导入数据
ADO.NET 对数据操作 以及如何通过C# 事务批量导入数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- Hadoop之——HBASE结合MapReduce批量导入数据
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46463889 废话不多说.直接上代码,你懂得 package hbase; imp ...
- Spark:DataFrame批量导入Hbase的两种方式(HFile、Hive)
Spark处理后的结果数据resultDataFrame可以有多种存储介质,比较常见是存储为文件.关系型数据库,非关系行数据库. 各种方式有各自的特点,对于海量数据而言,如果想要达到实时查询的目的,使 ...
- 数据批量导入HBase
测试数据: datas 1001 lilei 17 13800001111 1002 lily 16 13800001112 1003 lucy 16 13800001113 1004 meimei ...
随机推荐
- (35)zabbix Event acknowledgment事件确认
概述 以往服务器出现报警,运维人员处理完事之后,报警自动取消,但是下一次出现同样一个错误,但是换了一个运维人员,他可能需要重新排查问题,直到问题处理完毕. 针对这种情况,zabbix提供了event ...
- DateFormat的format()方法线程不安全的问题分析
最近看到<侦探剧场:堆内存神秘溢出事件>https://my.oschina.net/u/2368090/blog/1628720,于是自己也想测试了解一下DateFormat的多线程安全 ...
- laravel中的路由
相信玩过laravel框架的小伙伴们,都知道它路由的强大之处 今天我想给大家分析下这个 首先 要找到配置路由的位置 routes这个目录下,我们找到web.php文件 里面可以看到现成的一个路由 Ro ...
- python基础知识13-迭代器与生成器,导入模块
异常处理作业讲解 file = open('/home/pyvip/aaa.txt','w+') try: my_dict = {'name':'adb'} file.write(my_dict['a ...
- LeetCode(108) Convert Sorted Array to Binary Search Tree
题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...
- 强制停止及删除(卸载)Windows服务
1. 安装服务: CMD 打开命令行窗口:C:\> 运行:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe& ...
- 解决img标签上下出现间隙的方法
图片与父元素下边缘有 2px 的间隙,并不是因为空格.多个 inline-block 元素之间的间隙才是因为空格. 任何不是块级元素的可见元素都是内联元素,其表现的特性是“行布局”形式.----< ...
- 三、harbor部署之SSL
1 签名证书与自签名证书 签名证书:由权威颁发机构颁发给服务器或者个人用于证明自己身份的东西. 自签名证书:由服务器自己颁发给自己,用于证明自己身份的东西,非权威颁发机构发布. 2 openssl简介 ...
- python jieba包用法总结
# coding: utf-8 # ###jieba特性介绍 # 支持三种分词模式: # 精确模式,试图将句子最精确地切开,适合文本分析: # 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非 ...
- [android 应用开发]android 分层
1 应用层, 2 应用框架层(框架是所有开发人员共同使用和遵守的约定) 3 系统运行库层 4 linux内核层