当前位置:   article > 正文

myeclipse运行mapreduce报错:Cannot run program "cygpath": CreateProcess error=2,_myeclipse import.java.io.ioeception

myeclipse import.java.io.ioeception

直接上代码:Mapper

  1. package com.winksi.dotstat;
  2. import java.io.IOException;
  3. import org.apache.hadoop.io.Text;
  4. import org.apache.hadoop.io.LongWritable;
  5. import org.apache.hadoop.mapreduce.Mapper;
  6. public class DotProducedMapper extends Mapper<LongWritable, Text, Text, Text>{
  7. @Override
  8. public void map(LongWritable key, Text value, Context context)
  9. throws IOException, InterruptedException{
  10. String line = value.toString();
  11. String fileds[] = line.split(",");
  12. String imsi = fileds[0];
  13. String adccompany = fileds[1];
  14. String phone = fileds[2];
  15. String cur_time = fileds[3];
  16. String call_length = fileds[4];
  17. String call_type = fileds[5];
  18. String show_length = fileds[6];
  19. String pkgname = fileds[7];
  20. String ip = fileds[8];
  21. String model = fileds[9];
  22. String ossdk = fileds[10];
  23. String av = fileds[11];
  24. String cityId = fileds[12];
  25. StringBuffer strKey = new StringBuffer();
  26. strKey.append(model).append(",").append(adccompany).append(",")
  27. .append(ossdk.toUpperCase()).append(",").append(av).append(",")
  28. .append(phone);
  29. context.write(new Text(strKey.toString()), new Text(imsi));
  30. }
  31. }
Reducer:

  1. package com.winksi.dotstat;
  2. import java.io.IOException;
  3. import java.util.Date;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.Set;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Reducer;
  9. public class DotProducedReducer extends Reducer<Text, Text, Text, Text>{
  10. public void reduce(Text key, Iterator<Text> values,
  11. Context context)
  12. throws IOException, InterruptedException {
  13. int count = 0;
  14. Set<String> set = new HashSet<String>();
  15. while (values.hasNext()) {
  16. set.add(values.next().toString());
  17. count++;
  18. }
  19. StringBuffer sb = new StringBuffer();
  20. sb.append(count).append(",").append(set.size());
  21. Date yesterdayDate = new Date(new Date().getTime() - 1*24*60*60*1000);
  22. String yesterday = DotStatPvUv.getCurrentDay(yesterdayDate);
  23. StringBuffer sbKey = new StringBuffer();
  24. sbKey.append(key.toString()).append(",").append(yesterday);
  25. context.write(new Text(sbKey.toString()), new Text(sb.toString()));
  26. }
  27. }
Spring配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/hadoop"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:beans="http://www.springframework.org/schema/beans"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  8. http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
  9. <context:property-placeholder location="classpath:config.properties" />
  10. <configuration>
  11. fs.default.name=${hd.fs}
  12. </configuration>
  13. <job id="dotProductJob"
  14. input-path="${input.path}"
  15. output-path="${output.path}"
  16. mapper="com.winksi.dotstat.DotProducedMapper"
  17. reducer="com.winksi.dotstat.DotProducedReducer"/>
  18. <job-runner id="runner" run-at-startup="true"
  19. job-ref="dotProductJob" />
  20. </beans:beans>
maven的jar包pom.xml

  1. <dependency>
  2. <groupId>org.springframework.data</groupId>
  3. <artifactId>spring-data-hadoop</artifactId>
  4. <version>1.0.2.RELEASE-cdh4</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.hadoop</groupId>
  8. <artifactId>hadoop-core</artifactId>
  9. <version>${hadoop.core.version}</version>
  10. </dependency>
java代码:

public class Test {
    public static void main(String[] args) {
         ApplicationContext ctx = new ClassPathXmlApplicationContext("hadoop-mr.xml");
    }
}

运行报错:

Exception in thread "main" java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
 at org.apache.hadoop.util.Shell.runCommand(Shell.java:201)
 at org.apache.hadoop.util.Shell.run(Shell.java:183)
 at org.apache.hadoop.fs.FileUtil$CygPathCommand.<init>(FileUtil.java:413)
 at org.apache.hadoop.fs.FileUtil.makeShellPath(FileUtil.java:439)
 at org.apache.hadoop.fs.FileUtil.makeShellPath(FileUtil.java:466)
 at org.apache.hadoop.fs.RawLocalFileSystem.execCommand(RawLocalFileSystem.java:559)
 at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:551)
 at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:355)
 at org.apache.hadoop.fs.FilterFileSystem.mkdirs(FilterFileSystem.java:212)
原因是尽管连接的hadoop平台是在linux上面,但是我们的eclipse调试环境是在windows,我们需要安装一个linux模拟器“cygwin”来支持程序的运行。

在windows开发服务器上安装好cygwin,然后在环境变量中添加cygwin的bin目录,比如“D:\Program\cygwin\bin”,问题得以解决。

注意是在系统环境变量的Path上加入“D:\Program\cygwin\bin”这样我们就行像在linux上运行命令一样在windows的dos窗口下执行ls,cd等命令。

刚开始我一直是在系统环境变量下的classpath上加的D:\Program\cygwin\bin”,尝试了半天都是失败,后来才发现,是加错了地方。加完之后再cmd窗口中试一下ls,cd等命令。成功后重启myeclipse运行代码,就没有问题了。

------------------------------------------------------------------------------

之前纠结集成的Mapper和Reducer的问题,有两个文件可以继承,具体问题见:

http://wuyanzan60688.blog.163.com/blog/static/127776163201310164244955/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/276409
推荐阅读
相关标签
  

闽ICP备14008679号