Mapreduce读取Hbase表,写数据到一个Hbase表中

public class LabelJob
{

    public static void main(String[] args)
        throws Exception
    {
        Job job = Job.getInstance(new Configuration());
        job.setJarByClass(LabelJob.class);
        job.setJobName("Hbase.LabelJob");

        Configuration conf = job.getConfiguration();
        conf.set("tablename", "product_tags");

        Scan scan = new Scan();
        scan.setCaching(500);
        scan.setCacheBlocks(false);
        //输入表
        TableMapReduceUtil.initTableMapperJob("tb_user", scan, LabelMapper.class, Text.class, Text.class, job);

        job.setReducerClass(LabelReducer.class);
        //输出表
        TableMapReduceUtil.initTableReducerJob("usertags", LabelReducer.class, job);
        job.waitForCompletion(true);

    }

}

  

public class LabelMapper extends TableMapper<Text, Text>
{
      protected void setup(Context context)
        throws IOException, InterruptedException
    {
        super.setup(context);
        String tablename = context.getConfiguration().get("tablename");
         .................
    }
 protected void map(ImmutableBytesWritable rowKey, Result result, Context context)
        throws IOException, InterruptedException
    {
          String userid = Bytes.toString(rowKey.get()); // 读取HBase用户表rowkey

          String strlabel = fhb.getStringValue(result, "labels", "label");
         String[] userLabels = strlabel.split(",");
....................
}
}

  

public class LabelReducer extends TableReducer<Text, Text, ImmutableBytesWritable>
{
     @Override
    public void reduce(Text key, Iterable<Text> values, Context context)
        throws IOException, InterruptedException
    {
String rowKey = key.toString();// 读取Map输出
 for (Text v : values)
            {
                String tag = v.toString();
                Long count = tagMap.get(tag);
                tagMap.put(tag, (count == null) ? 1 : (count + 1));// 计数
            }
 Put put = new Put(productId.getBytes());
  put.add("prodtags".getBytes(), "prodtags".getBytes(),outputlabel.toString().getBytes());

context.write(new ImmutableBytesWritable(productId.getBytes()), put);

}

}

  

时间: 2024-08-29 15:26:29

Mapreduce读取Hbase表,写数据到一个Hbase表中的相关文章

MySQL 数据库中如何把A表的数据插入到B表?

web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要指定导入字段,设置只需要导入目标表中不存在的记录,虽然这些都可以在程序中拆分成简单sql来实现,但是用一个sql的话,会节省大量代码. 以mysql数据库为例分情况一一说明: 两张表:insertTest和insertTest2,前者中有测试数据 create table insertTest(id int(4),name varchar(12));insert into insertTest values(100,'tom')

如何把一个excel工作薄中N个工作表复制到另一个工作薄中

一般遇到标题这样的情况,许多人可能会一个一个的复制粘贴,其实完全不必那么麻烦. 你可以按以下步骤来操作: 第一步:打开所有要操作的excel工作薄 第二部:按住Shift键,选择所有要复制的工作表,在选择的工作表上单击右键 第三步:在弹出的菜单中,选择移动或复制(M)...选项. 第四步:在新弹出的窗口中选择转移到的目标工作薄,以及放置的位置.点击确定,转移成功.

mysql表ibdata1数据切换到单个表储存、默认导出的表结构

释放MySQL ibdata1文件的空间 mysqldump选项: --add-drop-table:在每个创建数据库表语句前添加删除数据库表的语句: --add-locks:备份数据库表时锁定数据库表: --all-databases:备份MySQL服务器上的所有数据库: --comments:添加注释信息: --compact:压缩模式,产生更少的输出: --complete-insert:输出完成的插入语句: --databases:指定要备份的数据库: --default-charact

Oracle数据库表空间 数据文件 用户 以及表创建的SQL代码

--create the tablespace CREATE SMALLFILE TABLESPACE "TABLE_CONTAINER" --创建表空间 DATAFILE 'E:\ORACLE\ORADATA\ORCL\table_01.DBF' --建立数据文件,数据文件的文件位置 SIZE 100M --数据文件的初始大小 AUTOEXTEND ON NEXT 100M --数据文件增量大小 MAXSIZE UNLIMITED --数据文件大小无限制 LOGGING EXTENT

读取每行的数据,加入到list中

有txt文件中,每行都有一个字符串或者数据,将每行的数据转换到一个list中 例如: 1 2 3 6 实现: f = open("test1.txt",'r') list1 = [] for i in f.readlines(): list1.append(i.strip('\n')) print(list1) f.close() 输出: ['1', '2', '3', '6'] 原文地址:https://www.cnblogs.com/yrxns/p/9889971.html

ssh框架提交表单数据后,数据库表出现问号?的解决方法

解决方法如下:连接数据库的时候需要指定编码方式,如下所示: jdbc.url = jdbc:mysql:///dbName?useUnicode=true&characterEncoding=UTF-8

mapreduce 只使用Mapper往多个hbase表中写数据

只使用Mapper不使用reduce会大大减少mapreduce程序的运行时间. 有时候程序会往多张hbase表写数据. 所以有如题的需求. 下面给出的代码,不是可以运行的代码,只是展示driver中需要进行的必要项设置,mapper类需要实现的接口,map函数需要的参数以及函数内部的处理方式. 实现过程比较曲折,只贴代码: class Qos2HbaseDriver extends Configured implements Tool { private static Logger logge

【原创】问题定位分享(16)spark写数据到hive外部表报错ClassCastException: org.apache.hadoop.hive.hbase.HiveHBaseTableOutputFormat cannot be cast to org.apache.hadoop.hive.ql.io.HiveOutputFormat

spark 2.1.1 spark在写数据到hive外部表(底层数据在hbase中)时会报错 Caused by: java.lang.ClassCastException: org.apache.hadoop.hive.hbase.HiveHBaseTableOutputFormat cannot be cast to org.apache.hadoop.hive.ql.io.HiveOutputFormat at org.apache.spark.sql.hive.SparkHiveWrit

sqlite expert导入excel表格 (包括使用问题、以及把 一个表的数据插入到另一个表中)

一.sqlite导入excel 1.打开要导入的excel --- 另存为(2007版点击左上角OFFICE图标即可找到) --- 保存类型:CSV类型 (提示什么不兼容什么的,一律点确定.) 2.打开sqlite expert ,选择建好的数据库文件(或者是导入的已存在的db文件,或者是自己新建一个.db数据库文件). 右击文件名---选择最后一个import text file(CSV,TSV) 3.(如果没有建过和这个excel名相同的表)右边Destination选择第一项new tab