Distributed分布式缓存代码

分布式缓存数据:

  1. hdfs dfs -text libin/input/distributedDemo.txt  
  2. hadoop  
  3. hive  
  4. hbase  
  5. kafka  
  6. spark  
  7. stome  
  8. pig  
  9. sqoop  
  10. flume  
  11. elasticsearch  
  12. docker  


输入数据:

  1. hdfs dfs -text libin/input/distributedinput.txt  
  2. hive|safsdf  
  3. asdg|dgjgd  
  4. tryrtyh|Fghfgh  
  5. spark|dfhgfdh  
  6. dfg|rtyt  
  7. storm|fghgh  
  8. spark|ghff  



代码:

  1. package mapreduce.baozi;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.net.URI;  
  7. import java.net.URISyntaxException;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10. import org.apache.hadoop.conf.Configuration;  
  11. import org.apache.hadoop.filecache.DistributedCache;  
  12. import org.apache.hadoop.fs.FileSystem;  
  13. import org.apache.hadoop.fs.Path;  
  14. import org.apache.hadoop.io.LongWritable;  
  15. import org.apache.hadoop.io.Text;  
  16. import org.apache.hadoop.mapreduce.Job;  
  17. import org.apache.hadoop.mapreduce.Mapper;  
  18. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  19. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  20.   
  21. @SuppressWarnings("deprecation")  
  22. public class DistributedDemo {  
  23.     public static void main(String[] arge) throws IOException, ClassNotFoundException, InterruptedException {  
  24.   
  25.         Configuration conf = new Configuration();  
  26.         conf.set("fs.default.name""hdfs://192.168.1.100:9000");  
  27.         FileSystem fs = FileSystem.get(conf);  
  28.         fs.delete(new Path("libin/output/distributedout.txt"));  
  29.   
  30.         conf.set("mapred.job.tracker""192.168.1.100:9001");  
  31.         conf.set("mapred.jar","/home/baozi/blb/distributed.jar");  
  32.         Job job = Job.getInstance(conf, DistributedDemo.class.getSimpleName());  
  33.         DistributedCache.createSymlink(job.getConfiguration());  
  34.         try {  
  35.             // HDFS中的libin/input/distributedDemo.txt为分布式缓存  
  36.             DistributedCache.addCacheFile(new URI("libin/input/distributedDemo.txt"),job.getConfiguration());  
  37.         } catch (URISyntaxException e1) {  
  38.             e1.printStackTrace();  
  39.         }  
  40.         job.setMapperClass(DistributedMaper.class);  
  41.         job.setMapOutputKeyClass(Text.class);  
  42.         job.setMapOutputValueClass(Text.class);  
  43.         FileInputFormat.addInputPath(job, new Path("libin/input/distributedinput.txt"));  
  44.         FileOutputFormat.setOutputPath(job, new Path("libin/output/distributedout.txt"));  
  45.         job.waitForCompletion(true);  
  46.     }  
  47.   
  48.     public static class DistributedMaper extends Mapper<LongWritable, Text, Text, Text> {  
  49.         String[] splitedValue;  
  50.         String info;  
  51.         private List<String> DistributediList = new ArrayList<String>();  
  52.   
  53.         protected void setup(Context context) throws IOException, InterruptedException {  
  54.             try {  
  55.                 Path[] cacheFiles = DistributedCache.getLocalCacheFiles(context.getConfiguration());  
  56.                 if (cacheFiles != null && cacheFiles.length > 0) {  
  57.                     String line;  
  58.                     BufferedReader br = new BufferedReader(new FileReader(cacheFiles[0].toString()));  
  59.                     try {  
  60.                         line = br.readLine();  
  61.                         while ((line = br.readLine()) != null) {  
  62.                             DistributediList.add(line);  
  63.                         }  
  64.                     } finally {  
  65.                         br.close();  
  66.                     }  
  67.                 }  
  68.             } catch (IOException e) {  
  69.                 System.err.println("Exception reading DistributedCache: " + e);  
  70.             }  
  71.         }  
  72.   
  73.         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {  
  74.             try {  
  75.                 splitedValue = value.toString().split("\\|");  
  76.                 info = splitedValue[0];  
  77.                 if (DistributediList.contains(info)) {  
  78.                     context.write(new Text(splitedValue[0]), value);  
  79.                 }  
  80.             } catch (Exception ex) {  
  81.                   
  82.             }  
  83.         }  
  84.     }  
  85. }  


