mapreduce 读写Parquet格式数据 Demo
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.parquet.example.data.Group;
import org.apache.parquet.example.data.simple.SimpleGroupFactory;
import org.apache.parquet.hadoop.ParquetInputFormat;
import org.apache.parquet.hadoop.ParquetOutputFormat;
import org.apache.parquet.hadoop.example.GroupReadSupport;
import org.apache.parquet.hadoop.example.GroupWriteSupport;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.OriginalType;
import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName;
import org.apache.parquet.schema.Types; /**
* MR Parquet格式数据读写Demo
*/
public class ParquetReaderAndWriteMRDemo { public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherargs=new GenericOptionsParser(conf, args).getRemainingArgs();
if(otherargs.length!=3){
System.out.println("<in> <out> 1");
System.out.println("<parquet-in> <out> 2");
System.out.println("<in> <parquet-out> 3");
System.out.println("<parquet-in> <parquet-out> 4");
System.exit(2);
}
//此demo 输入数据为2列 city ip MessageType schema = Types.buildMessage()
.required(PrimitiveTypeName.BINARY).as(OriginalType.UTF8).named("city")
.required(PrimitiveTypeName.BINARY).as(OriginalType.UTF8).named("ip")
.named("pair");
System.out.println("[schema]=="+schema.toString());
GroupWriteSupport.setSchema(schema, conf); Job job = Job.getInstance(conf, "ParquetReadMR");
job.setJarByClass(ParquetReaderAndWriteMRDemo.class); if(otherargs[2].equals("1")){
job.setMapperClass(NormalMapper.class);
job.setReducerClass(NormalReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.setInputPaths(job,otherargs[0] );
FileOutputFormat.setOutputPath(job, new Path(otherargs[1]));
if (!job.waitForCompletion(true))
return;
}
if(otherargs[2].equals("3")){
job.setMapperClass(ParquetWriteMapper.class);
job.setNumReduceTasks(0);
FileInputFormat.setInputPaths(job,otherargs[0] ); //parquet输出
job.setOutputFormatClass(ParquetOutputFormat.class);
ParquetOutputFormat.setWriteSupportClass(job, GroupWriteSupport.class);
// ParquetOutputFormat.setOutputPath(job, new Path(otherargs[1]));
FileOutputFormat.setOutputPath(job, new Path(otherargs[1]));
if (!job.waitForCompletion(true))
return;
} if(otherargs[2].equals("2")){
//parquet输入
job.setMapperClass(ParquetReadMapper.class);
job.setNumReduceTasks(0);
job.setInputFormatClass(ParquetInputFormat.class);
ParquetInputFormat.setReadSupportClass(job, GroupReadSupport.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.setInputPaths(job,otherargs[0] );
FileOutputFormat.setOutputPath(job, new Path(otherargs[1]));
if (!job.waitForCompletion(true))
return;
}
if(otherargs[2].equals("4")){
//TODO 不想写了
}
} public static class ParquetWriteMapper extends Mapper<LongWritable, Text, Void, Group> {
SimpleGroupFactory factory=null;
protected void setup(Context context) throws IOException ,InterruptedException {
factory = new SimpleGroupFactory(GroupWriteSupport.getSchema(context.getConfiguration()));
}; public void map(LongWritable _key, Text ivalue, Context context) throws IOException, InterruptedException {
Group pair=factory.newGroup();
String[] strs=ivalue.toString().split("\\s+");
pair.append("city", strs[0]);
pair.append("ip", strs[1]);
context.write(null,pair);
}
} public static class ParquetReadMapper extends Mapper<Void, Group, Text, Text> {
public void map(Void _key, Group group, Context context) throws IOException, InterruptedException {
String city=group.getString(0, 0);
String ip=group.getString(1, 0);
context.write(new Text(city),new Text(ip));
}
} public static class NormalMapper extends Mapper<LongWritable, Text, Text, Text> { public void map(LongWritable ikey, Text ivalue, Context context) throws IOException, InterruptedException {
String[] strs=ivalue.toString().split("\\s+");
context.write(new Text(strs[0]), new Text(strs[1]));
}
}
public static class NormalReducer extends Reducer<Text, Text, Text, Text> { public void reduce(Text _key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
for (Text text : values) {
context.write(_key,text);
} }
} }
mapreduce 读写Parquet格式数据 Demo的更多相关文章
- java 读写Parquet格式的数据 Parquet example
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOExce ...
- Hive 导入 parquet 格式数据(未完,待续)
Hive 导入 parquet 格式数据 Parquet 格式文件,查看Schema Parquet 之mapreduce Hive 导入 parquet 格式数据
- Hive 导入 parquet 格式数据
Hive 导入 parquet 数据步骤如下: 查看 parquet 文件的格式 构造建表语句 倒入数据 一.查看 parquet 内容和结构 下载地址 社区工具 GitHub 地址 命令 查看结构: ...
- matlab 读写其他格式数据文件(excel)
1. excel matlab和excel 中的数据互相导入 xlswrite() mat ⇒ excel 请问怎么把大容量的mat文件导出到excel文件中 – MATLAB中文论坛 % data. ...
- Android读写JSON格式的数据之JsonWriter和JsonReader
近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...
- 大数据学习day25------spark08-----1. 读取数据库的形式创建DataFrame 2. Parquet格式的数据源 3. Orc格式的数据源 4.spark_sql整合hive 5.在IDEA中编写spark程序(用来操作hive) 6. SQL风格和DSL风格以及RDD的形式计算连续登陆三天的用户
1. 读取数据库的形式创建DataFrame DataFrameFromJDBC object DataFrameFromJDBC { def main(args: Array[String]): U ...
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- spark DataFrame 读写和保存数据
一.读写Parquet(DataFrame) Spark SQL可以支持Parquet.JSON.Hive等数据源,并且可以通过JDBC连接外部数据源.前面的介绍中,我们已经涉及到了JSON.文本格式 ...
- Parquet 格式文件
Apache Parquet是Hadoop生态圈中一种新型列式存储格式,它可以兼容Hadoop生态圈中大多数计算框架(Hadoop.Spark等),被多种查询引擎支持(Hive.Impala.Dril ...
随机推荐
- SQLAIchemy 学习(一)Session 相关
0. 前言 最近是使用 SQLAlchemy 框架作为一个 ORM 框架,现对其做简单整理 1. 创建 Session 说到数据库,就离不开 Session.Session 的主要目的是建立与数据库的 ...
- nuget安装说明
1.先百度安装nuget.server 比如 https://www.cnblogs.com/tomfang/articles/3999303.html 2.官网下载nuget.exe nuget p ...
- Zookeeper 运维实践手册
Zookeeper是一个高可用的分布式数据管理与协调框架,该框架能很好地保证分布式环境中数据一致性.一般用来实现服务发现(类似DNS),配置管理,分布式锁,leader选举等. 一.生产环境中Zook ...
- 详解golang net之netpoll
golang版本1.12.9:操作系统:readhat 7.4 golang的底层使用epoll来实现IO复用.netPoll通过pollDesc结构体将文件描述符与底层进行了绑定.netpoll实现 ...
- ascii码对照表(收藏)
https://blog.csdn.net/yueyueniaolzp/article/details/82178954 十进制代码 十六进制代码 MCS 字符或缩写 DEC 多国字符名 ASCII ...
- 用siege测试接口高并发
siege -c 255 -r 2555 "http://10.1.1.6:3001/decode POST <./api.json" -t 100s
- nodejs anywhere 搭建本地静态文件服务
一.背景 工作中有时候往往会遇到下述场景:例如需要将新打好的安装包等文件临时性的给到同事,可能还需要给到多个同事.这时,我们往往有如下几种方案: 1,一般都会有公司内部的文件系统,上传文件后将对应的地 ...
- 更新element-ui版本
1. 卸载当前版本 npm uninstall element-ui 2. 安装指定版本 npm -S
- golang --os系统包
环境变量 Environ 获取所有环境变量, 返回变量列表 func Environ() []string package main import ( "fmt" "os ...
- EF Core中的DB First与Code First
前言: 大家都习惯在程序中生成对应的model来对数据库进行操作,所以如何快速的生成数据库表的对应model,是基础之一.总结了一下在我的认知中大概是这个结构: Db first方式: 先创建好对应的 ...