首先希望能夠把本地的一些資源能夠上傳到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倍了
計算出來的結果: