将手机上网日志文件批量导入到Hbase中。操作步骤:

1、将日志文件(请下载附件)上传到HDFS中,利用hadoop的操作命令上传:hadoop  fs -put input  /

2、创建Hbase表,通过Java操作

  1. package com.jiewen.hbase;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.HColumnDescriptor;
  6. import org.apache.hadoop.hbase.HTableDescriptor;
  7. import org.apache.hadoop.hbase.client.Get;
  8. import org.apache.hadoop.hbase.client.HBaseAdmin;
  9. import org.apache.hadoop.hbase.client.HTable;
  10. import org.apache.hadoop.hbase.client.Put;
  11. import org.apache.hadoop.hbase.client.Result;
  12. import org.apache.hadoop.hbase.client.ResultScanner;
  13. import org.apache.hadoop.hbase.client.Scan;
  14. import org.apache.hadoop.hbase.util.Bytes;
  15. public class HbaseDemo {
  16. public static void main(String[] args) throws IOException {
  17. String tableName = "wlan_log";
  18. String columnFamily = "cf";
  19. HbaseDemo.create(tableName, columnFamily);
  20. // HbaseDemo.put(tableName, "row1", columnFamily, "cl1", "data");
  21. // HbaseDemo.get(tableName, "row1");
  22. // HbaseDemo.scan(tableName);
  23. // HbaseDemo.delete(tableName);
  24. }
  25. // hbase操作必备
  26. private static Configuration getConfiguration() {
  27. Configuration conf = HBaseConfiguration.create();
  28. conf.set("hbase.rootdir", "hdfs://hadoop1:9000/hbase");
  29. // 使用eclipse时必须加入这个,否则无法定位
  30. conf.set("hbase.zookeeper.quorum", "hadoop1");
  31. return conf;
  32. }
  33. // 创建一张表
  34. public static void create(String tableName, String columnFamily)
  35. throws IOException {
  36. HBaseAdmin admin = new HBaseAdmin(getConfiguration());
  37. if (admin.tableExists(tableName)) {
  38. System.out.println("table exists!");
  39. } else {
  40. HTableDescriptor tableDesc = new HTableDescriptor(tableName);
  41. tableDesc.addFamily(new HColumnDescriptor(columnFamily));
  42. admin.createTable(tableDesc);
  43. System.out.println("create table success!");
  44. }
  45. }
  46. // 加入一条记录
  47. public static void put(String tableName, String row, String columnFamily,
  48. String column, String data) throws IOException {
  49. HTable table = new HTable(getConfiguration(), tableName);
  50. Put p1 = new Put(Bytes.toBytes(row));
  51. p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes
  52. .toBytes(data));
  53. table.put(p1);
  54. System.out.println("put'" + row + "'," + columnFamily + ":" + column
  55. + "','" + data + "'");
  56. }
  57. // 读取一条记录
  58. public static void get(String tableName, String row) throws IOException {
  59. HTable table = new HTable(getConfiguration(), tableName);
  60. Get get = new Get(Bytes.toBytes(row));
  61. Result result = table.get(get);
  62. System.out.println("Get: " + result);
  63. }
  64. // 显示全部数据
  65. public static void scan(String tableName) throws IOException {
  66. HTable table = new HTable(getConfiguration(), tableName);
  67. Scan scan = new Scan();
  68. ResultScanner scanner = table.getScanner(scan);
  69. for (Result result : scanner) {
  70. System.out.println("Scan: " + result);
  71. }
  72. }
  73. // 删除表
  74. public static void delete(String tableName) throws IOException {
  75. HBaseAdmin admin = new HBaseAdmin(getConfiguration());
  76. if (admin.tableExists(tableName)) {
  77. try {
  78. admin.disableTable(tableName);
  79. admin.deleteTable(tableName);
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. System.out.println("Delete " + tableName + " 失败");
  83. }
  84. }
  85. System.out.println("Delete " + tableName + " 成功");
  86. }
  87. }

3、将日志文件导入Hbase表wlan_log中:

  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.client.Put;
  5. import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
  6. import org.apache.hadoop.hbase.mapreduce.TableReducer;
  7. import org.apache.hadoop.hbase.util.Bytes;
  8. import org.apache.hadoop.io.LongWritable;
  9. import org.apache.hadoop.io.NullWritable;
  10. import org.apache.hadoop.io.Text;
  11. import org.apache.hadoop.mapreduce.Counter;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  15. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  16. public class HbaseBatchImport {
  17. public static void main(String[] args) throws Exception {
  18. final Configuration configuration = new Configuration();
  19. // 设置zookeeper
  20. configuration.set("hbase.zookeeper.quorum", "hadoop1");
  21. // 设置hbase表名称
  22. configuration.set(TableOutputFormat.OUTPUT_TABLE, "wlan_log");
  23. // 将该值改大,防止hbase超时退出
  24. configuration.set("dfs.socket.timeout", "180000");
  25. final Job job = new Job(configuration, "HBaseBatchImport");
  26. job.setMapperClass(BatchImportMapper.class);
  27. job.setReducerClass(BatchImportReducer.class);
  28. // 设置map的输出,不设置reduce的输出类型
  29. job.setMapOutputKeyClass(LongWritable.class);
  30. job.setMapOutputValueClass(Text.class);
  31. job.setInputFormatClass(TextInputFormat.class);
  32. // 不再设置输出路径。而是设置输出格式类型
  33. job.setOutputFormatClass(TableOutputFormat.class);
  34. FileInputFormat.setInputPaths(job, "hdfs://hadoop1:9000/input");
  35. job.waitForCompletion(true);
  36. }
  37. static class BatchImportMapper extends
  38. Mapper<LongWritable, Text, LongWritable, Text> {
  39. SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyyMMddHHmmss");
  40. Text v2 = new Text();
  41. protected void map(LongWritable key, Text value, Context context)
  42. throws java.io.IOException, InterruptedException {
  43. final String[] splited = value.toString().split("\t");
  44. try {
  45. final Date date = new Date(Long.parseLong(splited[0].trim()));
  46. final String dateFormat = dateformat1.format(date);
  47. String rowKey = splited[1] + ":" + dateFormat;
  48. v2.set(rowKey + "\t" + value.toString());
  49. context.write(key, v2);
  50. } catch (NumberFormatException e) {
  51. final Counter counter = context.getCounter("BatchImport",
  52. "ErrorFormat");
  53. counter.increment(1L);
  54. System.out.println("出错了" + splited[0] + " " + e.getMessage());
  55. }
  56. };
  57. }
  58. static class BatchImportReducer extends
  59. TableReducer<LongWritable, Text, NullWritable> {
  60. protected void reduce(LongWritable key,
  61. java.lang.Iterable<Text> values, Context context)
  62. throws java.io.IOException, InterruptedException {
  63. for (Text text : values) {
  64. final String[] splited = text.toString().split("\t");
  65. final Put put = new Put(Bytes.toBytes(splited[0]));
  66. put.add(Bytes.toBytes("cf"), Bytes.toBytes("date"), Bytes
  67. .toBytes(splited[1]));
  68. // 省略其它字段,调用put.add(....)就可以
  69. context.write(NullWritable.get(), put);
  70. }
  71. };
  72. }
  73. }

4、查看导入结果:

Hbase调用JavaAPI实现批量导入操作的更多相关文章

  1. 批量导入数据到HBase

    hbase一般用于大数据的批量分析,所以在很多情况下需要将大量数据从外部导入到hbase中,hbase提供了一种导入数据的方式,主要用于批量导入大量数据,即importtsv工具,用法如下:   Us ...

  2. 吴裕雄--天生自然HADOOP操作实验学习笔记:hbase的javaAPI应用

    实验目的 进一步了解hbase的操作 熟悉使用IDEA进行java开发 熟悉hbase的javaAPI 实验原理 前面已经了解通过hbase的shell操作hbase,确实比较难以使用,另外通过hiv ...

  3. python操作数据库之批量导入

    python操作数据库之批量导入 Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进. Python具有丰富和强大的库.它常被昵称为胶水语言,能够把用其他语言制作的 ...

  4. HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)

    HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ...

  5. Hbase笔记:批量导入

    工作中可能会有对HBase的复杂操作,我们现在对HBase的操作太简单了.复杂操作一般用HBaseScan操作,还有用框架对HBase进行复杂操作,iparler,sharker.我们说HBase是数 ...

  6. 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 ...

  7. Hadoop之——HBASE结合MapReduce批量导入数据

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46463889 废话不多说.直接上代码,你懂得 package hbase; imp ...

  8. Spark:DataFrame批量导入Hbase的两种方式(HFile、Hive)

    Spark处理后的结果数据resultDataFrame可以有多种存储介质,比较常见是存储为文件.关系型数据库,非关系行数据库. 各种方式有各自的特点,对于海量数据而言,如果想要达到实时查询的目的,使 ...

  9. 数据批量导入HBase

    测试数据: datas 1001 lilei 17 13800001111 1002 lily 16 13800001112 1003 lucy 16 13800001113 1004 meimei ...

随机推荐

  1. 优化mysql查询

    mysql提供了一个特别的explain语句,用来分析查询语句的性能 : explain select ... 1.在所有用于where,order by,group by的列上添加索引 创建索引 添 ...

  2. Elasticsearchs的安装/laravel-scout和laravel-scout-elastic的安装

    安装: https://github.com/medcl/elasticsearch-rtf 先下载包 下载解压后 cd elasticsearch-rtf-master ll bin/elastic ...

  3. linux系统,python3.7环境安装talib过程

    获取源码wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz 解压进入目录tar -zxvf ta-lib-0. ...

  4. java 比较String StringBuffer StringBuilder

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...

  5. Leetcode 363.矩形区域不超过k的最大数值和

    矩形区域不超过k的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,-2,3]], ...

  6. 关于IOS项目QQ空间授权提示安装最新版本的QQ的解决方法!

  7. Count on a tree(bzoj 2588)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  8. 蒲公英(bzoj 2724)

    Description Input 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1 Output Sample Input ...

  9. UVa1363 Joseph's Problem

    把整个序列进行拆分成[k,k/2),[k/2, k/3), [k/3,k/4)...k[k/a, k/b)的形式,对于k/i(整除)相同的项,k%i成等差数列. /*by SilverN*/ #inc ...

  10. Codeforces961F. k-substrings

    $n \leq 1000000$的字符串,对每一个子串$i$~$n-i+1$,求他最长的一个既是前缀又是后缀的子串. 这题要求的东西具有“对称性”,不充分利用难以解决.这里的“对称性”不仅指询问是对称 ...