कोड नीचे HDFS से एक फ़ाइल को पढ़ने और सांत्वना
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){
}
}
}
संपादित
ड्राइवर
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);
}
}
मैपर
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
}
}
}
में सामग्री प्रदर्शित करने के लिए आप में मदद करता है
उपरोक्त कोड आपको एचडीएफएस से एक टेक्स्ट फ़ाइल पढ़ने में मदद करता है।
स्रोत
2014-10-06 05:07:39
अब तक आपने जो कोशिश की है? और आप जो कार्यान्वित करने की कोशिश कर रहे हैं। कृपया विशिष्ट रहें। क्या आप उपयोगकर्ता से इनपुट फ़ाइल पढ़ना चाहते हैं और आउटपुट को एचडीएफएस में लिखना चाहते हैं? क्या आप थोड़ा और समझा सकते हैं। –
@ श्रीवीनी ठीक है यह यहाँ है। मैं BufferedReader का उपयोग कर एक टेक्स्ट फ़ाइल पढ़ना चाहता हूँ। और मैं आउटपुट को या तो एचडीएफएस (अधिमानतः) लिखना चाहता हूं या आउटपुट को दूसरी टेक्स्ट फ़ाइल में लिखना चाहता हूं। मैंने अभी तक कुछ भी कोशिश नहीं की है। लेकिन यही वह है जो मैं मूल रूप से करना चाहता हूं। मैंने नमूना कोड स्निपेट के लिए इंटरनेट पर देखा है लेकिन मेरे प्रश्न का उत्तर क्या नहीं मिला। क्या आप यहां मेरी मदद कर सकते हैं? – user2201650
एक और स्पष्टीकरण क्या आप एचडीएफएस से टेक्स्ट फाइल पढ़ना चाहते हैं? –