首先希望能够把本地的一些资源能够上传到hdfs当中,利用伪分布的格式分析工程的运行情况:执行put操作的时候出错
hadoop报错:could only be replicated to 0 nodes, instead of 1
出现错误之后利用jps查看已经启动的节点
但是发现datanode没有启动,因此stop-all所有的节点,重新格式化namenode节点,然后重新进行启动就可以了
首先介绍hdfs中常用的一些命令
hadoop fs -mkdir /user/trunk
hadoop fs -ls /user
hadoop fs -lsr /user (递归的)
hadoop fs -put test.txt /user/trunk
hadoop fs -put test.txt . (复制到hdfs当前目录下,首先要创建当前目录)
hadoop fs -get /user/trunk/test.txt . (复制到本地当前目录下)
hadoop fs -cat /user/trunk/test.txt
hadoop fs -tail /user/trunk/test.txt (查看最后1000字节)
hadoop fs -rm /user/trunk/test.txt
hadoop fs -help ls (查看ls命令的帮助文档)…….
感觉其实和一些常规的linux下的命令是差不多的
一 拷贝数据
hadoop fs -mkdir /user
hadoop fs -mkdir /user/trunck
创建新的hdfs上的文件目录
将本地的文件拷贝到其中
hadoop fs -put ~/Documents/wrong.txt /user/trunck
之后尝试用伪分布的结构跑一下wordcount的程序
可以看一下当前对应的目录下已经有这个文件了
hadoop fs -ls /user/trunck
二 新建一个maven的工程
利用maven工程在工程配置中的一些依赖项等比较好处理
可以利用在命令行中进行声明的方式
mvn archetype:create -DgroupId=com.demo.maven -DartifactId=mavenDemo
也可以通过在eclipse下配置完成了maven插件之后进行创建
创建完成后看到的结构是:
修改pom.xml中增加工程对hadoop的依赖情况
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testHadoop</groupId>
<artifactId>testHadoop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testHadoop</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
经过刷新之后可以看到在dependences中会出现下载好的jar依赖包
存储在/Ursers/usrname/.m2/repository当中
修改文中的代码
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
public class WordCount {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, 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<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.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);
}
}
依旧是一个统计词频的代码,和上一篇当中没有太大的变化
在maven工程建立之后,是可以直接通过命令创建对应的jar文件,分别是
mvn clean
mvn install
mvn package
在这里打包代码的输入是通过传递参数输入的
在命令行中执行
bash-3.2$ hadoop jar ~/project/testHadoop/target/testHadoop-0.0.1-SNAPSHOT.jar com.testHadoop.testHadoop.WordCount /user/trunck/wrong.txt /user/trunck/output
首先是打包的jar文件,然后是需要执行的类的结构,之后是输入的文件和输出的文件目录,输出的文件目录会自己进行新建
之后会看到输出:
输出文件中的值
三 输入多个文件
首先进行多次拷贝在一个目录下
那么整体计算出来的数据就是3倍了
计算出来的结果: