hadoop(一)

1 环境熟悉
安装jdk、hadoop
配置xml文件,启动伪分布式
运行example-jar,测试mapreduce程序
2 mapreduce编程
使用eclipse开发mapreduce程序,导出jar包
注意在eclipse下也可以运行mapreduce程序,相当于单机的hadoop
将数据放入hdfs,运行jar包,得到输出结果
3 安装使用hive和hbase

1 环境熟悉
hadoop提供binary和src两种类型的包。src可以自己定制编译

#登陆服务器
ssh username@IP
#解压、环境变量配置
java中的classpath

#配置xml、开启伪分布式
./sbin/start-hdfs.sh 开启hdfs
JPS是JDK 1.5提供的一个显示当前所有java进程pid的命令.
JPS查看是否已经启动伪分布式,查看是否有namenode等进程

#拷贝jar包到服务器,使用scp
scp myJar.jar username@IP:path
#运行jar包
hadoop jar input output
#拷贝结果到home目录,使用more查看
hdfs dfs -cat output/* > /home/result

简答的测试使用
echo "world my life" > 1.txt
echo "world my wife save me" > 2.txt
hdfs dfs -put input/* input
hdfs dfs -rm -r output
hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar wordcount input output
hdfs dfs -cat output/*
运行结果
me 1
my 2
save 1
wife 1
world 2

hdfs常用命令:
hdfs dfs -rm -r directory 删除目录 -ls directory 输出目录信息
hdfs dfs -put /yourMaterial input 移动文件到hdfs

可以使用webUI查看程序的运行情况,对应的hadoop服务器的IP,端口19888、50030、50070等,可以查看core-site.xml文件获知。

注意:每次再次运行jar,需要删除output文件夹或者更改输出文件夹的位置!

2 mapreduce编程

研读API,了解The Hadoop MapReduce framework
http://hadoop.apache.org/docs/stable/api/overview-summary.html

mapreduce的过程
输入inputformat>map>combiner>partitioner>reducer>outputformat

自定义的键、值的数据类型
实现WritableComparable接口,不需要比较,也可以只实现Writable接口
public class T implements WritableComparable <T>
重写write、readfields、compareTo方法

自定义的输入输出格式
InputFormat和RecordReader
InputFormat>InputSplit
one map task for each InputSplit generated by the InputFormat

OutputFormat和RecordWriter
job.setInputFormatClass(myInputFormat.class);

mapper
covert input key/value pairs to a set of intermediate key/value pairs.
设置自定义的mapper类
job.setMapperClass(myMapper.class);

输出使用
context.write(word, one);
可以通过context获取的参数,context是环境对象参数


combiner
定制Combiner来减少网络数据传输,combiner将mapper产生的中间对进一步合并再传给reducer,注意输入输出的键值对类型要相同。
设置自定义的combiner类(继承自reducer类)
job.setCombinerClass(myReducer.class);

partitioner
改变Map[combiner]中间结果到Reduce节点的分配方式
自定义
Job. setPartitionerClass(NewPartitioner)

reducer
输入:key iterable
由mapper或者combiner产生的键值对,按照key值分发给reducer,同一个key肯定发往同一个reducer,值存在values中,需要使用iterator读取。
设置自定义的reducer类
job.setReducerClass(myReducer.class);

编程
eclipse
其实eclipse不好用,智能提示太少,导入jar包不方便。
准备工作:
在项目lib中设置外部jar。五个文件夹内的jar common common/lib hdfs mapreduce yarn
设置运行时的输入参数
在run中设置run时的参数configuration,设置输入确定input、output,因为在程序中会获取命令行参数作为input、output路径。
导出jar包
export相应的jar包,选择main入口,以使用jar包在分布式的集群环境下运行。


使用intelliJ
在project structure中设置lib、jar导出
在run configuration中设置run时的参数


编写MapReduce程序:一个Map函数,一个Reduce函数,一个主函数


#linux 命令#
man <command> 查看linux命令
ssh username@IP 远程登陆linux服务器
more Press space to continue, 'q' to quit.最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会往回(back)一页显示
tar 打包和解压缩命令,将整个 /etc 目录下的文件全部打包成为 /tmp/etc.tar
tar -cvf /tmp/etc.tar /etc <==仅打包,不压缩!
tar -zcvf /tmp/etc.tar.gz /etc <==打包后,以 gzip 压缩
tar -zxvf 解压命令

管道 使用管道输出文件echo "world my life" > 1.txt

history 罗列历史命令

使用tree命令查看文件目录结构



原文地址:https://www.cnblogs.com/ceo1207/p/6052188.html