一道hadoop面试题

这题是网上找的,如果做的不对,请大家指正。

1 使用Hive或者自定义MR实现如下逻辑
product_no      lac_id  moment  start_time      user_id county_id       staytime        city_id
13429100031     22554   8       2013-03-11 08:55:19.151754088   571     571     282     571
13429100082     22540   8       2013-03-11 08:58:20.152622488   571     571     270     571
13429100082     22691   8       2013-03-11 08:56:37.149593624   571     571     103     571
13429100087     22705   8       2013-03-11 08:56:51.139539816   571     571     220     571
13429100087     22540   8       2013-03-11 08:55:45.150276800   571     571     66      571
13429100082     22540   8       2013-03-11 08:55:38.140225200   571     571     133     571
13429100140     26642   9       2013-03-11 09:02:19.151754088   571     571     18      571
13429100082     22691   8       2013-03-11 08:57:32.151754088   571     571     287     571
13429100189     22558   8       2013-03-11 08:56:24.139539816   571     571     48      571
13429100349     22503   8       2013-03-11 08:54:30.152622440   571     571     211     571
字段解释:
product_no:用户手机号;
lac_id:用户所在基站;
start_time:用户在此基站的开始时间;
staytime:用户在此基站的逗留时间。

需求描述:
根据lac_id和start_time知道用户当时的位置,根据staytime知道用户各个基站的逗留时长。根据轨迹合并连续基站的staytime。
最终得到每一个用户按时间排序在每一个基站驻留时长

期望输出举例:
13429100082     22540   8       2013-03-11 08:58:20.152622488   571     571     270     571
13429100082     22691   8       2013-03-11 08:56:37.149593624   571     571     390     571
13429100082     22540   8       2013-03-11 08:55:38.140225200   571     571     133     571
13429100087     22705   8       2013-03-11 08:56:51.139539816   571     571     220     571
13429100087     22540   8       2013-03-11 08:55:45.150276800   571     571     66      571

说说我的思路:先按照TextInputFormat进行map,在map函数中再对每一行处理将手机号作为map的outputkey,行内容为outputvalue。在reduce的是按照时间排序。

