当在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'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollationsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

mysqloffersechar,varchar,text,and denumforstringdata.usecharforfixed Lengttrings,varcharerforvariable長度,文本forlarger文本,andenumforenforcingDataAntegrityWithaEtofValues。

優化MySQLBLOB請求可以通過以下策略:1.減少BLOB查詢頻率,使用獨立請求或延遲加載;2.選擇合適的BLOB類型(如TINYBLOB);3.將BLOB數據分離到單獨表中;4.在應用層壓縮BLOB數據;5.對BLOB元數據建立索引。這些方法結合實際應用中的監控、緩存和數據分片,可以有效提升性能。

掌握添加MySQL用戶的方法對於數據庫管理員和開發者至關重要,因為它確保數據庫的安全性和訪問控制。 1)使用CREATEUSER命令創建新用戶,2)通過GRANT命令分配權限,3)使用FLUSHPRIVILEGES確保權限生效,4)定期審計和清理用戶賬戶以維護性能和安全。

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

在MySQL中處理字符串數據類型和索引的最佳實踐包括:1)選擇合適的字符串類型,如CHAR用於固定長度,VARCHAR用於可變長度,TEXT用於大文本;2)謹慎索引,避免過度索引,針對常用查詢創建索引;3)使用前綴索引和全文索引優化長字符串搜索;4)定期監控和優化索引,保持索引小巧高效。通過這些方法,可以在讀取和寫入性能之間取得平衡,提升數據庫效率。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器