Skip to content

Instantly share code, notes, and snippets.

@ZenBowman
Last active April 24, 2017 07:30
Show Gist options
  • Save ZenBowman/9492608 to your computer and use it in GitHub Desktop.
Save ZenBowman/9492608 to your computer and use it in GitHub Desktop.
Using the Hive Thrift Client in Java and Scala - an example
import junit.framework.TestCase;
import org.apache.hadoop.hive.service.HiveClient;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import java.util.List;
/**
* Created by psamtani on 3/11/14.
*/
public class TestThriftClientJava extends TestCase {
public void testThriftClient() throws TException {
final TSocket tSocket = new TSocket(HIVE_SERVER_LOCATION, 10098);
final TProtocol protocol = new TBinaryProtocol(tSocket);
final HiveClient client = new HiveClient(protocol);
tSocket.open();
client.execute("show tables");
final List<String> results = client.fetchAll();
for (String result : results) {
System.out.println(result);
}
tSocket.close();
}
}
import junit.framework.TestCase
import org.apache.hadoop.hive.service.HiveClient
import org.apache.thrift.transport.{TTransport, TSocket}
import org.apache.thrift.protocol.TBinaryProtocol
/**
* Created by psamtani on 3/11/14.
*
* Requires the following maven packages:
* 1) org.apache.hive:hive-jdbc:0.12.0
*/
class ThriftClientTest extends TestCase {
def testThriftClient() {
val transport = new TSocket(HIVE_SERVER_LOCATION, 10098)
val protocol = new TBinaryProtocol(transport)
val hiveClient = new HiveClient(protocol)
transport.open()
hiveClient.execute("show tables")
hiveClient.fetchAll().toArray.foreach(Console.println(_))
}
}
@asw456
Copy link

asw456 commented Apr 1, 2014

does this work for both hiveserver and hiverserver2? If 2 then it requires SASL to be turned off? cheers

@AsrRajiv
Copy link

no it doesn't worked.. I tried directly connecting instead of extending an interface or another class.

Here is my code

TTransport transport;
transport = new TSocket("remmote ip address", hiveport);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
HiveClient client = new HiveClient(protocol);
client.execute("show tables");
final List results = client.fetchAll();
for (String result : results) {
System.out.println(result);
}
transport.close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment