In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how Hadoop implements HelloWorld. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Purpose: to export the Hello,World in the input file to the file as World Hello.
Enter the contents of the file:
Code example:
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 * will input the Hello,World in the file Output as World Hello to file * / public class HelloWorld {/ * mapper * to preprocess our data * / 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])) }} / * processor * is used for business calculation of data records preprocessed by mapper. Then output * / 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));}}
Package the code and copy it to hadoopmaster:
Execute the jar package:
Hadoop jar helloworld.jar
Get the output file:
Thank you for reading! This is the end of the article on "how to achieve HelloWorld in Hadoop". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.