mapreduce处理结果向输出至mysql(直接插入/更新/追加式更新)

 

Java代码   收藏代码
  1. package cn.m15.ipj.job.usergroup;  
  2.   
  3. Mapreduce处理结果向输出至mysql  
  4.   
  5. 1.写入mysql  
  6.     <1>job中输出的配置:  
  7.         DBOutputFormat.setOutput(this, MySQLConstant.MYSQL_FIX_USER,  
  8.         MySQLConstant.MYSQL_FIX_OPEN_FIRST_FIELDS);  
  9.         (DBOutputFormat为hadoop自带API,将输入插入数据库)  
  10.         public final static String  
  11.         MYSQL_FIX_USER =  
  12.         "ipj_fix_user";  
  13.         public final static String[]  
  14.         MYSQL_FIX_OPEN_FIRST_FIELDS = {"app_id","version","imei","first_open","date"};  
  15.            
  16.     <2>reduce中写入:  
  17.         private FixOpenAppFirstRecord record = new FixOpenAppFirstRecord();  
  18.            
  19.         record.setApp_id(Integer.parseInt(app_id));  
  20.         record.setImei(imei);  
  21.         record.setVersion(version);  
  22.         record.setFirst_open(exactDate);  
  23.         record.setDate(date);  
  24.         context.write(record, NULL);  
  25.            
  26.     <3>FixOpenAppFirstRecord中字段的顺序配置(只列出一条):  
  27.         @Override  
  28.         public void write(PreparedStatement statement) throws SQLException {  
  29.             statement.setInt(1, app_id);  
  30.             statement.setString(2, version);  
  31.             statement.setString(3, imei);  
  32.             statement.setString(4, first_open);  
  33.             statement.setString(5, date);  
  34.         }  
  35.     <4>注意:  
  36.         1.reduce中record的set值的顺序无所谓,可以任意  
  37.         2.job的mysql字段MYSQL_FIX_OPEN_FIRST_FIELDS的顺序一定要和类FixOpenAppFirstRecord中字段的配置顺序一致  
  38. 2.更新mysql(改变值)  
  39.     <1>job中输出的配置:  
  40.         FixDBOutputFormat.setOutput(this, MySQLConstant.MYSQL_FIX_USER,  
  41.         MySQLConstant.MYSQL_FIX_IS_TAOBAO_FIELDS);  
  42.         (FixDBOutputFormat为自定义Format类,用于更新mysql)  
  43.         public final static String  
  44.         MYSQL_FIX_USER =  
  45.         "ipj_fix_user";  
  46.         public final static String[]  
  47.         MYSQL_FIX_IS_WEIBO_FIELDS = {"is_weibo","app_id","version","imei"};  
  48.     <2>reduce中写入:  
  49.         private FixIsMallUserRecord record = new FixIsMallUserRecord();  
  50.         record.setApp_id(Integer.parseInt(app_id));  
  51.         record.setVersion(version);  
  52.         record.setImei(imei);  
  53.         record.setIs_taobao(is_taobao);  
  54.         context.write(record, NULL);  
  55.     <3>FixIsMallUserRecord 中字段的顺序配置(只列出一条):  
  56.         @Override  
  57.         public void readFields(ResultSet resultSet) throws SQLException {  
  58.             this.is_taobao = resultSet.getInt(1);  
  59.             this.app_id = resultSet.getInt(2);  
  60.             this.version = resultSet.getString(3);  
  61.             this.imei = resultSet.getString(4);  
  62.         }  
  63.     <4>FixDBOutputFormat中关键的拼接sql代码:  
  64.         public String constructQuery(String table, String[] fieldNames) {  
  65.         if (fieldNames == null) {  
  66.             throw new IllegalArgumentException(  
  67.             "Field names may not be null");  
  68.         }  
  69.            
  70.         StringBuilder query = new StringBuilder();  
  71.         query.append("UPDATE ").append(table);  
  72.         if (fieldNames.length > 0 && fieldNames[0] != null  
  73.         && fieldNames[1] != null&& fieldNames[2] != null  
  74.         && fieldNames[3] != null) {  
  75.             query.append(" SET ");  
  76.             query.append(fieldNames[0] + " =?");  
  77.             query.append(" WHERE ");  
  78.             query.append(fieldNames[1] + " =?");  
  79.             query.append(" AND ");  
  80.             query.append(fieldNames[2] + " =?");  
  81.             query.append(" AND ");  
  82.             query.append(fieldNames[3] + " =?");  
  83.             return query.toString();  
  84.             }  
  85.             return null;  
  86.         }  
  87.     <5>注意:  
  88.         1.reduce中record的set值的顺序无所谓,可以任意  
  89.         2.job的mysql字段MYSQL_FIX_IS_WEIBO_FIELDS的顺序一定要和类  
  90.         3.2中的顺序也一定要和FixDBOutputFormat类中的更新顺序一致(第一个参数为要更新的值,第二三四个参数分别为条件参数)  
  91. 3.更新mysql(值的追加)  
  92.     <1>job中输出的配置:  
  93.         FixAppendDBOutputFormat.setOutput(this, MySQLConstant.MYSQL_FIX_USER,  
  94.         MySQLConstant.MYSQL_FIX_MALL_LOGIN_FIELDS);  
  95.         (FixAppendDBOutputFormat自定义Format,用户更新mysql[追加])  
  96.         public final static String  
  97.         MYSQL_FIX_USER =  
  98.         "ipj_fix_user";  
  99.         public final static String[]  
  100.         MYSQL_FIX_MALL_LOGIN_FIELDS = {"login_taobao_count","app_id","version","imei"};  
  101.     <2>reduce中写入:  
  102.         private FixMallTotalLoginRecord record = new FixMallTotalLoginRecord();  
  103.         record.setApp_id(Integer.parseInt(app_id));  
  104.         record.setVersion(version);  
  105.         record.setImei(imei);  
  106.         record.setLogin_taobao_count(num);  
  107.         context.write(record, NULL);  
  108.     <3>FixIsMallUserRecord 中字段的顺序配置(只列出一条):  
  109.         @Override  
  110.         public void write(PreparedStatement statement) throws SQLException {  
  111.             statement.setInt(1, login_taobao_count);  
  112.             statement.setInt(2, app_id);  
  113.             statement.setString(3, version);  
  114.             statement.setString(4, imei);  
  115.         }  
  116.     <4>FixAppendDBOutputFormat中关键的拼接sql代码  
  117.         public String constructQuery(String table, String[] fieldNames) {  
  118.             if (fieldNames == null) {  
  119.                 throw new IllegalArgumentException  
  120.                 ("Field names may not be null");  
  121.             }  
  122.             StringBuilder query = new StringBuilder();  
  123.             query.append("UPDATE ").append(table);  
  124.             if ( fieldNames.length > 0 &&  
  125.             fieldNames[0] != null &&  
  126.             fieldNames[1] != null &&  
  127.             fieldNames[2] != null &&  
  128.             fieldNames[3] != null) {  
  129.                 query.append(" SET ");  
  130.                 query.append(fieldNames[0] +  
  131.                 " = "+fieldNames[0]+"+?");  
  132.                 query.append(" WHERE ");  
  133.                 query.append(fieldNames[1] + " =?");  
  134.                 query.append(" AND ");  
  135.                 query.append(fieldNames[2] + " =?");  
  136.                 query.append(" AND ");  
  137.                 query.append(fieldNames[3] + " =?");  
  138.                 return query.toString();  
  139.                 }  
  140.             return null;  
  141.         }  
  142.     <5>注意:  
  143.         1.reduce中record的set值的顺序无所谓,可以任意  
  144.         2.job的mysql字段MYSQL_FIX_MALL_LOGIN_FIELDS的顺序一定要和类  
  145.         3.2中的顺序也一定要和FixAppendDBOutputFormat类中的更新顺序一致(第一个参数为要更新的值[在原有基础上增加],第二三四个参数分别为条件参数)  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值