Home >Database >Mysql Tutorial >ApacheHive一点一点进步(2)–HIVEJDBC

ApacheHive一点一点进步(2)–HIVEJDBC

WBOY
WBOYOriginal
2016-06-07 16:29:41999browse

Hive提供了多种方式进行数据的访问。其中对 java 的支持是最好的,而且是其最原生的支持。传说中的JDBC。哈哈! 在 hive 安装目录下的lib目录中有 hive - jdbc -0.8.1.jar 。以0.8版本的为例来介绍。 当然了,也别忘了要通过hive的server方式将hive启动起来

Hive提供了多种方式进行数据的访问。其中对java的支持是最好的,而且是其最原生的支持。传说中的JDBC。哈哈!

hive安装目录下的lib目录中有hive-jdbc-0.8.1.jar 。以0.8版本的为例来介绍。

当然了,也别忘了要通过hive的server方式将hive启动起来。命令就不在这里介绍了。

以下是官网提供的一段示例,使用起来比较简单。client端支持的语法在这里都是支持的。

而且可以通过这个进行环境变量设置,这个设置并不会影响server端,只在本次会话中生效,所以不用担心任务间影响。

Java

import java.sql.SQLException;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.sql.DriverManager; public class HiveJdbcClient {  private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";   /** * @param args * @throws SQLException   */  public static void main(String[] args) throws SQLException {      try {      Class.forName(driverName);    } catch (ClassNotFoundException e) {      // TODO Auto-generated catch block      e.printStackTrace();      System.exit(1);    }    Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");    Statement stmt = con.createStatement();    String tableName = "testHiveDriverTable";    stmt.executeQuery("drop table " + tableName);    ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");    // show tables    String sql = "show tables '" + tableName + "'";    System.out.println("Running: " + sql);    res = stmt.executeQuery(sql);    if (res.next()) {      System.out.println(res.getString(1));    }    // describe table    sql = "describe " + tableName;    System.out.println("Running: " + sql);    res = stmt.executeQuery(sql);    while (res.next()) {      System.out.println(res.getString(1) + "\t" + res.getString(2));    }     // load data into table    // NOTE: filepath has to be local to the hive server    // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line    String filepath = "/tmp/a.txt";    sql = "load data local inpath '" + filepath + "' into table " + tableName;    System.out.println("Running: " + sql);    res = stmt.executeQuery(sql);     // select * query    sql = "select * from " + tableName;    System.out.println("Running: " + sql);    res = stmt.executeQuery(sql);    while (res.next()) {      System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));    }     // regular hive query    sql = "select count(1) from " + tableName;    System.out.println("Running: " + sql);    res = stmt.executeQuery(sql);    while (res.next()) {      System.out.println(res.getString(1));    }  }}

Python

#!/usr/bin/env python import sys from hive import ThriftHivefrom hive.ttypes import HiveServerExceptionfrom thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol try:    transport = TSocket.TSocket('localhost', 10000)    transport = TTransport.TBufferedTransport(transport)    protocol = TBinaryProtocol.TBinaryProtocol(transport)     client = ThriftHive.Client(protocol)    transport.open()     client.execute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")    client.execute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")    client.execute("SELECT * FROM r")    while (1):      row = client.fetchOne()      if (row == None):        break      print row    client.execute("SELECT * FROM r")    print client.fetchAll()     transport.close() except Thrift.TException, tx:    print '%s' % (tx.message)

PHP

<?php // set THRIFT_ROOT to php directory of the hive distribution$GLOBALS&#91;'THRIFT_ROOT'&#93; = '/lib/php/';// load the required files for connecting to Hiverequire_once $GLOBALS&#91;'THRIFT_ROOT'&#93; . 'packages/hive_service/ThriftHive.php';require_once $GLOBALS&#91;'THRIFT_ROOT'&#93; . 'transport/TSocket.php';require_once $GLOBALS&#91;'THRIFT_ROOT'&#93; . 'protocol/TBinaryProtocol.php';// Set up the transport/protocol/client$transport = new TSocket&#40;'localhost', 10000&#41;;$protocol = new TBinaryProtocol&#40;$transport&#41;;$client = new ThriftHiveClient&#40;$protocol&#41;;$transport->open(); // run queries, metadata calls etc$client->execute('SELECT * from src');var_dump($client->fetchAll());$transport->close();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn