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();【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!