mr如何使用hcatalog读写hive表
本篇文章给大家分享的是有关mr如何使用hcatalog读写hive表,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、网站设计、城西网络推广、小程序设计、城西网络营销、城西企业策划、城西品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供城西建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
public class onTimeMapper extends Mapper {
@Override
protected void map(WritableComparable key, HCatRecord value,
org.apache.hadoop.mapreduce.Mapper.Context context)
throws IOException, InterruptedException {
// Get table schema
HCatSchema schema = HCatBaseInputFormat.getTableSchema(context);
Integer year = new Integer(value.getString("year", schema));
Integer month = new Integer(value.getString("month", schema));
Integer DayofMonth = value.getInteger("dayofmonth", schema);
context.write(new IntPair(year, month), new IntWritable(DayofMonth));
}
}
public class onTimeReducer extends Reducer {public void reduce (IntPair key, Iterable value, Context context) throws IOException, InterruptedException{ int count = 0; // records counter for particular year-month for (IntWritable s:value) { count++; } // define output record schema List columns = new ArrayList(3); columns.add(new HCatFieldSchema("year", HCatFieldSchema.Type.INT, "")); columns.add(new HCatFieldSchema("month", HCatFieldSchema.Type.INT, "")); columns.add(new HCatFieldSchema("flightCount", HCatFieldSchema.Type.INT,"")); HCatSchema schema = new HCatSchema(columns); HCatRecord record = new DefaultHCatRecord(3); record.setInteger("year", schema, key.getFirstInt()); record.set("month", schema, key.getSecondInt()); record.set("flightCount", schema, count); context.write(null, record);}}
public class onTimeDriver extends Configured implements Tool{
private static final Log log = LogFactory.getLog( onTimeDriver.class );
public int run( String[] args ) throws Exception{
Configuration conf = new Configuration();
Job job = new Job(conf, "OnTimeCount");
job.setJarByClass(onTimeDriver.class);
job.setMapperClass(onTimeMapper.class);
job.setReducerClass(onTimeReducer.class);
HCatInputFormat.setInput(job, "airline", "ontimeperf");
job.setInputFormatClass(HCatInputFormat.class);
job.setMapOutputKeyClass(IntPair.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DefaultHCatRecord.class);
job.setOutputFormatClass(HCatOutputFormat.class);
HCatOutputFormat.setOutput(job, OutputJobInfo.create("airline", "flight_count", null));
HCatSchema s = HCatOutputFormat.getTableSchema(job);
HCatOutputFormat.setSchema(job, s);
return (job.waitForCompletion(true)? 0:1);
}
public static void main(String[] args) throws Exception{
int exitCode = ToolRunner.run(new onTimeDriver(), args);
System.exit(exitCode);
}
}
create table airline.flight_count(Year INT ,Month INT ,flightCount INT)ROW FORMAT DELIMITED FIELDS TERMINATED BY ','STORED AS TEXTFILE;
以上就是mr如何使用hcatalog读写hive表,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
本文题目:mr如何使用hcatalog读写hive表
文章分享:http://lswzjz.com/article/giooss.html