当在MAC上搭建好开发环境之后,第一件事肯定是找一个hello world程序练习下。而hadoop世界的hello word程序就是下面的这个Word Count程序。 1. 新建项目 步骤:FileNewOtherMap/Reduce Project 项目名可以随便取,如MapReduceSample。然后新建类WordCount.ja
当在MAC上搭建好开发环境之后,第一件事肯定是找一个hello world程序练习下。而hadoop世界的hello word程序就是下面的这个Word Count程序。
1. 新建项目
步骤:File–>New–>Other–>Map/Reduce Project
项目名可以随便取,如MapReduceSample。然后新建类WordCount.java,其代码如下:
package com.lifeware.test;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class WordCount {
public static class Map extends MapReduceBase implements Mapper{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
? ? ? ? public void map(LongWritable key, Text value, OutputCollectoroutput, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}
public static class Reduce extends MapReduceBase implements Reducer{
public void reduce(Text key, Iteratorvalues, OutputCollector output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
JobConf conf = new JobConf(WordCount.class);
conf.setJobName(“wordcount”);
? ?conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
? ?conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
? ?FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
? ?JobClient.runJob(conf);
}
}
2. 数据准备
为了运行程序,我们分别需要一个输入和输出的文件夹。输出文件夹,在程序运行完成后会自动生成。我们需要给程序传人一个输入文件夹。
2.1. ?准备本地文件
在当前项目目录下新建文件夹input,并在文件夹下新建两个文件file1、file2,这两个文件内容分别如下:
?file1: ? ?Hello World Bye World
file2: ? ? ?Hello Hadoop Goodbye Hadoop
2.2. 将文件夹input上传到分布式文件系统中?
在已经启动Hadoop守护进程终端中cd 到hadoop安装目录,运行下面命令:
bin/hadoop fs -put ../test/input input
将input文件夹上传到了hadoop文件系统后,在该系统下就多了一个input文件夹,你可以使用下面命令查看:
bin/hadoop fs -ls
或者直接通过Eclipse插件,查看DFS Locations显示:
3. 运行项目
3.1. ?在新建的项目MapReduceSample,点击WordCount.java,右键–>Run As–>Run Configurations
3.2. 在弹出的Run Configurations对话框中,点Java Application,右键–>New,这时会新建一个application名为WordCount
3.3. ?配置运行参数,点Arguments,在Program arguments中输入“你要传给程序的输入文件夹和你要求程序将计算结果保存的文件夹”,如:
hdfs://localhost:9000/user/metaboy/input hdfs://localhost:9000/user/metaboy/output
这里面的input就是你刚传上去文件夹。文件夹地址你可以根据自己具体情况填写。
4.运行程序
点击Run,运行程序,过段时间将运行完成,等运行结束后,可以在终端中用命令:
? ? ?bin/hadoop fs -ls
或者使用插件hadoop eclipse插件查看是否生成文件夹output。
?5. 结果查看
用下面命令查看生成的文件内容:
? ? bin/hadoop fs -cat output/*
运行完这个程序之后,基本上就算是步入到Hadoop这个大家族啦!
原文地址:第一个Map/Reduce程序, 感谢原作者分享。

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor
