MapReduce读取定长文件入库Hive表Orc格式


MapReduce读取定长文件入库Hive表Orc格式

定长文件指,每一行的byte是相同的。且有一个定义定长数据中,每一部分是什么字段,长度多少等信息。

需要写入到指定的Hive分区的时候, 需要创建对应分区并指定地址为输出地址。既可完成。

MapReduce启动程序

实例代码,配置读取文件,Map操作,Reduce操作以及输出文件。

package com.study.spark.mr;

import com.study.spark.mr.mapper.FixedLengthMapper;
import com.study.spark.mr.reduce.OrcFixedReduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FixedLengthInputFormat;
import org.apache.orc.TypeDescription;
import org.apache.orc.mapred.OrcStruct;
import org.apache.orc.mapreduce.OrcOutputFormat;
import org.apache.parquet.hadoop.ParquetInputFormat;

public class FileOrcParquetExample {
    
    public static void main(String[] args) throws Exception{
        mr();
    }


    public static void mr() throws Exception {

        Configuration configuration = new Configuration();
        int recordLength = 200; //定长文件每行长度,如果文件每行带有/n则需要加1
        configuration.setInt(FixedLengthInputFormat.FIXED_RECORD_LENGTH,recordLength);
        configuration.set("encode","文件编号格式");
        configuration.set("orc.mapred.output.schema",schema().toString());

        Job job = Job.getInstance(configuration);
        //设置执行的
        job.setJarByClass(FileParquetExample.class);
        job.setJobName("FileParquetExample");
        Path path = new Path("hdfs:");
        ParquetInputFormat.setInputPaths(job,path);
        job.setInputFormatClass(FixedLengthInputFormat.class);
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(BytesWritable.class);
        job.setMapperClass(FixedLengthMapper.class);

        job.setOutputFormatClass(OrcOutputFormat.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(OrcStruct.class);
        job.setReducerClass(OrcFixedReduce.class);
        //文件输出位置
        OrcOutputFormat.setOutputPath(job,new Path("hdfs://"));

        job.waitForCompletion(true);

    }

    public static  TypeDescription schema(){
        OrcStruct各种数据格式参考链接:https://blog.csdn.net/swg321321/article/details/125879576
        TypeDescription description = new TypeDescription(TypeDescription.Category.STRUCT);
        description.addField("boolean",TypeDescription.createBoolean());
        description.addField("decimal",TypeDescription.createDecimal()).withPrecision(22).withScale(2);
        return description;

    }

}

FixedLengthMapper

代码实现,定长文件读取出现的数据,在这里进入Mapper处理。

package com.study.spark.mr.mapper;

import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class FixedLengthMapper extends Mapper<LongWritable, BytesWritable, LongWritable, BytesWritable> {
    /**
     * 在这里完成,对数据的修改。如果不错修改也可以放到Reduce中进行修改
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    protected void map(LongWritable key, BytesWritable value, Context context) throws IOException, InterruptedException {
        context.write(key, value);
    }
}

OrcFixedReduce

代码实现,从Map读取到数据转为Orc文件

package com.study.spark.mr.reduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.orc.TypeDescription;
import org.apache.orc.mapred.OrcStruct;

import java.io.IOException;

public class OrcFixedReduce extends Reducer<LongWritable, BytesWritable, NullWritable, OrcStruct> {

    private TypeDescription typeDescription;

    /**
     * Called once at the start of the task.
     */
    protected void setup(Context context) throws IOException, InterruptedException {

        Configuration config = context.getConfiguration();
        if(config.get("orc.mapred.output.schema") == null){
            throw new RuntimeException("需要设置ORC的Schema,orc.mapred.output.schema");
        }
        typeDescription =  TypeDescription.fromString(config.get("orc.mapred.output.schema"));
    }

    /**
     * This method is called once for each key. Most applications will define
     * their reduce class by overriding this method. The default implementation
     * is an identity function.
     */
    @SuppressWarnings("unchecked")
    protected void reduce(LongWritable key, Iterable<BytesWritable> values, Context context) throws IOException, InterruptedException {
        for(BytesWritable value: values) {
            OrcStruct orcStruct = new OrcStruct(typeDescription);
            byte[] bs = value.getBytes();
            //在这里实现自己的分割字符
            //OrcStruct各种数据格式写入参考链接:https://blog.csdn.net/swg321321/article/details/125879576

            context.write(NullWritable.get(),orcStruct);
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于hadoopHive数据仓库JavaAPI简单调用的实例,关于Hive的简介在此不赘述。hive提供了三种用户接口:CLI,JDBC/ODBC和 WebUI CLI,即Shell命令行 JDBC/ODBC 是 Hive 的Java,与使用传统数据库JDBC的方式类似 WebGUI是通过浏览器访问 Hive 本文主要介绍的就是第二种用户接口,直接进入正题。 1、Hive 安装: 1)hive的安装请参考网上的相关文章,测试时只在hadoop一个节点上安装hive即可。 2)测试数据data文件'\t'分隔: 1 zhangsan 2 lisi 3 wangwu 3)将测试数据data上传到linux目录下,我放置在:/home/hadoop01/data 2、在使用 JDBC 开发 Hive 程序时, 必须首先开启 Hive 的远程服务接口。使用下面命令进行开启: Java代码 收藏代码 hive --service hiveserver >/dev/null 2>/dev/null & 我们可以通过CLI、Client、Web UI等Hive提供的用户接口来和Hive通信,但这三种方式最常用的是CLI;Client 是Hive的客户端,用户连接至 Hive Server。在启动 Client 模式的时候,需要指出Hive Server所在节点,并且在该节点启动 Hive Server。 WUI 是通过浏览器访问 Hive。今天我们来谈谈怎么通过HiveServer来操作Hive。   Hive提供了jdbc驱动,使得我们可以用Java代码来连接Hive并进行一些类关系型数据库的sql语句查询等操作。同关系型数据库一样,我们也需要将Hive的服务打开;在Hive 0.11.0版本之前,只有HiveServer服务可用,你得在程序操作Hive之前,必须在Hive安装的服务器上打开HiveServer服务,如下: 1 [wyp@localhost/home/q/hive-0.11.0]$ bin/hive --service hiveserver -p10002 2 Starting Hive Thrift Server 上面代你已经成功的在端口为10002(默认的端口是10000)启动了hiveserver服务。这时候,你就可以通过Java代码来连接hiveserver,代码如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值