2014-10-06 14 views
9

Bir giriş dosyası okuyabilen ve çıktıyı başka bir metin dosyasına yazabilen bir MapReduce programı yazmaya çalışıyorum. Bunun için BufferedReader sınıfını kullanmayı planlıyorum. Ama bunu bir MapReduce programında nasıl kullanacağımı bilmiyorum.Hadoop Haritası Reduce text dosyasını okuyun

Birisi bana bir kod parçacığı verebilir mi?

P.S. Hadoop ve MapReduce programlarına tamamen yeniyim. Yani lütfen benimle kal.

Önceden teşekkür ederiz. Kodun altında

+0

Şimdiye kadar ne denediniz? Ve ne yapmaya çalıştığınız. lütfen açık ol. Kullanıcıdan bir girdi dosyası okumak ve çıktıyı HDFS'ye yazmak ister misiniz? Biraz daha açıklayabilir misin? –

+0

@SreeVeni Ok burada. BufferedReader kullanarak bir metin dosyası okumak istiyorum. Ve çıkışı HDFS'ye (tercihen) yazmak veya çıktıyı başka bir metin dosyasına yazmak istiyorum. Henüz hiç bir şey denemedim. Ama temelde yapmak istediğim şey bu. Örnek kod snippet'leri için internete baktım ama soruma cevap veremedim. Bana yardım edebilir misin? – user2201650

+0

Bir kez daha açıklama HDFS'den metin dosyalarını okumak ister misiniz? –

cevap

7

HDF'ler bir dosyayı okumak ve konsolun

import java.io.BufferedReader; 
import java.io.InputStreamReader; 

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.FileSystem; 
import org.apache.hadoop.fs.Path; 

public class Cat{ 
    public static void main (String [] args) throws Exception{ 
     try{ 
      Path pt=new Path("hdfs:/path/to/file");//Location of file in HDFS 
      FileSystem fs = FileSystem.get(new Configuration()); 
      BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt))); 
      String line; 
      line=br.readLine(); 
      while (line != null){ 
       System.out.println(line); 
       line=br.readLine(); 
      } 
     }catch(Exception e){ 
     } 
    } 
} 

DÜZENLEME

Sürücü

public class ReadFile { 

    public static void main(String[] args) throws Exception { 
     Configuration conf = new Configuration(); 
     Job job = new Job(conf, "Read a File"); 


     FileSystem fs = FileSystem.get(conf); 
     job.setOutputKeyClass(Text.class); 
     job.setOutputValueClass(IntWritable.class); 
     if (fs.exists(new Path(args[1]))) 
      fs.delete(new Path(args[1]), true); 
     job.setMapperClass(Map.class); 
     job.setReducerClass(Reduce.class); 

     job.setInputFormatClass(TextInputFormat.class); 
     job.setOutputFormatClass(TextOutputFormat.class); 

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

} 

Mapper

public class Map extends Mapper<LongWritable, Text, Text, IntWritable> { 

    public void setup(Context context) throws IOException{ 
     Path pt=new Path("hdfs:/path/to/file");//Location of file in HDFS 
     FileSystem fs = FileSystem.get(new Configuration()); 
     BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt))); 
     String line; 
     line=br.readLine(); 
     while (line != null){ 
      System.out.println(line); 
      line=br.readLine(); 
     } 
    } 
    public void map(LongWritable key, Text value, Context context) 
      throws IOException, InterruptedException { 
     //as your wish 
     } 
    } 
} 
içeriği görüntülemek için yardımcı olur

Kodun üstünde HDFS'den bir metin dosyası okumanıza yardımcı olur.

+0

ile güncelleştirin Kod parçacığı için teşekkür ederiz. Ama bunu denedim. İstediğim şey harita azaltma stili programında bir metin dosyası okumak. yani, mapper ve redüktör sınıflarını kullanmak. – user2201650

+0

Harita azaltma stili programında bir metin dosyasını kimlerin okuyacağını biliyor musunuz? Çünkü yapmam gereken şey bu. Bana nasıl yapılacağını söylersen çok yardımcı olurdum. Şimdiden teşekkür ederim! – user2201650

+0

@ user2201650: Düzenle –

İlgili konular