2015-07-08 1 views
0

Я попытался запустить счетчик слов в eclipse. но с этим что-то не так; его даже нельзя отлаживать.Невозможно выполнить подсчет слов на hadoop.thanks

package test; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.LongWritable; 
import org.apache.hadoop.io.Text; 
import org.apache.hadoop.mapreduce.Job; 
import org.apache.hadoop.mapreduce.Mapper; 
import org.apache.hadoop.mapreduce.Reducer; 
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; 
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; 

import test.test.Map2.Combine; 

public class test { 


public static class Map2 extends Mapper<LongWritable, Text, Text, Text> { 
    public void map(LongWritable key, Text value, Context context) 
      throws IOException, InterruptedException { 
     String line = value.toString(); 
     String values=line.split(" ")[0]+"\\|"+line.split(" ")[1]; 
     context.write(new Text(" "),new Text(values)); 
     } 
    //method reduce start 
    public static class Combine extends Reducer<Text, Text, Text, IntWritable> { 
    ArrayList<String> top5array= new ArrayList<String>(); 
    public void reduce(Text key, Iterable<Text> values, Context context) 
     throws IOException, InterruptedException { 

     //arraylist  
     while(top5array.get(4)==null) 
     { 
      top5array.add(values.iterator().next().toString()); 
     } 


    while(values.iterator().hasNext()) 
    { 
     String currentValues=values.iterator().next().toString(); 
     String currentkey=currentValues.split("\\|")[0]; 
     Integer currentnum=Integer.parseInt(currentValues.split("\\|")[1]); 

     for(int i=0;i<5;i++) 
     { 
     Integer numofArray = Integer.parseInt(top5array.get(i).split("\\|")[1]); 
      if(top5array.get(i) != null && currentnum < numofArray) 
       { 
       break; 
       } 
       if(i == 4) 
         { 
         String currentKeyValuePair = currentkey + currentnum.toString(); 
         top5array.add(5, currentKeyValuePair); 
         Collections.sort(top5array); 
         top5array.remove(0); 
         } 
      }// for end 
     }// while end 
    }//method reduce end 
} // Combine end 
} 
// map end 

public static class Reduce2 extends Reducer<Text, Text, Text, Text> { 
ArrayList<String> top5array= new ArrayList<String>(); 
    public void reduce(Text key, Iterable<Text> values, Context context) 
     throws IOException, InterruptedException { 


     while(top5array.get(4)==null) 
     { 
      top5array.add(values.iterator().next().toString()); 
     } 


    while(values.iterator().hasNext()) 
    { 
     String currentValues=values.iterator().next().toString(); 
     String currentkey=currentValues.split("\\|")[0]; 
     Integer currentnum=Integer.parseInt(currentValues.split("\\|")[1]); 

     for(int i=0;i<5;i++) 
     { 
     Integer numofArray = Integer.parseInt(top5array.get(i).split("\\|")[1]); 
      if(top5array.get(i) != null && currentnum < numofArray) 
       { 
       break; 
       } 
       if(i == 4) 
         { 
         String currentKeyValuePair = currentkey + currentnum.toString(); 
         top5array.add(5, currentKeyValuePair); 
         Collections.sort(top5array); 
         top5array.remove(0); 
         } 
      } 
     } 
     String top5StringConca = ""; 
     for(int i=0; i < 5; i++){ 
     top5StringConca = top5StringConca + top5array.get(i); 
     } 
     context.write(new Text(" "), new Text(top5StringConca)); 
    } 
} 



//the second of mapreduce end 


public static void main(String[] args) throws Exception { 
    Configuration conf = new Configuration(); 
    Job job = Job.getInstance(conf); 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(Text.class); 
    job.setMapperClass(Map2.class); 
    job.setReducerClass(Reduce2.class); 
    job.setCombinerClass(Combine.class); 
    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 

    FileInputFormat.addInputPath(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 
    job.waitForCompletion(true); 

} 

} 

Проблема при запуске он показывает следующее исключение:

WARN [main] util.NativeCodeLoader (NativeCodeLoader.java:<clinit>(62)) 
- Unable to load native-hadoop library for your platform using builtin-java classes where applicable` 

Как я могу решить эту проблему?

+0

Вы используете это на окнах? – Nakul91

+0

Я бегу на ubuntu – qianda66

+0

Я хочу написать, как отлаживать программу mapreduce в eclipse. – qianda66

ответ

0
  1. Добавить свой банкомат в вашем проекте.
  2. Если вы уже настроили hadoop, тогда вы можете указать свои hdfs внутри затмения. Для этого у вас есть зависимости.
  3. Добавить зависимые отношения (если вы используете maven) внутри вашего pom.xml. Также добавьте дополнительный плагин для eclipse. Here - руководство. Они позволят вашему Map-Reduce Perspective в eclipse. Я добавил следующие зависимости внутри моего проекта:

    <dependency> 
        <groupId>org.apache.hadoop</groupId> 
        <artifactId>hadoop-core</artifactId> 
        <version>1.2.1</version> 
        <scope>compile</scope> 
    </dependency> 
    
    <dependency> 
        <groupId>org.apache.hadoop</groupId> 
        <artifactId>hadoop-common</artifactId> 
        <version>2.6.0</version> 
    </dependency> 
    
    <dependency> 
        <groupId>org.apache.hadoop</groupId> 
        <artifactId>hadoop-client</artifactId> 
        <version>2.6.0</version> 
    </dependency> 
    
  4. Вы увидите зависимость сама содержит Hadoop баночку. Теперь это зависит от того, хотите ли вы использовать существующую конфигурацию конфигурации или по умолчанию, предоставляемую банкой.

  5. Теперь попробуйте запустить свой класс водителя хаоса. Вы можете легко отладить код в eclipse. Кроме того, теперь ваша перспектива в отношении доступа включена. Здесь вы можете добавить свой путь hdfs.

Вы также можете проверить this для удаленной отладки.

+0

Спасибо за ваш ответ .but не могу найти pom.xml? Можете ли вы сказать мне, где это, пожалуйста? – qianda66

+0

Тогда вы не используете проект на основе maven. Поиск файлов jar с использованием имени 'artifact-Id' загружает банки и включается в ваш проект. например. 'hadoop-client.jar' – Nakul91

+0

поблагодарить u .i решили мою проблему. – qianda66