ApacheHive一点一点进步(2)–HIVEJDBC
程序员文章站
2022-04-20 13:48:04
...
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
open(); // run queries, metadata calls etc$client->execute('SELECT * from src');var_dump($client->fetchAll());$transport->close();
原文地址:ApacheHive一点一点进步(2)–HIVEJDBC, 感谢原作者分享。