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 ...
随机推荐
- Hibernate 连接MySQL/SQLServer/Oracle数据库的hibernate.cfg.xml文件
用Hibernate配置连接数据库可以方便我们对POJO的操作,节省了很多时间和代码.下面就分别说明连接不同数据库需要在hibernate.cfg.xml做的配置. 需要数据库驱动包可以点击这里下载: ...
- 【操作系统之十三】Netfilter与iptables
一.Netfilter Netfilter是由Rusty Russell提出的Linux 2.4内核防火墙框架,该框架既简洁又灵活,可实现安全策略应用中的许多功能,如数据包过滤.数据包处理.地址伪装. ...
- linux _文件目录与权限
1. 目录相关 . 代表次层目录 .. 代表上一层目录 - 代表前一个工作目录 ~ 代表目前使用者身份所在home目录 ~account 代表account这个使用者的home目录 cd 切换目录(c ...
- SQL --------------- order by 排序
首先创建一个表弄点数据 order by 关键字用于排序查询 默认按照升序(asc)进行排列 降序要使用 desc排序方式:数字按照大小 英文字母和汉字按照第一个字母 从 a-z 排列语 法: ...
- VUE方法
1.$event 变量 $event 变量用于访问原生DOM事件. <!DOCTYPE html> <html lang="zh"> <head> ...
- 使用eclipse git插件合并merge部分代码方法
当有一个父项目,它的下面有多个子项目:或者一个项目下边,只想合并部分路径,甚至部分文件的内容,使用下边的方法可以达到目的,特此记录: 1.主项目右键 -> team -> remove f ...
- 如何在Quartus II中查看RTL原理图
整个工程代码编写并且编译完成之后,标题栏选择Tools→Netlist Viewers→RTL Viewer即可
- UWP 使用Launcher 启动迅雷
不得不说UWP有些地方真的不方便! 另外也要夸一下迅雷,还是蛮不错的! 代码 await Launcher.LaunchUriAsync(new Uri("magnet:?xt") ...
- 04、状态模式(State)
一.概念: 当一个对象的内在状态改变时,允许改变其行为,这个对象看起来像是改变了其类.[DP] 二.作用: 状态模式的主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况.吧状态的判断逻辑转 ...
- 正在开发的JavaScript引擎有哪些?
正在开发的JavaScript引擎有哪些? V8,用C++编写,开放源代码,由Google丹麦开发,是Google Chrome的一部分,也用于Node.js. JavaScriptCore,开放源代 ...