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 ...
随机推荐
- 手撕代码:统计1到n二进制数中1出现的总次数
题目描述: 互娱手撕代码题. 统计从1到n这n个数的二进制表示中1出现的次数. 思路分析: 思路一:直接的做法是从1遍历到n,对于每个数和1做与操作,之后,对于这个数不断做右移操作,不断和1做与操作, ...
- markdown文本内跳转
Markdown文本内跳转 构建茅的过程中使用markdown语法,类似于markdown向外跳转链接,目的地地址写成#. 在markdown文本中写入: 目录 跳转 跳转部分按照html文本的写法 ...
- Field redisTemplate in xxxxxx required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.
*************************** APPLICATION FAILED TO START *************************** Description: Fie ...
- 【转帖】分布式事务之解决方案(XA和2PC)
分布式事务之解决方案(XA和2PC) https://zhuanlan.zhihu.com/p/93459200 博彦信息技术有限公司 java工程师 3. 分布式事务解决方案之2PC(两阶段提交 ...
- RC4算法
RC4算法简介:https://baike.baidu.com/item/RC4%E7%AE%97%E6%B3%95/9686396?fr=aladdin RC4算法java实现: /** * RC4 ...
- Python 学习:常用函数整理
整理Python中常用的函数 一,把字符串形式的list转换为list 使用ast模块中的literal_eval函数来实现,把字符串形式的list转换为Python的基础类型list from as ...
- Mysql系列(十一)—— 性能分析其他常用监控
show status show status可以查询显示出当前mysql server的状态信息.该语句不需要任何权限. 对于show status可以时用like子句,模糊检索需要的状态信息.如: ...
- Java匹马行天下之JavaSE核心技术——工具类
Java匹马行天之JavaSE核心技术——工具类 一.Object类 java.lang.ObjectObject类是所有类直接或间接的父类 常用的方法: toString():以字符串形式返回对象的 ...
- jquery动画控制非css属性
JQuery的animate()方法可以通过渐变的更改CSS属性来实现简单的动画效果, 比如 $("#box").animate({height:"300px" ...
- ionic 股票列表 网络读取数据,实现下拉刷新,上拉加载
html: <ion-header> <ion-toolbar> <ion-title> 股票 </ion-title> </ion-toolba ...