您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Hadoop如何實(shí)現(xiàn)HelloWorld的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
目的:將輸入文件的中的Hello,World輸出到文件為World Hello.
輸入文件內(nèi)容:
代碼實(shí)例:
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; 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; /** * HelloWorld Job * 將輸入文件中的Hello,World, 以World Hello輸出到文件 */ public class HelloWorld { /** * 映射器 * 用于將我們的數(shù)據(jù)進(jìn)行預(yù)處理 */ private static class MyMapper extends Mapper<LongWritable, Text, Text, Text>{ @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { System.out.println("mapper running..."); System.out.println("key=" + key.get()); System.out.println("value=" + value.toString()); String[] strValue = value.toString().split(","); context.write(new Text(strValue[1]), new Text(strValue[0])); } } /** * 處理器 * 用于將mapper預(yù)處理的數(shù)據(jù)記錄進(jìn)行業(yè)務(wù)計(jì)算,然后輸出 */ private static class MyReducer extends Reducer<Text, Text, Text, Text>{ @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { System.out.println("reducer running..."); System.out.println("key=" + key.toString()); String val = values.iterator().next().toString(); System.out.println("value=" + val); context.write(key, new Text(val)); } } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration configuration = new Configuration(); Job job = new Job(configuration, "helloworld_job"); job.setJarByClass(HelloWorld.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path("hdfs://hadoopmaster:9000/in/helloworld.txt")); String outFileExt = "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoopmaster:9000/out/helloworld"+outFileExt)); System.out.println(job.waitForCompletion(true)); } }
將代碼打包, 拷貝到hadoopmaster上:
執(zhí)行jar包:
hadoop jar helloworld.jar
得到輸出文件:
感謝各位的閱讀!關(guān)于“Hadoop如何實(shí)現(xiàn)HelloWorld”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。