hive thrift demo001

孔明
• 阅读 980



import org.apache.hadoop.hive.common.auth.HiveAuthUtils;
import org.apache.hive.jdbc.Utils.JdbcConnectionParams;
import org.apache.hive.service.auth.PlainSaslHelper;
import org.apache.hive.service.rpc.thrift.*;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.security.auth.Subject;
import javax.security.sasl.SaslException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * HiveConnection.
 *
 */
public class HiveXConn {
    public static final Logger LOG = LoggerFactory.getLogger(HiveXConn.class.getName());

    private String jdbcUriString;
    private String host;
    private int port;

    private JdbcConnectionParams connParams;

    private TTransport transport;

    // TODO should be replaced by CliServiceClient
    private TCLIService.Iface client;
    private TSessionHandle sessHandle = null;
    private int loginTimeout = 0;
    private TProtocolVersion protocol;
    private int fetchSize = 1000;






    public HiveXConn(String uri, Properties info) throws Exception {



        // JDBC URL: jdbc:hive2://<host>:<port>/dbName;sess_var_list?hive_conf_list#hive_var_list
        // each list: <key1>=<val1>;<key2>=<val2> and so on
        // sess_var_list -> sessConfMap
        // hive_conf_list -> hiveConfMap
        // hive_var_list -> hiveVarMap
        host = "192.168.11.9";
        port = 10000;


        // open the client transport
        openTransport();
        // set up the client
        client = new TCLIService.Client(new TBinaryProtocol(transport));
        // open client session
        openSession();

        client = newSynchronizedClient(client);



    }


    public static TCLIService.Iface newSynchronizedClient(
            TCLIService.Iface client) {
        return (TCLIService.Iface) Proxy.newProxyInstance(
                org.apache.hive.jdbc.HiveConnection.class.getClassLoader(),
                new Class [] { TCLIService.Iface.class },
                new SynchronizedHandler(client));
    }




    private void openTransport() throws Exception {

        transport =  createBinaryTransport();
        if (!transport.isOpen()) {
            transport.open();
        }
    }

//    https://github.com/ds112/hbase-on-windows/blob/77e5f31715f3b4a258f212b242cd698ad983af60/Samples/Java/Hive/ThriftAPI/src/main/java/Client.java


    private TTransport createBinaryTransport() throws SaslException {
        // we are using PLAIN Sasl connection with user/password
        String userName = "hive";
        String passwd = "hive";

        TTransport  socketTransport = HiveAuthUtils.getSocketTransport(host, port, loginTimeout);
        transport = PlainSaslHelper.getPlainTransport(userName, passwd, socketTransport);
        return transport;
    }



    private void openSession() throws SQLException {
        TOpenSessionReq openReq = new TOpenSessionReq();

        Map<String, String> openConf = new HashMap<String, String>();
        openConf.put("use:database", "default");
        // set the fetchSize
        openConf.put("set:hiveconf:hive.server2.thrift.resultset.default.fetch.size",
                Integer.toString(fetchSize));
        openReq.setConfiguration(openConf);


        try {
            TOpenSessionResp openResp = client.OpenSession(openReq);

            protocol = openResp.getServerProtocolVersion();

            sessHandle = openResp.getSessionHandle();

            TExecuteStatementReq execReq = new TExecuteStatementReq(sessHandle, "SELECT * FROM test limit 10");
//            TExecuteStatementReq execReq = new TExecuteStatementReq(sessHandle, "show tables");
            TExecuteStatementResp execResp = client.ExecuteStatement(execReq);

            TOperationHandle stmtHandle = execResp.getOperationHandle();


            TFetchResultsReq fetchReq = new TFetchResultsReq(stmtHandle, TFetchOrientation.FETCH_FIRST, 100);
            TFetchResultsResp resultsResp = client.FetchResults(fetchReq);

            TRowSet resultsSet = resultsResp.getResults();

            List<List> tableResult = new ArrayList<List>();
            if (resultsSet.getColumns() != null && resultsSet.getColumns().size() != 0)
            {
                List<TColumn> resultColumns = resultsSet.getColumns();
                for(int i=0;i<resultColumns.size();i++ )
                {
                    TColumn resultRow = resultColumns.get(i);
                    List result = new ArrayList();
                    if(resultRow.isSetBinaryVal() == true)
                    {
                        result = resultRow.getBinaryVal().getValues();
                    }
                    else if(resultRow.isSetBoolVal() == true)
                    {
                        result =resultRow.getBoolVal().getValues();
                    }
                    else if(resultRow.isSetByteVal() == true)
                    {
                        result =resultRow.getByteVal().getValues();
                    }
                    else if(resultRow.isSetDoubleVal() == true)
                    {
                        result=resultRow.getDoubleVal().getValues();
                    }
                    else if(resultRow.isSetI16Val() == true)
                    {
                        result =resultRow.getI16Val().getValues();
                    }
                    else if(resultRow.isSetI32Val() == true)
                    {
                        result =resultRow.getI32Val().getValues();
                    }
                    else if(resultRow.isSetI64Val() == true)
                    {
                        result =resultRow.getI64Val().getValues();
                    }
                    else if(resultRow.isSetStringVal()==true)
                    {
                        result = resultRow.getStringVal().getValues();
                    }
                    tableResult.add(result);
                }
            }
            for(int i=0;i<tableResult.get(0).size();i++)
            {
                for (List list : tableResult) {
                    System.out.print(list.get(i).toString() + "\t");
                }
                System.out.println();
            }



            TCloseOperationReq closeReq = new TCloseOperationReq();
            closeReq.setOperationHandle(stmtHandle);
            client.CloseOperation(closeReq);
            TCloseSessionReq closeConnectionReq = new TCloseSessionReq(sessHandle);
            client.CloseSession(closeConnectionReq);

            transport.close();


        } catch (TException e) {
            LOG.error("Error opening session", e);
            throw new SQLException("Could not establish connection to "
                    + jdbcUriString + ": " + e.getMessage(), " 08S01", e);
        }
    }




    private static class SynchronizedHandler implements InvocationHandler {
        private final TCLIService.Iface client;
        private final ReentrantLock lock = new ReentrantLock(true);

        SynchronizedHandler(TCLIService.Iface client) {
            this.client = client;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object [] args)
                throws Throwable {
            try {
                lock.lock();
                return method.invoke(client, args);
            } catch (InvocationTargetException e) {
                // all IFace APIs throw TException
                if (e.getTargetException() instanceof TException) {
                    throw (TException)e.getTargetException();
                } else {
                    // should not happen
                    throw new TException("Error in calling method " + method.getName(),
                            e.getTargetException());
                }
            } catch (Exception e) {
                throw new TException("Error in calling method " + method.getName(), e);
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws Exception{
        HiveXConn d1 = new HiveXConn("",null);


    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Easter79 Easter79
3年前
typeScript数据类型
//布尔类型letisDone:booleanfalse;//数字类型所有数字都是浮点数numberletdecLiteral:number6;lethexLiteral:number0xf00d;letbinaryLiteral:number0b101
Wesley13 Wesley13
3年前
VBox 启动虚拟机失败
在Vbox(5.0.8版本)启动Ubuntu的虚拟机时,遇到错误信息:NtCreateFile(\\Device\\VBoxDrvStub)failed:0xc000000034STATUS\_OBJECT\_NAME\_NOT\_FOUND(0retries) (rc101)Makesurethekern
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
3年前
MBR笔记
<bochs:100000000000e\WGUI\Simclientsize(0,0)!stretchedsize(640,480)!<bochs:2b0x7c00<bochs:3c00000003740i\BIOS\$Revision:1.166$$Date:2006/08/1117