新聞中心
這篇文章給大家分享的是有關(guān)Hadoop如何實(shí)現(xiàn)HelloWorld的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁(yè)設(shè)計(jì)師等,應(yīng)用各種網(wǎng)絡(luò)程序開(kāi)發(fā)技術(shù)和網(wǎng)頁(yè)設(shè)計(jì)技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)專業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站制作(企業(yè)站、成都響應(yīng)式網(wǎng)站建設(shè)、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗(yàn)的提升,我們力求做到極致!
目的:將輸入文件的中的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{ @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 { @Override protected void reduce(Text key, Iterable 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ò),可以把它分享出去讓更多的人看到吧!
文章標(biāo)題:Hadoop如何實(shí)現(xiàn)HelloWorld
分享鏈接:http://www.ef60e0e.cn/article/jheigd.html