Java Write local orc file (Hive2 API) __java

Source: Internet
Author: User
Tags rowcount

Java write local orc file (Hive2 API)

Hive2.0 later, a new API was used to read and write orc files (https://orc.apache.org).
The code in this article generates the Orc file locally using a Java program, and then loads it into the hive table.
The code is as follows:

Package COM.LXW1234.HIVE.ORC;   Import org.apache.hadoop.conf.Configuration; Import Org.apache.hadoop.fs.FileSystem; Import Org.apache.hadoop.fs.Path; Import Org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector; 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;   public class Testorcwriter {  public static void main (string[] args) throws Exception {//define ORC data structure, that is, table structure//C reate TABLE Lxw_orc1 (//Field1 string,//Field2 string,//field3 string//) stored as ORC; Typedescription schema = Typedescription.createstruct (). AddField ("Field1", Typedescription.createstring ()). AddField ("Field2", Typedescription.createstring ()). AddField ("Field3", typedescription.createstring ()); Output orc file local absolute path String lxw_orc1_file = "/tmp/lxw_orc1_file.orc"; Configuration conf = new Configuration (); Filesystem.getlocal (conf); Writer WRIter = Orcfile.createwriter (new Path (Lxw_orc1_file), orcfile.writeroptions (conf). Setschema (Schema). Stripesize ( 67108864). buffersize (131072). BlockSize (134217728). Compress (Compressionkind.zlib). Version (OrcFile.Version.V_0_ 12)); The content to be written string[] contents = new string[]{"1,a,aa", "2,b,bb", "3,c,cc", "4,d,dd"}; Vectorizedrowbatch batch = Schema.createrowbatch (); for (String content:contents) {int rowcount = batch.size++; string[] logs = Content.split (",",-1); for (int i=0; i<logs.length; i++) {((bytescolumnvector) batch.cols[i]). Setval (RowCount, logs[i].getbytes ()); Batch full if (batch.size = = Batch.getmaxsize ()) {writer.addrowbatch (batch); Batch.reset ();}} } writer.addrowbatch (Batch); Writer.close (); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.