用hadoop也算有一段时间了,一直没有注意过hadoop运行过程中,产生的数据日志,比如说System打印的日志,或者是log4j,slf4j等记录的日志,存放在哪里,日志信息的重要性,在这里散仙就不用多说了,调试任何程序基本上都得需要分析日志。
hadoop的日志主要是MapReduce程序,运行过程中,产生的一些数据日志,除了系统的日志外,还包含一些我们自己在测试时候,或者线上环境输出的日志,这部分日志通常会被放在userlogs这个文件夹下面,我们可以在mapred-site.xml里面配置运行日志的输出目录,散仙测试文件内容如下:
- <?xmlversion="1.0"?>
- <?xml-stylesheettype="text/xsl"href="configuration.xsl"?>
- <!--Putsite-specificpropertyoverridesinthisfile.-->
- <configuration>
- <!--jobtracker的master地址-->
- <property>
- <name>mapred.job.tracker</name>
- <value>192.168.75.130:9001</value>
- </property>
- <property>
- <!--hadoop的日志输出指定目录-->
- <name>mapred.local.dir</name>
- <value>/root/hadoop1.2/mylogs</value>
- </property>
- </configuration>
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Put site-specific property overrides in this file. --> <configuration> <!-- jobtracker的master地址--> <property> <name>mapred.job.tracker</name> <value>192.168.75.130:9001</value> </property> <property> <!-- hadoop的日志输出指定目录--> <name>mapred.local.dir</name> <value>/root/hadoop1.2/mylogs</value> </property> </configuration>
配置好,日志目录后,我们就可以把这个配置文件,分发到各个节点上,然后启动hadoop。
下面我们看来下在eclipse环境中如何调试,散仙在setup,map和reduce方法中,分别使用System打印了一些数据,当我们使用local方式跑MR程序时候,日志并不会被记录下来,而是直接会在控制台打印,散仙的测试代码如下:
- packagecom.qin.testdistributed;
- importjava.io.File;
- importjava.io.FileReader;
- importjava.io.IOException;
- importjava.net.URI;
- importjava.util.Scanner;
- importorg.apache.hadoop.conf.Configuration;
- importorg.apache.hadoop.filecache.DistributedCache;
- importorg.apache.hadoop.fs.FSDataInputStream;
- importorg.apache.hadoop.fs.FileSystem;
- importorg.apache.hadoop.fs.Path;
- importorg.apache.hadoop.io.IntWritable;
- importorg.apache.hadoop.io.LongWritable;
- importorg.apache.hadoop.io.Text;
- importorg.apache.hadoop.mapred.JobConf;
- importorg.apache.hadoop.mapreduce.Job;
- importorg.apache.hadoop.mapreduce.Mapper;
- importorg.apache.hadoop.mapreduce.Reducer;
- importorg.apache.hadoop.mapreduce.lib.db.DBConfiguration;
- importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- importorg.apache.log4j.pattern.LogEvent;
- importorg.slf4j.Logger;
- importorg.slf4j.LoggerFactory;
- importcom.qin.operadb.WriteMapDB;
- /**
- *测试hadoop的全局共享文件
- *使用DistributedCached
- *
- *大数据技术交流群:37693216
- *@authorqindongliang
- *
- ****/
- publicclassTestDistributed{
- privatestaticLoggerlogger=LoggerFactory.getLogger(TestDistributed.class);
- privatestaticclassFileMapperextendsMapper<LongWritable,Text,IntWritable>{
- Pathpath[]=null;
- /**
- *Map函数前调用
- *
- **/
- @Override
- protectedvoidsetup(Contextcontext)
- throwsIOException,InterruptedException{
- logger.info("开始启动setup了哈哈哈哈");
- //System.out.println("运行了.........");
- Configurationconf=context.getConfiguration();
- path=DistributedCache.getLocalCacheFiles(conf);
- System.out.println("获取的路径是:"+path[0].toString());
- //FileSystemfs=FileSystem.get(conf);
- FileSystemfsopen=FileSystem.getLocal(conf);
- //FSDataInputStreamin=fsopen.open(path[0]);
- //System.out.println(in.readLine());
- //for(PathtmpRefPath:path){
- //if(tmpRefPath.toString().indexOf("ref.png")!=-1){
- //in=reffs.open(tmpRefPath);
- //break;
- //}
- //}
- //FileReaderreader=newFileReader("file://"+path[0].toString());
- //Filef=newFile("file://"+path[0].toString());
- //FSDataInputStreamin=fs.open(newPath(path[0].toString()));
- //Scannerscan=newScanner(in);
- //while(scan.hasNext()){
- //System.out.println(Thread.currentThread().getName()+"扫描的内容:"+scan.next());
- //}
- //scan.close();
- //
- //System.out.println("size:"+path.length);
- }
- @Override
- protectedvoidmap(LongWritablekey,Textvalue,Contextcontext)
- throwsIOException,InterruptedException{
- //System.out.println("mapaaa");
- //logger.info("Map里的任务");
- System.out.println("map里输出了");
- //logger.info();
- context.write(newText(""),newIntWritable(0));
- }
- @Override
- protectedvoidcleanup(Contextcontext)
- throwsIOException,InterruptedException{
- logger.info("清空任务了。。。。。。");
- }
- }
- privatestaticclassFileReduceextendsReducer<Object,Object,Object>{
- @Override
- protectedvoidreduce(Objectarg0,Iterable<Object>arg1,
- Contextarg2)throwsIOException,InterruptedException{
- System.out.println("我是reduce里面的东西");
- }
- }
- publicstaticvoidmain(String[]args)throwsException{
- JobConfconf=newJobConf(TestDistributed.class);
- //conf.set("mapred.local.dir","/root/hadoop");
- //Configurationconf=newConfiguration();
- //conf.set("mapred.job.tracker","192.168.75.130:9001");
- //读取person中的数据字段
- //conf.setJar("tt.jar");
- //注意这行代码放在最前面,进行初始化,否则会报
- StringinputPath="hdfs://192.168.75.130:9000/root/input";
- StringoutputPath="hdfs://192.168.75.130:9000/root/outputsort";
- Jobjob=newJob(conf,"a");
- DistributedCache.addCacheFile(newURI("hdfs://192.168.75.130:9000/root/input/f1.txt"),job.getConfiguration());
- job.setJarByClass(TestDistributed.class);
- System.out.println("运行模式:"+conf.get("mapred.job.tracker"));
- /**设置输出表的的信息第一个参数是job任务,第二个参数是表名,第三个参数字段项**/
- FileSystemfs=FileSystem.get(job.getConfiguration());
- Pathpout=newPath(outputPath);
- if(fs.exists(pout)){
- fs.delete(pout,true);
- System.out.println("存在此路径,已经删除......");
- }
- /**设置Map类**/
- //job.setOutputKeyClass(Text.class);
- //job.setOutputKeyClass(IntWritable.class);
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(IntWritable.class);
- job.setMapperClass(FileMapper.class);
- job.setReducerClass(FileReduce.class);
- FileInputFormat.setInputPaths(job,newPath(inputPath));//输入路径
- FileOutputFormat.setOutputPath(job,newPath(outputPath));//输出路径
- System.exit(job.waitForCompletion(true)?0:1);
- }
- }