You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by Apache Wiki <wi...@apache.org> on 2009/03/27 16:25:24 UTC

[Hadoop Wiki] Trivial Update of "Hive/HiveClient" by RaghothamMurthy

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Hadoop Wiki" for change notification.

The following page has been changed by RaghothamMurthy:
http://wiki.apache.org/hadoop/Hive/HiveClient

New page:
This page describes the different clients supported by Hive. The command line client currently only supports an embedded server. The JDBC and thrift-java clients support both embedded and standalone servers. Clients in other languages only support standalone servers. For details about the standalone server see [wiki:Self:Hive/HiveServer HiveServer].

= Command line =
Operates in embedded mode only, i.e., it needs to have access to the hive libraries. For more details see [wiki:Self:Hive/GettingStarted GettingStarted].

= JDBC =
For embedded mode, uri is just "jdbc:hive://". For standalone server, uri is "jdbc:hive://host:port/dbname" where host and port are determined by where the hive server is run. For example, "jdbc:hive://localhost:10000/default". Currently, the only dbname supported is "default".

{{{

import java.sql.*;

class HiveJdbcClient {
public static void main(String[] args) {
  String uri = "jdbc:hive://"; // for embedded mode
  String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
  // load the hive jdbc driver
  Class.forName(driverName);
  // currently do not support username and password
  Connection con = DriverManager.getConnection(uri, ""/*user*/, ""/*passwd*/); 
  Statement stmt = con.createStatement();

  // DDL Statement
  stmt.executeQuery("CREATE TABLE r(a STRING, b INT, c DOUBLE)");
  stmt.executeQuery("CREATE TABLE s(a STRING, b INT, c DOUBLE)");

  // Metadata 
  ResultSet res = stmt.executeQuery("SHOW TABLES");
  // fetch results
  while (res.next()) {
   // Column indexes start from 1
   System.out.println("Table name: " + res.getString(1));
  }

  // Metadata 
  ResultSet res = stmt.executeQuery("DESCRIBE r");
  while (res.next()) {
   System.out.println("Column Name: " + res.getString(1));
   System.out.println("Column Type: " + res.getString(2));
   System.out.println("Comment: " + res.getString(3));
  }

  // DML Statement
  stmt.executeQuery("LOAD TABLE LOCAL INPATH '/path/to/file' INTO TABLE r");

  // Select
  res = stmt.executeQuery("SELECT * FROM r");
  while (res.next()) {
   System.out.println(res.getString(1));
   System.out.println(res.getInt(2));
   System.out.println(res.getDouble(3));
  }

  // Insert
  res = stmt.executeQuery("INSERT OVERWRITE TABLE s SELECT * FROM r WHERE b > 10");

 }
}
}}}

= Python =
Set PYTHONPATH to build/dist/lib/py
{{{
#!/usr/bin/env python

import sys

from hive import ThriftHive
from hive.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from 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 (row = client.fetchOne()):
      print row
    client.execute("SELECT * FROM r")
    print client.fetchAll()

    transport.close()

except Thrift.TException, tx:
    print '%s' % (tx.message)
}}}

= PHP

= Thrift Java Client =

= ODBC =
In the works.

= Thrift C++ Client
In the works.