mavenhdfsMapReduce

1. 配置maven环境

windows配置Maven

  1. 下载Maven二进制压缩包
  2. 解压配置maven环境变量
  3. 验证 mvn -v
C:\Users\Administrator>mvn -v
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00)

2. 创建maven项目

2.1 pom.xml 依赖

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<hadoop.version>2.7.2</hadoop.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency> </dependencies>

2.2 单元测试

读取hdfs文件内容

package com.lyf;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test; import java.io.IOException; /**
* @author lyf
* @date 2019/1/9 16:41
* 用java操作hdfs文件系统
*/
public class HdfsTest { @Test
public void test01() throws IOException {
// 获取配置
Configuration conf = new Configuration();
// 配置参数
conf.set("fs.defaultFS","hdfs://192.168.37.151:9000");
// 获取hdfs文件系统的操作对象
FileSystem fs = FileSystem.get(conf);
// 对文件的具体操作
FSDataInputStream fis = fs.open(new Path("/README.txt"));
IOUtils.copyBytes(fis, System.out, 4096, true);// 4096kb 缓存大小 true 关闭流
}
}

For the latest information about Hadoop, please visit our website at:
...
Hadoop Core uses the SSL libraries from the Jetty project written
by mortbay.org.

3. hdfs文件操作

3.1 文件上传和下载

/**
* @author lyf
* @date 2019/1/9 16:41
* 用java操作hdfs文件系统
*/
public class HdfsTest { /**
* 读取文件
* @throws IOException
*/
@Test
public void test01() throws IOException {
// 获取配置
Configuration conf = new Configuration();
// 配置参数
conf.set("fs.defaultFS","hdfs://192.168.37.151:9000");
// 获取hdfs文件系统的操作对象
FileSystem fs = FileSystem.get(conf);
// 对文件的具体操作
FSDataInputStream fis = fs.open(new Path("/README.txt"));
IOUtils.copyBytes(fis, System.out, 4096, true);// 4096kb 缓存大小 true 关闭流
fis.close();
} /**
* 下载文件
* @throws IOException
*/
@Test
public void test02() throws IOException {
// 获取配置
Configuration conf = new Configuration();
// 配置参数
conf.set("fs.defaultFS","hdfs://192.168.37.151:9000");
// 获取hdfs文件系统的操作对象
FileSystem fs = FileSystem.get(conf);
// 对文件的具体操作
FSDataInputStream fis = fs.open(new Path("/README.txt"));
OutputStream out = new FileOutputStream(new File("D:\\read.txt"));
IOUtils.copyBytes(fis, out, 4096, true);// 4096kb 缓存大小 true 关闭流 fis.close();
out.close();
} /**
* 上传文件
* @throws IOException
*/
@Test
public void test03() throws IOException, URISyntaxException, InterruptedException {
// 获取配置
Configuration conf = new Configuration();
// 获取hdfs文件系统的操作对象
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.37.151:9000"), conf, "root");
// 对文件的具体操作
fs.copyFromLocalFile(new Path("D:\\123.txt"), new Path("/123"));
System.out.println("finished...");
}
}

3.2 RPC远程方法调用

定义接口

public interface Hello {
public static final long versionID = 1L;
public String say(String words);
}

实现接口

public class RpcServer implements Hello {
@Override
public String say(String words) {
System.out.println("Client: "+words);
return words + " [by Server]";
} public static void main(String []args) { try {
RPC.Server server = new RPC.Builder(new Configuration())
.setInstance(new RpcServer())
.setProtocol(Hello.class)
.setBindAddress("127.0.0.1")
.setPort(6666)
.build();
// 启动服务
server.start();
System.out.println("server is running...");
} catch (IOException e) {
e.printStackTrace();
} }
}

远程调用

public class RpcClient {
public static void main(String []args) throws IOException, InterruptedException {
Hello hello = RPC.getProxy(Hello.class,
1,
new InetSocketAddress("127.0.0.1", 6666),
new Configuration());
String res = hello.say("I'm lyf");
Thread.sleep(2000);
System.out.println(res);
}
}

server is running...
Client: I'm lyf
I'm lyf [by Server]

4. MapReduce操作

分布式并行离线计算框架

4.1 WordCount

pom.xml

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<hadoop.version>2.7.2</hadoop.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency> </dependencies>

统计单词出现次数

