hbase数据导入
hbase数据导入:
参考http://blog.csdn.net/hua840812/article/details/7414875,在把代码copy下来后,发现运行总是报错:
java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.hbase.io.ImmutableBytesWritable, recieved org.apache.hadoop.io.LongWritable;
原因是map的输出必须按照现有的版本来写,也就是extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>
要这样写,不能简单的写成extends Mapper,
代码还是贴出来:
生成hfile的代码:
package com.bonc.db; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat;
import org.apache.hadoop.hbase.mapreduce.KeyValueSortReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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.output.FileOutputFormat; public class TestHFileToHBase { public static class TestHFileToHBaseMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> { protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] values = value.toString().split("\\|");
ImmutableBytesWritable rowkey = new ImmutableBytesWritable(
values[].toString().trim().getBytes());
KeyValue kvProtocol;
if (values.length>){
kvProtocol = new KeyValue(values[].toString().trim().getBytes(), "url_type".getBytes(), "type".getBytes(),System.currentTimeMillis(), values[].toString().trim()
.getBytes());
}else{
kvProtocol=new KeyValue(values[].toString().trim().getBytes(), "url_type".getBytes(), "type".getBytes(),System.currentTimeMillis(), "NULL".getBytes());
}
context.write(rowkey, kvProtocol);
// KeyValue kvSrcip = new KeyValue(row, "SRCIP".getBytes(),
// "SRCIP".getBytes(), values[1].getBytes());
// context.write(k, kvSrcip);
// HFileOutputFormat.getRecordWriter
} } public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = HBaseConfiguration.create();
Job job = new Job(conf, "TestHFileToHBase");
job.setJarByClass(TestHFileToHBase.class); job.setOutputKeyClass(ImmutableBytesWritable.class);
job.setOutputValueClass(KeyValue.class); job.setMapperClass(TestHFileToHBaseMapper.class);
job.setReducerClass(KeyValueSortReducer.class);
// job.setOutputFormatClass(org.apache.hadoop.hbase.mapreduce.HFileOutputFormat.class);
job.setOutputFormatClass(HFileOutputFormat.class);
// job.setNumReduceTasks(4);
// job.setPartitionerClass(org.apache.hadoop.hbase.mapreduce.SimpleTotalOrderPartitioner.class); // HBaseAdmin admin = new HBaseAdmin(conf);
HTable table = new HTable(conf, "url_rule"); HFileOutputFormat.configureIncrementalLoad(job, table); FileInputFormat.addInputPath(job, new Path(args[]));
FileOutputFormat.setOutputPath(job, new Path(args[])); System.exit(job.waitForCompletion(true) ? : );
} }
hfile导入到表的代码:
package com.bonc.db;
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles;
import org.apache.hadoop.hbase.util.Bytes; public class TestLoadIncrementalHFileToHBase { // private static final byte[] TABLE = Bytes.toBytes("hua");
// private static final byte[] QUALIFIER = Bytes.toBytes("PROTOCOLID");
// private static final byte[] FAMILY = Bytes.toBytes("PROTOCOLID"); public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
// byte[] TABLE = Bytes.toBytes("hua");
byte[] TABLE = Bytes.toBytes(args[]);
HTable table = new HTable(TABLE);
LoadIncrementalHFiles loader = new LoadIncrementalHFiles(conf);
loader.doBulkLoad(new Path(args[]), table);
// loader.doBulkLoad(new Path("/hua/testHFileResult/"), table);
} }
悲剧的是在从hfile导入到表的时候报错:
java.io.IOException: java.io.IOException: Failed rename of maprfs://133.0.76.41:7222/user/hbasetest/url_type/4447706551787973235 to maprfs://133.0.76.41:7222/hbase/url_rule/732e6d3d150caa8bd3d8d228e3d9c9a0/url_type/914168143008836217
at org.apache.hadoop.hbase.regionserver.StoreFile.rename(StoreFile.java:512)
虽然解决办法在这里:
http://answers.mapr.com/questions/685/hbase-and-completebulkload
但是我实在是没看懂。so,我采用了最原始的方法:
split将文件分割成小文件,然后:
package com.bonc.db;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException; import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put; import com.bonc.URLMatch.HBaseMain;
public class URL_HBase { public static void main(String[] args) throws IOException{
//文件绝对路径改成你自己的文件路径
FileReader fr=new FileReader(args[]);
//可以换成工程目录下的其他文本文件
HTablePool pool = new HTablePool(HBaseMain.conf, );
HTable table = (HTable) pool.getTable("url_rule");
BufferedReader br=new BufferedReader(fr);
while(br.readLine()!=null){
String[] s=br.readLine().toString().split("\\|");
if(s.length>){
Put put = new Put(s[].trim().getBytes());
put.add("url_type".getBytes(), "type".getBytes(), s[].trim().getBytes());
table.put(put);
}else{
System.out.println(s);
}
}
br.close();
}
}
终于成功了,希望有人能够帮我翻译一下,怎么解决是个什么意思。。唉。
hbase数据导入的更多相关文章
- ImportTsv-HBase数据导入工具
一.概述 HBase官方提供了基于Mapreduce的批量数据导入工具:Bulk load和ImportTsv.关于Bulk load大家可以看下我另一篇博文. 通常HBase用户会使用HBase A ...
- Bulk Load-HBase数据导入最佳实践
一.概述 HBase本身提供了非常多种数据导入的方式,通常有两种经常使用方式: 1.使用HBase提供的TableOutputFormat,原理是通过一个Mapreduce作业将数据导入HBase 2 ...
- HBase数据导入导出工具
hbase中自带一些数据导入.导出工具 1. ImportTsv直接导入 1.1 hbase中建表 create 'testtable4','cf1','cf2' 1.2 准备数据文件data.txt ...
- Hbase数据导入导出
平时用于从生产环境hbase到导出数据到测试环境. 导入数据: import java.io.BufferedReader; import java.io.File; import java.io.F ...
- Hive及HBase数据迁移
一. Hive数据迁移 场景:两个Hadoop平台集群之间Hive表迁移. 基本思路:Hive表元数据和文件数据export到HDFS文件,通过Distcp将HDFS迁移到另一个集群的HDFS文件,再 ...
- HBase 实战(1)--HBase的数据导入方式
前言: 作为Hadoop生态系统中重要的一员, HBase作为分布式列式存储, 在线实时处理的特性, 备受瞩目, 将来能在很多应用场景, 取代传统关系型数据库的江湖地位. 本篇博文重点讲解HBase的 ...
- HBase(三): Azure HDInsigt HBase表数据导入本地HBase
目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见 HDP2.4安装(五):集群及组件安装 , ...
- [原创]HBase学习笔记(4)- 数据导入
需要分别从Oracle和文本文件往HBase中导入数据,这里介绍几种数据导入方案. 1.使用importTSV导入HBase importTSV支持增量导入.新数据插入,已存在数据则修改. 1.1.首 ...
- Sqoop将mysql数据导入hbase的血与泪
Sqoop将mysql数据导入hbase的血与泪(整整搞了大半天) 版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: https://my.oschina.net/yunsh ...
随机推荐
- centos中mariadb的相关操作
Tip 1 在使用mariadb中启动服务报错 : Failed to start mariadb.service: Unit not found. 解决办法: yum install -y mari ...
- Func<>委托、扩展方法、yield、linq ForEach综合运用
1.先定义一个Model类 public class P1 { public string name { get; set; } public int age ...
- 几种常用的java 实现反转的方法———reverse
1.最简单的方法 public static String reverse1(String str) return new StringBuffer(str).reverse().toString() ...
- 记一次被自己DDOS攻击
服务器报警初步分析进一步分析最终分析总结 TOC 服务器报警 7月24号下午5点半开始,突然服务器报警,检查监控,发现CPU异常100%. 该服务器正常情况下CPU使用率在40%已经算高了,另外负载经 ...
- python图像处理-生成随机验证码
前面说了PIL库,还说了图片的缩放.旋转和翻转.现在说下网站上常用的随机验证码的生成.参考网站:https://www.liaoxuefeng.com/wiki/1016959663602400/10 ...
- python lambda表达式的两种用处
1 用处1定义匿名函数 不带参数的: a = ") 带参数的 b = lambda x, y:x * y 2 当函数作为参数时,直接为该函数传参. def func1(m, n): retu ...
- centos7重启网卡报Job for network.service failed because...错误
解决: [root@mina0 hadoop]# systemctl stop NetworkManager[root@mina0 hadoop]# systemctl disable Network ...
- mint-ui下拉加载(demo实例)
<template> <div class="share"> <div class="header"> <div cl ...
- Java注解【五、注解实战】
需求: 1.表:用户ID,用户名,年龄,邮箱. 2.实现方法,传入实体,打印sql. 实现: 1.表: package Annotation; @Table("user") pub ...
- 目标检测之车辆行人(darknet版yolov3)
序言 自动驾驶是目前非常有前景的行业,而视觉感知作为自动驾驶中的“眼睛”,有着非常重要的地位和作用.为了能有效地识别到行驶在路上的动态目标,如汽车.行人等,我们需要提前对这些目标的进行训练, ...