首页 > 编程语言 >orc使用java生成文件的示例代码

orc使用java生成文件的示例代码

时间:2024-07-30 18:28:37浏览次数:7  
标签:java String 示例 strs public new import orc

包含了int等基本类型、string、数组

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.ListColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
import org.apache.orc.CompressionKind;
import org.apache.orc.OrcFile;
import org.apache.orc.TypeDescription;
import org.apache.orc.Writer;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成orc文件
 */
public class WriteToOrcFile {

    public static class Bean {
        int id;
        String name;
        String[] strs;

        public Bean(int id, String name, String[] strs) {
            this.id = id;
            this.name = name;
            this.strs = strs;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String[] getStrs() {
            return strs;
        }

        public void setStrs(String[] strs) {
            this.strs = strs;
        }
    }

    public static void main(String[] args) throws IOException, SQLException {
        new WriteToOrcFile().writeOrc("myfile.orc");
    }

    public void writeOrc(String filename) throws IOException, SQLException {
        Configuration conf = new Configuration();
        List<Bean> list = new ArrayList();
        Bean Bean1 = new Bean(1, "1", new String[]{"1", "1", "1"});
        Bean Bean2 = new Bean(2, "2", new String[]{"2", "2", "2"});
        Bean Bean3 = new Bean(3, "3", new String[]{"3", "3", "3"});
        list.add(Bean1);
        list.add(Bean2);
        list.add(Bean3);
        //确定每一列的数据类型
        TypeDescription schema = TypeDescription.createStruct()
                .addField("id", TypeDescription.createInt())
                .addField("name", TypeDescription.createString())
                .addField("strs", TypeDescription.createList(TypeDescription.createString()));
        //输出orc文件到本地路径
        String path = "/temp/" + filename;
        File file = new File(path);
        if (file.exists()) {//文件存在则删除
            System.out.println("orc文件文件存在,删除");
            file.delete();
        }
        //设置写入流时的参数,
        Writer writer = OrcFile.createWriter(new Path(path), OrcFile.writerOptions(conf)
                .setSchema(schema)
                .stripeSize(67108864)
                .bufferSize(64 * 1024)
                .blockSize(128 * 1024 * 1024)
                .rowIndexStride(10000)
                .blockPadding(true)
                //默认压缩算法为zlib,zlib相对于snappy压缩算法,压缩比更低,压缩效果更好,但是花费了更多的压缩时间
                .compress(CompressionKind.ZLIB));
        VectorizedRowBatch batch = schema.createRowBatch();
        //获取每一列的引用
        LongColumnVector a = (LongColumnVector) batch.cols[0];
        BytesColumnVector b = (BytesColumnVector) batch.cols[1];
        ListColumnVector c = (ListColumnVector) batch.cols[2];
        //开始转换成二进制的orc文件
        for (Bean o : list) {
            int row = batch.size++;
            //int,double,long等数据类型用  引用.vector
            a.vector[row] = o.getId();
            //String等数据类型用 引用.setVal
            b.setVal(row, o.getName().getBytes());
            //数组
            String[] strs = o.getStrs();
            c.offsets[row] += 1;
            c.lengths[row] = strs.length;
            c.childCount += c.lengths[row];
            for (int i = 0; i < strs.length; i++) {
                ((BytesColumnVector) c.child).setVal((int) (c.offsets[row] + i), strs[i].getBytes());
            }
            writer.addRowBatch(batch);
            batch.reset();
        }
        System.out.println("orc文件写出完成");
        writer.close();
    }
}

 

标签:java,String,示例,strs,public,new,import,orc
From: https://www.cnblogs.com/zyanrong/p/18333125

相关文章

  • Codeforces Round 933 (Div. 3) D 题
    D.RudolfandtheBallGame原题链接:https://codeforces.com/contest/1941/problem/D RudolfandBernarddecidedtoplayagamewiththeirfriends. n peoplestandinacircleandstartthrowingaballtoeachother.Theyarenumberedfrom 1 to nn i......
  • Java中的变量
    变量目录变量变量的声明变量的分类局部变量成员变量(实例变量)类变量(静态变量)参数变量变量的声明在Java语言中,所有的变量在使用前必须声明。声明变量的基本格式如下:typeidentifier[=value][,identifier[=value]...];格式说明:type--数据类型。identifier--......
  • 你知道orcal数据库配置文件listener.ora,tnsnames.ora中错一个空格有多大的威力吗
    上周客户要求说要修改,所有的用户密码,提升网络安全的等级性,于是积极响应,结果发现系统里面的报表无法打开,咨询开发老师已经,对方希望能够重启服务器,对这个做法,我是不怎么赞成的,因为每次重启都伴随一大堆问题,实在令人苦恼,可是一时又想不到其他解决办法,于是只能同意他重启。果不其然,服......
  • JavaScript 数据结构与基础算法
    数据结构全解参考:数据结构|博客园-SRIGT相关代码仓库查看:data-struct-js|Github-SR1GT0x00前置知识(1)类使用关键字class声明一个类classPerson{}JavaScript的类中通过constructor使用构建函数classPerson{constructor(name){this.name......
  • java @Cacheable生成的redisKey,出现两个连续的冒号::
    1、参考基于redis2.1.6实现springcache生成的key多出一个冒号2、解决需要对key进行处理,【重点】是computePrefixWith方法config=config.computePrefixWith(cacheName->{returncacheName+StrUtil.COLON;});以下是完整代码实现CacheK......
  • Codeforces Round 929 (Div. 3)---->E. Turtle vs. Rabbit Race: Optimal Trainings
    https://codeforces.com/contest/1933/problem/E#include<bits/stdc++.h>#definexfirst#defineysecondusingnamespacestd;typedeflonglongll;typedef__int128i128;typedefpair<int,int>pii;constintN=2e5+10,M=110;intn,q;inta[N];ll......
  • Java使用EasyExcel自定义合并(横纵合并)、自定义行高列宽、自适应行高列宽工具Excel导出
    目录一、自适应行高列宽工具类1、自适应行高2、自适应列宽二、自定义行高列宽工具类1、自定义行高2、自定义列宽三、自定义合并工具类四、自定义样式五、实现Excel的完整代码最近又开始写Excel导出的业务,之前写的自适应行高列宽工具类并不满足现下的需求需求是导出......
  • [RoarCTF 2019]Easy Java
    [RoarCTF2019]EasyJavaStep1点击help按钮后发现:URL变成:url/Download?filename=help.docx而回显:java.io.FileNotFoundException:{help.docx}而当我尝试尝试POST,发现文件成功下载:Step2发现可能的漏洞点后,结合WEB-INF相关知识(见文末)可以下载WEB-INF/web.xmlPOST参数......
  • 强化学习Reinforcement Learning算法的样本效率提升策略
    强化学习ReinforcementLearning算法的样本效率提升策略1.背景介绍1.1问题的由来在强化学习领域,提升算法的样本效率是关键挑战之一。在许多现实世界的应用场景中,比如机器人自主导航、智能游戏、自动驾驶、医疗健康决策以及大规模服务系统优化,获取高价值的环境反馈往往......
  • 从CNN到Transformer:基于PyTorch的遥感影像、无人机影像的地物分类、目标检测、语义分
    原文链接:从CNN到Transformer:基于PyTorch的遥感影像、无人机影像的地物分类、目标检测、语义分割和点云分类教程https://mp.weixin.qq.com/s?__biz=MzUzNTczMDMxMg==&mid=2247610610&idx=5&sn=f973c3e430c89d6123ca8f4892086c55&chksm=fa827115cdf5f8036ef8111c6f06cf592a8c0587......