运行:

  1. hadoop jar distributed.jar  
  2. 15/10/19 09:18:13 INFO Configuration.deprecation: fs.default.name is deprecated. Instead, use fs.defaultFS  
  3. 15/10/19 09:18:15 INFO Configuration.deprecation: mapred.job.tracker is deprecated. Instead, use mapreduce.jobtracker.address  
  4. 15/10/19 09:18:15 INFO Configuration.deprecation: mapred.jar is deprecated. Instead, use mapreduce.job.jar  
  5. 15/10/19 09:18:15 INFO client.RMProxy: Connecting to ResourceManager at sh-rslog1/27.115.29.102:8032  
  6. 15/10/19 09:18:15 WARN mapreduce.JobSubmitter: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.  
  7. 15/10/19 09:18:16 INFO input.FileInputFormat: Total input paths to process : 1  
  8. 15/10/19 09:18:16 INFO mapreduce.JobSubmitter: number of splits:1  
  9. 15/10/19 09:18:16 INFO Configuration.deprecation: fs.default.name is deprecated. Instead, use fs.defaultFS  
  10. 15/10/19 09:18:16 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1435558921826_11703  
  11. 15/10/19 09:18:16 INFO impl.YarnClientImpl: Submitted application application_1435558921826_11703  
  12. 15/10/19 09:18:16 INFO mapreduce.Job: The url to track the job: http://sh-rslog1:8088/proxy/application_1435558921826_11703/  
  13. 15/10/19 09:18:16 INFO mapreduce.Job: Running job: job_1435558921826_11703  
  14. 15/10/19 09:18:24 INFO mapreduce.Job: Job job_1435558921826_11703 running in uber mode : false  
  15. 15/10/19 09:18:24 INFO mapreduce.Job:  map 0% reduce 0%  
  16. 15/10/19 09:18:32 INFO mapreduce.Job:  map 100% reduce 0%  
  17. 15/10/19 09:18:40 INFO mapreduce.Job:  map 100% reduce 100%  
  18. 15/10/19 09:18:40 INFO mapreduce.Job: Job job_1435558921826_11703 completed successfully  
  19. 15/10/19 09:18:40 INFO mapreduce.Job: Counters: 49  
  20.         File System Counters  
  21.                 FILE: Number of bytes read=68  
  22.                 FILE: Number of bytes written=213001  
  23.                 FILE: Number of read operations=0  
  24.                 FILE: Number of large read operations=0  
  25.                 FILE: Number of write operations=0  
  26.                 HDFS: Number of bytes read=220  
  27.                 HDFS: Number of bytes written=54  
  28.                 HDFS: Number of read operations=6  
  29.                 HDFS: Number of large read operations=0  
  30.                 HDFS: Number of write operations=2  
  31.         Job Counters   
  32.                 Launched map tasks=1  
  33.                 Launched reduce tasks=1  
  34.                 Data-local map tasks=1  
  35.                 Total time spent by all maps in occupied slots (ms)=5410  
  36.                 Total time spent by all reduces in occupied slots (ms)=12318  
  37.                 Total time spent by all map tasks (ms)=5410  
  38.                 Total time spent by all reduce tasks (ms)=6159  
  39.                 Total vcore-seconds taken by all map tasks=5410  
  40.                 Total vcore-seconds taken by all reduce tasks=6159  
  41.                 Total megabyte-seconds taken by all map tasks=8309760  
  42.                 Total megabyte-seconds taken by all reduce tasks=15767040  
  43.         Map-Reduce Framework  
  44.                 Map input records=7  
  45.                 Map output records=3  
  46.                 Map output bytes=54  
  47.                 Map output materialized bytes=60  
  48.                 Input split bytes=136  
  49.                 Combine input records=0  
  50.                 Combine output records=0  
  51.                 Reduce input groups=2  
  52.                 Reduce shuffle bytes=60  
  53.                 Reduce input records=3  
  54.                 Reduce output records=3  
  55.                 Spilled Records=6  
  56.                 Shuffled Maps =1  
  57.                 Failed Shuffles=0  
  58.                 Merged Map outputs=1  
  59.                 GC time elapsed (ms)=58  
  60.                 CPU time spent (ms)=4200  
  61.                 Physical memory (bytes) snapshot=1125724160  
  62.                 Virtual memory (bytes) snapshot=5105754112  
  63.                 Total committed heap usage (bytes)=2022703104  
  64.         Shuffle Errors  
  65.                 BAD_ID=0  
  66.                 CONNECTION=0  
  67.                 IO_ERROR=0  
  68.                 WRONG_LENGTH=0  
  69.                 WRONG_MAP=0  
  70.                 WRONG_REDUCE=0  
  71.         File Input Format Counters   
  72.                 Bytes Read=84  
  73.         File Output Format Counters   
  74.                 Bytes Written=54  


查看结果:

  1. hdfs dfs -text libin/output/distributedout.txt/*  
  2. hive    hive|safsdf  
  3. spark   spark|ghff  
  4. spark   spark|dfhgfdh 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值