package com.lyf;

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;// 长包的是2.x
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.IOException; /**
* Word count 词频统计
* @author lyf
* @date 2019/1/26 14:45
*/
public class MyWordCount { /**
* Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
* KEYIN 行偏移量, map阶段输入key类型
* VALUEIN 行的值, map阶段输入value类型
* KEYOUT map阶段输出key类型
* VALUEOUT map阶段输出value类型
*
* When you are old and grey and full of sleep
* And nodding by the fire,take down this book
* And slowly read,and dream of the soft look
* Your eyes had once,and of their shadows deep
*
* 0 When you are old and grey and full of sleep
* 43 And nodding by the fire,take down this book
* 87 And slowly read,and dream of the soft look
* 130 Your eyes had once,and of their shadows deep
*
* map阶段输出:
* When 1
* you 1
* are 1
* old 1
* and 1
* grey 1
* and 1
* full 1
* of 1
* sleep 1
*
* reduce阶段
* reducer输入:
*/
public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ public static Text k = new Text();
public static IntWritable v = new IntWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
/***
* 1. 从输入数据中获取文件每一行的值
* 2. 对每一行进行切分(非必须),当前例子是空格
* 3. 循环处理
* */
String line = value.toString();
String[] words = line.split(" ");
for(String word : words){
k.set(word);
v.set(1);
// map阶段输出
context.write(k,v);
}
}
} public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
/***
* 1. 自定义计数器
* 2. 循环计数
* 3. 输出结果
* */
int counter = 0;
for (IntWritable i : values){
counter += i.get();
}
context.write(key, new IntWritable(counter));
}
} public static void main(String []args) throws IOException, ClassNotFoundException, InterruptedException {
// 1. 获取配置
Configuration conf = new Configuration();
// 2. 对conf进行设置(没有不用设置)
// 3. 获取job对象
Job job = Job.getInstance(conf, "mywordcount");
// 4. 设置job运行主类
job.setJarByClass(MyWordCount.class); // 5. 对map阶段进行设置
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0])); // 6. 对reduce阶段进行设置
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileOutputFormat.setOutputPath(job, new Path(args[1])); // 7. 提交job并打印信息
int isok = job.waitForCompletion(true) ? 0 : 1;
// 退出整个job
System.exit(isok);
}
}

测试文本:

When you are old and grey and full of sleep
And nodding by the fire,take down this book
And slowly read,and dream of the soft look
Your eyes had once,and of their shadows deep

4.2 本地模式运行

下载hadoop Windows安装包,配置HADOOP_HOME,Path中添加%HADOOP_HOME%/bin,注意bin目录中不要缺少winutil.exe 和 hadoop.dll,否则会报错.配置完后重启电脑生效
在idea中添加 运行参数, 选择


运行main函数就可以看到结果

4.3 集群模式运行

  1. Maven打包并上传jar包
  2. Linux创建mywords测试文件,并上传到hdfs
    hdfs dfs -put /home/mywords /mywords
  3. 运行测试
    yarn jar /home/mr01-1.0-SNAPSHOT.jar com.lyf.MyWordCount /mywords /out/02
  4. 查看结果
    hdfs dfs -cat /out/02/part-r-00000

And 2
When 1
...
you 1

5. 封装util

package com.lyf.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.Date; /**
* @author lyf
* @date 2019/3/18 0018 下午 7:58
*/
public class HadoopDriverUtilPro{ private String jobName;
private Class map;
private Class reduce;
private Class main;
private String outPath; /**
* 自动生成任务编号
*/
public HadoopDriverUtilPro() {
this.jobName = "job_" + new Date().getTime();
} /**
* 设置运行类
* @param map Mapper类
* @param reducer Reduce类
* @param main 运行主类
*/
public HadoopDriverUtilPro(Class map, Class reducer, Class main) {
this.jobName = "job_" + new Date().getTime();
this.map = map;
this.reduce = reducer;
this.main = main;
} /**
* 设置输入输出路径
* @param in 文件输入路径
* @param out 文件输出路径
* @return
* @throws IOException
*/
public Job getInstance(String in, String out) throws IOException {
// 1. 获取配置
Configuration conf = new Configuration();
// 2. 获取job对象
Job job = Job.getInstance(conf, jobName);
// 3. 设置运行主类
job.setJarByClass(main);
// 4. 设置Map
job.setMapperClass(map);
// 获取泛型列表
ParameterizedType p = (ParameterizedType) map.getGenericSuperclass();
// 获取map阶段的输出类型并设置
job.setMapOutputKeyClass((Class) p.getActualTypeArguments()[2]);
job.setMapOutputValueClass((Class) p.getActualTypeArguments()[3]);
FileInputFormat.addInputPath(job, new Path(in)); // 5. 设置Reduce
job.setReducerClass(reduce);
// 获取reduce阶段的输出类型并设置
p = (ParameterizedType) reduce.getGenericSuperclass();
job.setOutputKeyClass((Class) p.getActualTypeArguments()[2]);
job.setOutputValueClass((Class) p.getActualTypeArguments()[3]);
// 6. 随机生成输出目录,方便调试
this.outPath = out+"\\"+r(4);
FileOutputFormat.setOutputPath(job, new Path(outPath));
return job;
} /**
* 生成随机数
* @param len 随机数长度
* @return
*/
public String r(int len) {
return (Math.random()+"").substring(2,len+2);
} public void close(Job job) throws InterruptedException, IOException, ClassNotFoundException {
// 提交job
int isok = job.waitForCompletion(true) ? 0 : 1;
System.out.println("---------输出目录-----------");
System.out.println("outpath: "+outPath);
// 退出
System.exit(isok);
}
}