[java]  view plain copy
  1. package hadoop;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.URI;  
  5. import java.net.URISyntaxException;  
  6. import java.util.ArrayList;  
  7. import java.util.Collections;  
  8. import java.util.Comparator;  
  9. import java.util.List;  
  10.   
  11. import org.apache.hadoop.conf.Configuration;  
  12. import org.apache.hadoop.fs.FSDataOutputStream;  
  13. import org.apache.hadoop.fs.FileSystem;  
  14. import org.apache.hadoop.fs.Path;  
  15. import org.apache.hadoop.io.LongWritable;  
  16. import org.apache.hadoop.io.NullWritable;  
  17. import org.apache.hadoop.io.Text;  
  18. import org.apache.hadoop.mapreduce.Job;  
  19. import org.apache.hadoop.mapreduce.Mapper;  
  20. import org.apache.hadoop.mapreduce.Reducer;  
  21. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  22. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  23.   
  24. public class HadoopTest1   
  25. {  
  26.     public static String split = " +|\t";  //定义一个分隔符,空格和tab都可以  
  27.       
  28.     public static class MyComarator implements Comparator   //由于不是按照整个字符串比较,所以实现一个Comparator接口,按时间来比较  
  29.     {  
  30.         @Override  
  31.         public int compare(Object o1, Object o2)   
  32.         {  
  33.             // TODO Auto-generated method stub  
  34.             String str1 = (String)o1;  
  35.             String str2 = (String)o2;  
  36.               
  37.             String []arr1 = str1.split(split);  
  38.             String []arr2 = str2.split(split);  
  39.               
  40.             return (arr1[3] + arr1[4]).compareTo((arr2[3] + arr2[4]));  
  41.         }  
  42.     }  
  43.       
  44.     public static class MyMapper extends Mapper<LongWritable, Text, Text, Text>  
  45.     {  
  46.         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException  
  47.         {  
  48.             if (key.equals(new LongWritable(0)))  //过滤掉第一行  
  49.             {  
  50.                 return;  
  51.             }  
  52.             String line = value.toString();  
  53.             String[] elements = line.split(split);  
  54.             context.write(new Text(elements[0]), value);  
  55.         }  
  56.     }  
  57.     public static class MyReducer extends Reducer<Text, Text, NullWritable, Text>  
  58.     {  
  59.         public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException  
  60.         {  
  61.             List<String>list = new ArrayList<String>();  
  62.               
  63.             for (Text v : values)  
  64.             {  
  65.                 list.add(v.toString());  
  66.             }  
  67.               
  68.             list.sort(new MyComarator());  
  69.             Collections.reverse(list);  
  70.               
  71.             for (int i =0; i < list.size(); ++i)  
  72.             {  
  73.                 context.write(NullWritable.get(), new Text(list.get(i)));  
  74.             }  
  75.         }  
  76.     }  
  77.       
  78.     public static void main(String[] args)  
  79.     {  
  80.         String HDFS_PATH = "hdfs://master:9000";  
  81.         String INPUT_PATH = "/home/hadoop/hadoop-data/20150721/input";  
  82.         String OUTT_PATH  = "/home/hadoop/hadoop-data/20150721/output";  
  83.           
  84.         try  
  85.         {  
  86.             FileSystem fs = FileSystem.get(new URI(HDFS_PATH), new Configuration());  
  87.             FSDataOutputStream out = fs.create(new Path(HDFS_PATH + INPUT_PATH + "/text"));  
  88.             String text = "product_no    lac_id  moment            start_time          user_id  county_id  staytime  city_id\n"  
  89.                         + "13429100031     22554   8       2013-03-11 08:55:19.151754088   571     571     282     571\n"  
  90.                         + "13429100082     22540   8       2013-03-11 08:58:20.152622488   571     571     270     571\n"  
  91.                         + "13429100082     22691   8       2013-03-11 08:56:37.149593624   571     571     103     571\n"  
  92.                         + "13429100087     22705   8       2013-03-11 08:56:51.139539816   571     571     220     571\n"  
  93.                         + "13429100087     22540   8       2013-03-11 08:55:45.150276800   571     571     66      571\n"  
  94.                         + "13429100082     22540   8       2013-03-11 08:55:38.140225200   571     571     133     571\n"  
  95.                         + "13429100140     26642   9       2013-03-11 09:02:19.151754088   571     571     18      571\n"  
  96.                         + "13429100082     22691   8       2013-03-11 08:57:32.151754088   571     571     287     571\n"  
  97.                         + "13429100189     22558   8       2013-03-11 08:56:24.139539816   571     571     48      571\n"  
  98.                         + "13429100349     22503   8       2013-03-11 08:54:30.152622440   571     571     211     571";  
  99.             out.write(text.getBytes());  
  100.             out.close();  
  101.               
  102.             Job job = new Job(new Configuration(), "HadoopTest1");            
  103.             job.setJarByClass(HadoopTest1.class);  
  104.               
  105.             job.setMapperClass(MyMapper.class);  
  106.             job.setReducerClass(MyReducer.class);  
  107.               
  108.             job.setMapOutputKeyClass(Text.class);  
  109.             job.setMapOutputValueClass(Text.class);  
  110.               
  111.             job.setOutputKeyClass(NullWritable.class);  
  112.             job.setOutputValueClass(Text.class);  
  113.                       
  114.             if (fs.exists(new Path(HDFS_PATH + OUTT_PATH))) //删除已有的输出文件  
  115.             {  
  116.                 fs.delete(new Path(HDFS_PATH + OUTT_PATH), true);  
  117.             }  
  118.   
  119.             TextInputFormat.addInputPath(job, new Path(HDFS_PATH + INPUT_PATH));  
  120.             FileOutputFormat.setOutputPath(job, new Path(HDFS_PATH + OUTT_PATH));  
  121.               
  122.             job.waitForCompletion(true);  
  123.               
  124.         }  
  125.         catch (URISyntaxException e)  
  126.         {  
  127.             e.printStackTrace();  
  128.         }  
  129.         catch (IOException e)  
  130.         {  
  131.             e.printStackTrace();  
  132.         }  
  133.         catch (ClassNotFoundException e)  
  134.         {  
  135.             e.printStackTrace();  
  136.         }  
  137.         catch (InterruptedException e)  
  138.         {  
  139.             e.printStackTrace();  
  140.         }  
  141.     }  
  142. }  

最后的输出结果:

[plain]  view plain copy
  1. 13429100031     22554   8       2013-03-11 08:55:19.151754088   571     571     282     571  
  2. 13429100082     22540   8       2013-03-11 08:58:20.152622488   571     571     270     571  
  3. 13429100082     22691   8       2013-03-11 08:57:32.151754088   571     571     287     571  
  4. 13429100082     22691   8       2013-03-11 08:56:37.149593624   571     571     103     571  
  5. 13429100082     22540   8       2013-03-11 08:55:38.140225200   571     571     133     571  
  6. 13429100087     22705   8       2013-03-11 08:56:51.139539816   571     571     220     571  
  7. 13429100087     22540   8       2013-03-11 08:55:45.150276800   571     571     66      571  
  8. 13429100140     26642   9       2013-03-11 09:02:19.151754088   571     571     18      571  
  9. 13429100189     22558   8       2013-03-11 08:56:24.139539816   571     571     48      571  
  10. 13429100349     22503   8       2013-03-11 08:54:30.152622440   571     571     211     571  

如有不对的地方,还请大家指教。


你可能感兴趣的:(hadoop,hive)