设置sudo权限
su
vi /etc/sudoers
在root ALL=(ALL) ALL
增加一行
用户名 ALL=(ALL) ALL
ssh免密码登录
ssh localhost
cd ~/.ssh/
ssh-keygen -t rsa
一直回车即可
cat id_rsa.pub >> authorized_keys
chmod 600 ./authorized_keys 权限问题
jdk安装
tar -zxvf jdk-8u77-linux-x64.tar.gz
sudo vi /etc/profile
export JAVA_HOME=/home/yushengyun/jdk1.8.0_77
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
java -version
hadoop安装
tar -zxvf hadoop-2.6.2.tar.gz
sudo vi /etc/profile
Hadoop Environment Variables
export HADOOP_HOME=/usr/local/hadoop
export HADOOP_INSTALL=$HADOOP_HOME
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export YARN_HOME=$HADOOP_HOME
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin
source /etc/profile
core-site.xml
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<value>file:/home/yushengyun/hadoop-2.6.2/tmp</value>
<description>Abase for other temporary directories.</description>
</property>
<property>
<name>fs.defaultFS</name>
<value>hdfs://localhost:9000</value>
</property>
</configuration>
hdfs-site.xml
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>file:/home/yushengyun/hadoop-2.6.2/tmp/dfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>file:/home/yushengyun/hadoop-2.6.2/tmp/dfs/data</value>
</property>
</configuration>
hadoop namenode -format
然后我们启动hadoop
进入hadoop文件夹
执行./sbin/start-dfs.sh
jps
在用户文件夹下创建
mkdir word_count
mkdir word_input
我们创建等会使用单词计数的文件
cd word_input/
vi word.txt
javac -classpath /home/yushengyun/hadoop-2.6.2/share/hadoop/common/hadoop-common-2.6.2.jar:/home/yushengyun/hadoop-2.6.2/share/hadoop/common/lib/commons-cli-1.2.jar:/home/yushengyun/hadoop-2.6.2/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.6.2.jar -d /home/yushengyun/word_count// WordCount.java
jar -cvf wordcount.jar *.class
hadoop fs -mkdir word_input
hadoop fs -put word_input/word.txt word_input/
hadoop jar word_count/wordcount.jar WordCount word_input word_output
hadoop fs -ls word_output
hadoop fs -cat word_output/part-r-00000
java程序
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class WordCount {
public static class WordCountMap extends
Mapper<LongWritable, Text, Text, IntWritable> {
private final IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer token = new StringTokenizer(line);
while (token.hasMoreTokens()) {
word.set(token.nextToken());
context.write(word, one);
}
}
}
public static class WordCountReduce extends
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(WordCount.class);
job.setJobName("wordcount");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(WordCountMap.class);
job.setReducerClass(WordCountReduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
}