设为首页 收藏本站
查看: 352|回复: 0

[经验分享] Hadoop简单的Map/Reduce

[复制链接]

尚未签到

发表于 2016-12-9 06:23:21 | 显示全部楼层 |阅读模式
/**
* 需求:
*     统计Hbase数据库中某个字符串的使用人数
*     字符串存储格式 1002;1003,2003,1443;1232,232
*   最后统计 模块   使用人数
*       1    1002    X
*       ...
*     把最后的统计信息存储到结果表中.
*/
public class CountUse {
public static Logger loger = Wloger.loger;
/**
* Internal Mapper to be run by Hadoop.
*/
public static class MapTask extends TableMapper<UdcSellHTable, IntWritable> {
private UdcSellHTable udcSell = new UdcSellHTable();
private String tableName;
private byte[] family;
private HashMap<String, byte[]> indexes;
private UdcSellHTable word = new UdcSellHTable();
private final static IntWritable one = new IntWritable(1);
/**
* Map 统计使用人数
*/
@SuppressWarnings("static-access")
@Override
protected void map(ImmutableBytesWritable rowKey, Result result,
Context context) throws IOException, InterruptedException {
// 搜索数据库设置到对象当中
for (Map.Entry<String, byte[]> entity : indexes.entrySet()) {
String column = entity.getKey();
PropertyName property = new PropertyName(family, entity
.getValue());
Object type = udcSell.types.get(column);
Object value = CountUseUtil.changeValue(new Entity(tableName,
result), property, type);// 数据库中的值
try {
loger.info("debug:"+udcSell);
loger.info("debug:"+UdcSellHTable.filter(column));
loger.info("debug:"+value);
BeanUtils.setProperty(udcSell, UdcSellHTable.filter(column),
value);
} catch (Exception e) {
loger.info("对象属性定义有问题,请check[" + udcSell + ":" + column
+ "[" + value + "]]");
}
}
// filter 属性过滤,过滤掉不符合规则的记录
String widget = udcSell.getWidget();// widget数据,//674324;321,321,312;3321
word.setWidget(widget);//设置widget
loger.debug("debug:"+widget);
List<String> keys = CountUseUtil.getKeys(widget);
for (String key : keys) {
word.setUserId(udcSell.getUserId());
word.setDate(udcSell.getDate());
word.setModuleId(Long.parseLong(key));
word.setUseCount(1);
loger.info("Map <word>" + word.toString() + "</word>");
context.write(word, one);
}
}
@SuppressWarnings("static-access")
@Override
protected void setup(Context context) throws IOException,
InterruptedException {
Configuration configuration = context.getConfiguration();
tableName = configuration.get("input.tablename");
String familyName = configuration.get("input.familyname");
udcSell.setDate(CountUseUtil.yestoday());
String[] fields = new String[] { udcSell.COLUMN_USERID,
udcSell.getWidgetDate() };
indexes = new HashMap<String, byte[]>();
for (String field : fields) {
indexes.put(field, Bytes.toBytes(field));
}
family = Bytes.toBytes(familyName);
}
}
/**
* Reduce进行合计
*
*/
public static class CombinerTask extends
Reducer<UdcSellHTable, IntWritable, UdcSellHTable, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(UdcSellHTable key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
loger.info("combiner <key>" + key + "</key>");
loger.info("combiner <sum>" + sum + "</sum>");
context.write(key, result);
}
}
/**
* Reduce合计入库
*
*/
public static class Reduce extends
TableReducer<UdcSellHTable, IntWritable, ImmutableBytesWritable> {
private UdcSellHTable udcSell = new UdcSellHTable();
private String tableName;
private IntWritable result = new IntWritable();
private HashMap<String, byte[]> indexes;
private byte[] family;
public void reduce(UdcSellHTable key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
key.setUseCount(result.get());
loger.info("Reduce <key>" + key + "</key>");
loger.info("Reduce <sum>" + sum + "</sum>");
String keyStr = "1_" + key.getModuleId();
Put put = new Put(Bytes.toBytes(keyStr));// 以模块id为key
for (Map.Entry<String, byte[]> entity : indexes.entrySet()) {
try {
put.add(family, entity.getValue(), Bytes.toBytes(BeanUtils
.getProperty(key, entity.getKey())));
} catch (Exception e) {
loger.info("对象属性定义有问题,请check[" + udcSell + ":"
+ entity.getKey() + "[" + entity.getValue() + "]]");
}
}
ImmutableBytesWritable imw = new ImmutableBytesWritable(Bytes
.toBytes(tableName));
context.write(imw, put);
}
@SuppressWarnings("static-access")
@Override
protected void setup(Context context) throws IOException,
InterruptedException {
Configuration configuration = context.getConfiguration();
tableName = configuration.get("output.tablename");
String familyName = configuration.get("output.familyname");
family = Bytes.toBytes(familyName);
String[] fields = new String[] { udcSell.COLUMN_USERID,udcSell.COLUMN_MODULEID,
udcSell.COLUMN_USE };
indexes = new HashMap<String, byte[]>();
for (String field : fields) {
indexes.put(field, Bytes.toBytes(field));
}
}
}
/**
*
*
* Job configuration.
*/
public static Job configureJob(Configuration conf, String[] args)
throws IOException {
String inputTable = "udc_sell";
String inputFamily = "s_year";
String outputTable = "job_result";
String outputFamily = "s_base";
conf.set("input.tablename", inputTable);
conf.set("input.familyname", inputFamily);
conf.set(TableInputFormat.INPUT_TABLE, inputTable);
conf.set("output.tablename", outputTable);
conf.set("output.familyname", outputFamily);
conf.set(TableOutputFormat.OUTPUT_TABLE, outputTable);
Job job = new Job(conf, inputTable);
job.setNumReduceTasks(1);
job.setJarByClass(CountUse.class);
job.setMapperClass(MapTask.class);
job.setMapOutputKeyClass(UdcSellHTable.class);
job.setMapOutputValueClass(IntWritable.class);
job.setCombinerClass(CombinerTask.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TableInputFormat.class);
job.setOutputFormatClass(MultiTableOutputFormat.class);
return job;
}
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
loger.info("开始 job");
Job job = configureJob(conf, otherArgs);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-311508-1-1.html 上篇帖子: hadoop自己的例子 下篇帖子: hadoop任务调度详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表