hadoop相关
执行wordcount
代码
package org.apache.hadoop.examples; import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer; 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.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat; public class WordCount { public static class Map extends MapReduceBase implements
Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable();
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 = ;
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.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class); conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class); FileInputFormat.setInputPaths(conf, new Path(args[]));
FileOutputFormat.setOutputPath(conf, new Path(args[])); JobClient.runJob(conf);
}
}
首先进行编译:
javac -classpath ./share/hadoop/common/hadoop-common-2.7..jar:./share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7..jar -d WordCount ./WordCount/WordCount.java
然后压包
jar -cvf wordcount.jar org/*
在复制到hadoop的工作目录下
然后在hadoop工作目录下面新建一个input目录 mkdir input,在目录里面新建一个文件vi file1,输入以下内容:
hello world
hello hadoop
hello mapreduce
,把该文件上传到hadoop的分布式文件系统中去
- ./bin/hadoop fs -put input/file* input
(6)然后我们开始执行
- ./bin/hadoop jar wordcount.jar org.apache.hadoop.examples.WordCount input wordcount_output
(7)最后我们查看运行结果
- ./bin/hadoop fs -cat wordcount_output/part-r-00000
参考:
http://cardyn.iteye.com/blog/1356361
https://blog.csdn.net/qichangleixin/article/details/43376587
二.往hdfs写数据
java代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils; import java.io.*;
import java.net.URI; /**
* blog: http://www.iteblog.com/
* Date: 14-1-2
* Time: 下午6:09
*/
public class AppendContent {
public static void main(String[] args) {
String hdfs_path = "input/file1";//文件路径
Configuration conf = new Configuration();
conf.setBoolean("dfs.support.append", true); String inpath = "./append.txt";
FileSystem fs = null;
try {
fs = FileSystem.get(URI.create(hdfs_path), conf);
//要追加的文件流,inpath为文件
InputStream in = new
BufferedInputStream(new FileInputStream(inpath));
OutputStream out = fs.append(new Path(hdfs_path));
IOUtils.copyBytes(in, out, , true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意指定的hdfs路径,用hdfs://localhost:9000/input/路径一直不行,不知道什么原因。
编译
javac -classpath ./share/hadoop/common/hadoop-common-2.7..jar:./share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7..jar -d ./classes ./my_append/AppendContent.java
压包
jar -cvf ./my_jar/append.jar ./classes/*
运行
./bin/hadoop jar ./my_jar/append.jar AppendContent
AppendContent是类的名字
查看
./bin/hdfs dfs -cat input/*
或者代码可以改为通过args传参的方式传入hdfs路径, 方便多进程操作
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream; import java.io.*;
import java.net.URI; /**
* blog: http://www.iteblog.com/
* Date: 14-1-2
* Time: 下午6:09
*/
public class AppendContent {
public static void main(String[] args) {
//String hdfs_path = "input/file1";//文件路径
String hdfs_path = args[];
Configuration conf = new Configuration();
conf.setBoolean("dfs.support.append", true); //String inpath = "./append.txt";
FileSystem fs = null;
try {
fs = FileSystem.get(URI.create(hdfs_path), conf);
FSDataOutputStream out = fs.append(new Path(hdfs_path)); String s="";
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
s+='a';
}
int readLen = s.getBytes().length;
out.write(s.getBytes(), , readLen);
} //int readLen = "0123456789".getBytes().length; //while (-1 != readLen) //out.write("0123456789".getBytes(), 0, readLen); //要追加的文件流,inpath为文件
//InputStream in = new
// BufferedInputStream(new FileInputStream(inpath));
//OutputStream out = fs.append(new Path(hdfs_path));
//IOUtils.copyBytes(in, out, 4096, true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
编译与压包命令同上,执行命令如下
./bin/hadoop jar ./my_jar/append.jar AppendContent input/file1
参考:https://blog.csdn.net/jameshadoop/article/details/24179413
https://blog.csdn.net/wypblog/article/details/17914021
脚本
#!/bin/bash #开始时间
begin=$(date +%s%N) for ((i=; i<;i++))
do
{
./bin/hadoop jar ./my_jar/append.jar AppendContent input/file${i}
}
done wait
#结束时间
end=$(date +%s%N)
#spend=$(expr $end - $begin) use_tm=`echo $end $begin | awk '{ print ($1 - $2) / 1000000000}'`
echo "花费时间为$use_tm"
二. java在ext3中的测试
程序
import java.io.*;
import java.net.URI;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile; public class Toext3 {
public static void main(String[] args) {
//String hdfs_path = "input/file1";//文件路径
String ext3_path = args[];
FileWriter writer = null;
//String inpath = "./append.txt";
try {
String s="";
for(int i=;i<;i++)
{
s="";
for(int j=;j<;j++)
{
s+='b';
}
writer = new FileWriter(ext3_path, true);
writer.write(s);
System.out.println(ext3_path);
} } catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
}
编译:
javac -classpath ./share/hadoop/common/hadoop-common-2.7..jar:./share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7..jar -d ./data/classes data/Toext3.java
运行
java -cp ./data/classes/ Toext3 ./data/ext3/file0
在运行中,-cp指明class文件的路径,Toext3指出要运行的类
hadoop相关的更多相关文章
- [Linux] 安装JDK和Maven及hadoop相关环境
紧接上一篇,继续安装hadoop相关环境 JDK安装: 1. 下载,下面这两个地址在网上找的,可以直接下载: http://download.oracle.com/otn-pu ...
- Hadoop相关项目Hive-Pig-Spark-Storm-HBase-Sqoop
Hadoop相关项目Hive-Pig-Spark-Storm-HBase-Sqoop的相关介绍. Hive Pig和Hive的对比 摘要: Pig Pig是一种编程语言,它简化了Hadoop常见的工作 ...
- 一 hadoop 相关介绍
hadoop 相关介绍 hadoop的首页有下面这样一段介绍.对hadoop是什么这个问题,做了简要的回答. The Apache™ Hadoop® project develops open-sou ...
- Hadoop自学笔记(一)常见Hadoop相关项目一览
本自学笔记来自于Yutube上的视频Hadoop系列.网址: https://www.youtube.com/watch?v=-TaAVaAwZTs(当中一个) 以后不再赘述 自学笔记,难免有各类错误 ...
- Hadoop相关问题解决
Hadoop相关问题解决 Hive 1.查询hivemeta信息,查到的numRows为-1 集群厂商 集群版本 是否高可用 是否开启认证 cdh 不限 不限 不限 在hivemeta库中可以通过以下 ...
- Hadoop相关日常操作
1.Hive相关 脚本导数据,并设置运行队列 bin/beeline -u 'url' --outputformat=tsv -e "set mapreduce.job.queuename= ...
- Hadoop 相关链接
Apache 软件下载 http://mirror.bit.edu.cn/apache/ 相关文档链接: Apache Hadoop 2.5.2 http://hadoop.apache.org ...
- 大数据及hadoop相关知识介绍
一.大数据的基本概念 1.1什么是大数据 互联网企业是最早收集大数据的行业,最典型的代表就是Google和百度,这两个公司是做搜索引擎的,数量都非常庞大,每天都要去把互联网上的各种各样的网页信息抓取下 ...
- Hadoop相关基础知识
因为个人对这块的知识也不熟悉,所以大多内容来源于网络. 1. Hadoop项目框架 2. Hadoop Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以 ...
- Hadoop相关笔记
一. Zookeeper( 分布式协调服务框架 ) 1. Zookeeper概述和集群搭建: (1) Zookeeper概述: Zookeeper 是一个分布式 ...
随机推荐
- LCA 近期公共祖先 小结
LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...
- java 基本数据类型及自己主动类型提升
基本数据类型:8种 1.整型: byte 1个字节 8位 -128到127 short 2个字节 16位 -2^15到(2^15)-1 int 4个字节 32 ...
- caffe训练CIFAR数据库
CIFAR-10是一个用于普适物体识别的数据集.Cifar-10由60000张32*32的RGB彩色图片构成,50000张训练图片,10000张测试图片,分为10类.cifar下载地址: http:/ ...
- WPF常用资源
Textbox error template<Style x:Key="ControlBaseStyle" TargetType="{x:Type Control} ...
- ES6常用特性
以下是ES6排名前十的最佳特性列表(排名不分先后): Default Parameters(默认参数) in ES6 Template Literals (模板文本)in ES6 Multi-line ...
- Java NIO(一)概述
说明 java NIO是从java1.4开始引入的一个新的IO API,它支持面向缓冲区,基于通道的IO操作,它的核心是通道(channel),缓冲区(buffer),选择器(selector) NI ...
- Block formatting context & Inline formatting context(BFC&IFC)的区别(转载)
何为BFC与IFC bfc与ifc是针对页面正常流的两种环境,块级元素处于bfc环境中,行内元素处于ifc环境中. 元素是块级元素or行内元素由其display属性决定: block, table, ...
- C#追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件
C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); sw ...
- ThoughtWorks 技术雷达(2013年5月)
ThoughtWorks技术雷达(2013年5月) 作者ThoughtWorks技术战略委员会 发布于 六月 25, 2013| 讨论 新浪微博腾讯微博 豆瓣网 Twitter Facebook li ...
- os.getcwd()函数的用法
获得当前路径 在Python中可以使用os.getcwd()函数获得当前的路径. 其原型如下所示: os.getcwd() 该函数不需要传递参数,它返回当前的目录.需要说明的是,当前目录并不是指脚本所 ...