修改WordCount的main函数为:

public static void main(String []args) throws IOException, ClassNotFoundException, InterruptedException {
// 构建Util,参数依次为: Mapper类 Reduce类 驱动类
HadoopDriverUtilPro util = new HadoopDriverUtilPro(MyMapper.class, MyReducer.class, MyWordCount.class);
// 创建job,参数依次为: 输入路径 输出路径
// 直接写死路径测试也可以
Job job = util.getInstance(args[0],args[1]);
// 关闭任务
util.close(job);
}

测试起来更方便点

Java操作Hadoop集群的更多相关文章

  1. java操作redis集群配置[可配置密码]和工具类(比较好用)

    转: java操作redis集群配置[可配置密码]和工具类 java操作redis集群配置[可配置密码]和工具类     <dependency>   <groupId>red ...

  2. java操作redis集群配置[可配置密码]和工具类

    java操作redis集群配置[可配置密码]和工具类     <dependency>   <groupId>redis.clients</groupId>   & ...

  3. 操作Hadoop集群

    操作Hadoop集群 所有必要的配置完成后,将文件分发到所有机器上的HADOOP_CONF_DIR目录.这应该是所有机器上相同的目录. 一般来说,建议HDFS和YARN作为单独的用户运行.在大多数安装 ...

  4. Java操作 Redis 集群

    // 连接redis集群 @Test public void testJedisCluster() { JedisPoolConfig config = new JedisPoolConfig(); ...

  5. windows环境:idea或者eclipse指定用户名操作hadoop集群

    方法 在系统的环境变量或java JVM变量添加HADOOP_USER_NAME(具体值视情况而定). 比如:idea里面可以如下添加HADOOP_USER_NAME=hdfs 原理:直接看源码 /h ...

  6. 【大数据系列】hadoop集群设置官方文档翻译

    Hadoop Cluster Setup Purpose Prerequisites Installation Configuring Hadoop in Non-Secure Mode Config ...

  7. Spark集群环境搭建——Hadoop集群环境搭建

    Spark其实是Hadoop生态圈的一部分,需要用到Hadoop的HDFS.YARN等组件. 为了方便我们的使用,Spark官方已经为我们将Hadoop与scala组件集成到spark里的安装包,解压 ...

  8. hadoop集群配置和在windows系统上运用java操作hdfs

    安装 配置 概念 hadoop常用shell命令 使用java操作hadoop 本文介绍hadoop集群配置和在windows系统上运用java操作hdfs 安装 http://mirror.bit. ...

  9. Java接口对Hadoop集群的操作

    Java接口对Hadoop集群的操作 首先要有一个配置好的Hadoop集群 这里是我在SSM框架搭建的项目的测试类中实现的 一.windows下配置环境变量 下载文件并解压到C盘或者其他目录. 链接: ...

随机推荐

  1. 更改matlab默认精度

    我使用的是R2018a 1.打开预设 2.命令行窗口 3.变量

  2. 【Excel】多条件查找

    例如下图:要求在单元格从C10中根据分类与名称找出相应的数量 1.VLOOKUP函数(数组公式) {=VLOOKUP(A10&B10,IF({1,0},A2:A6&B2:B6,C2:C ...

  3. Thinking In SE

    各种编程范式的区别 并发模型 并行架构: 位级(bit-level)并行 指令级(instruction-level)并行 数据级(data)并行 数据级并行 任务级(task-level)并行 -- ...

  4. cesium常用设置【转】

    https://blog.csdn.net/D_Walker/article/details/82188514 1.加载线上cesium代码<link href="http://ces ...

  5. hive中同源多重insert写法

    多重insert: with tmp_a as ( select name from tmp_test3 ) from tmp_a insert overwrite table tmp_test1 s ...

  6. DispatcherServlet 被加载顺序

    javax.servlet.Servlet#init 0 -> javax.servlet.GenericServlet#init(javax.servlet.ServletConfig) 1 ...

  7. LC 968. Binary Tree Cameras

    Given a binary tree, we install cameras on the nodes of the tree. Each camera at a node can monitor  ...

  8. vue---自定义指令的使用

    在vue开发项目中,指令的使用场景也是比较多的,那么该如何定义使用呢? 找到 src / directive 下新建 gender 目录,下面新建 index.js 和 gender.js index ...

  9. Sword 第三方库介绍一

    /* 获取字符编码 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <str ...

  10. 使用draw.io桌面版代替visio制作流程图

    前言 draw.io是一款在github上的开源产品,由于需要构建在线文档,需要插入画图类型, 对比多款开源产品,最终选择了draw.io. draw.io图标资源非常的丰富,方便导入图标资源,基本上 ...