使用hiveudf解压缩列数据

piv4azn7  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(459)

context:decompress the 使用hive udf evaluate()方法的列数据
例外情况:
失败,出现异常java.io.ioexception:org.apache.hadoop.hive.ql.metadata.hiveexception:无法执行方法public static org.apache.hadoop.io.text test.udfdecompressor.evaluate(java.lang.string)在对象测试上抛出org.apache.hadoop.hive.ql.metadata.hiveexception。udfdecompressor@1008df1e 类test.udfdecompressor的参数{x��}千瓦⸲�_一�����ö¤ï¿½\��a-b型�我�@`�����"�第三次�我����$_�e�� } 大小1
源代码:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.InflaterInputStream;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaStringObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

public class Decompress extends UDF{
public static String evaluate(String data1) throws IOException, DataFormatException{
ByteArrayInputStream bao=new ByteArrayInputStream(data1.getBytes());
InflaterInputStream iis= new InflaterInputStream(bao);
String out="";
byte[] bt=new byte[1024];
int len=-1;
while ((len =iis.read(bt))!=-1){ 
out += new String(Arrays.copyOf(bt, len));
}
JavaStringObjectInspector stringInspector;
stringInspector = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
String ip = stringInspector.getPrimitiveJavaObject(out);

//return new String(ip.getBytes(Charset.forName("UTF-8")));
//return new String(ip.getBytes(Charset.forName("UTF-8")));
return ip;
}
}

我尝试了多种方法来使用gzib、zlibjavaapi解压,但是遇到了相同的错误。有谁能帮助我解决这个问题,并建议使用hiveudf解压列数据的正确方法吗
提前谢谢。

frebpwbc

frebpwbc1#

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.Text;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;

public class Decompress extends UDF {

    private final Text r = new Text();

    public Text evaluate(BytesWritable bw) throws IOException {
        ByteArrayInputStream zipped = new ByteArrayInputStream(bw.getBytes());
        InflaterInputStream inflater = new InflaterInputStream(zipped);
        ByteArrayOutputStream unzipped = new ByteArrayOutputStream();
        byte[] bt = new byte[1024];
        int len;
        while ((len = inflater.read(bt)) != -1) {
            unzipped.write(bt, 0, len);
        }

        r.clear();
        r.set(unzipped.toByteArray());
        return r;
    }
}

相关问题