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 (); }