eclipse 集成hadoop开发环境
操作系统:MacOs 10.12
hadoop集群版本:hadoop-1.2.0版本
hadoop集成eclipse插件:hadoop-eclipse-plugin-1.2.0.jar
eclipse版本:Eclipse Mars.2 Release (4.5.2)
下面讲述eclipse搭建hadoop集群的详细步骤:(由于hadoop集群部署在其他机器上,故此处需要远程连接hadoop集群)
在eclipse软件plugins(插件目录) 目录下hadoop-eclipse-plugin-1.2.0.jar(eclipse集成hadoop插件jar文件)
设置开发环境HADOOP_HOME (在Mac终端上进行)
export HADOOP_HOME=/Users/liumengxi/Documents/Soft/hadoop-1.2.1/
export PATH=$PATH:$HADOOP_HOME/bin
选择Map/Reduce窗口右键新建map/reduce工程,创建Map/Reduce
上述配置项稍加解释:
Location name 工程名字,随便起
Map/Reduce Master 对应hadoop集群下的mapred-site.xml文件中的配置 此处的host为集群master的ip地址为:192.168.0.188 端口号为:9001
DFS Master 对应hadoop集群下的core-site.xml文件中的配置 此处的端口号为:9000
User name 此处填写的是 远程连接的主机名 此处为:yao(有一点需要强调:如果需要远程连接hadoop集群,mac上的用户名必须和hadoop集群的用户名保持一致,否则的话,就会出现权限问题。LZ的mac用户名和hadoop集群并没有保持一致,但是还是可以对hadoop集群进行操作,是因为有以下配置 )。
由于在hadoop集群下的mapred-site.xml文件中配置
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
上述配置hadoop集群关闭权限的认证。
如果正式发布时,远程连接的用户必须与hadoop集群用户保持一致,即可不用修改master的权限认证策略(dfs.permissions)。
新建一个Map/Reduce程序(LZ在这里引入一个统计网站访问时间以及IP地址的例子)。
import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class Test_1 extends Configured implements Tool{ enum Counter{ LINESKIP, } public static class Map extends Mapper<LongWritable,Text,NullWritable,Text>{ public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{ String line = value.toString(); try { String [] lineSplit = line.split(" " ); String month = lineSplit[0 ]; String time = lineSplit[1 ]; String mac = lineSplit[6 ]; Text out = new Text(month + ' ' + time + ' ' + mac); context.write( NullWritable.get(), out); } catch ( java.lang.ArrayIndexOutOfBoundsException e ) { context.getCounter(Counter.LINESKIP).increment(1 ); return ; } } } @Override public int run(String[] args) throws Exception {Configuration conf = getConf(); Job job = new Job(conf, "Test_1" ); job.setJarByClass(Test_1.class ); FileInputFormat.addInputPath( job, new Path(args[ 0 ]) ); FileOutputFormat.setOutputPath( job, new Path(args[ 1 ]) ); job.setMapperClass( Map.class ); job.setOutputFormatClass( TextOutputFormat.class ); job.setOutputKeyClass( NullWritable.class ); job.setOutputValueClass( Text.class ); job.waitForCompletion(true ); System.out.println( "任务名称:" + job.getJobName() ); System.out.println( "任务成功:" + ( job.isSuccessful()? "是" : "否" ) ); return job.isSuccessful() ? 0 : 1 ; } public static void main(String[] args) throws Exception { if ( args.length != 2 ) { System.err.println("" ); System.err.println("Usage: Test_1 < input path > < output path > " ); System.err.println("Counter:" ); System.err.println("\t" + "LINESKIP" + "\t" + "Lines which are too short" ); System.exit(-1 ); } DateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); Date start = new Date(); int res = ToolRunner.run( new Configuration(), new Test_1(), args); Date end = new Date(); float time = ( float ) (( end.getTime() - start.getTime() ) / 60000.0 ) ; System.out.println( "任务开始:" + formatter.format(start) ); System.out.println( "任务结束:" + formatter.format(end) ); System.out.println( "任务耗时:" + String.valueOf( time ) + " 分钟" ); System.exit(res); } }
点击run configurations按钮,新建运行任务,配置运行参数等。
map/reduce运行后产生的输出文件
至此,整个eclipse集成hadoop集群的过程以及开发第一个map/reduce程序全部完成。