Connect to hive in PHP to execute SQL query

Source: Internet
Author: User

Conditions for using PHP to connect to hive

1. Install Thrift

#./Configure -- without-Ruby

# Make & make install

If libevent-devel is not installed, install the two dependent libraries Yum-y install libevent-devel first.

Start hive thrift after installation

#./Hive -- service hiveserver>/dev/null 2>/dev/null &

Check whether the default port 10000 of hiveserver is enabled. If yes, it indicates that the port is successfully enabled.

 

Note: The PHP version is 5.2 or later.

PHP connection to hiveCode

<? </P> <p> // path of the hive thrift dependency package in PHP connection <br/> $ globals ['thrift _ root'] = '/var/www/html/hivephp/ thrift /'; <br/> // load the required files for connecting to hive <br/> require_once $ globals ['thrift _ root']. 'packages/hive_service/thrithive. PHP '; <br/> require_once $ globals ['thrift _ root']. 'Transport/tsocket. PHP '; <br/> require_once $ globals ['thrift _ root']. 'protocol/ tbinaryprotocol. php'; <br/> // set up the T Ransport/protocol/client <br/> $ transport = new tsocket ('localhost', 10000); <br/> $ protocol = new tbinaryprotocol ($ transport ); <br/> $ client = new thrithiveclient ($ Protocol); <br/> $ transport-> open (); </P> <p> // run queries, metadata calletc </P> <p> $ client-> execute ('add JAR/usr/local/hadoop/hive/lib/hive-contrib-0.7.0-cdh3u0.jar '); <br/> $ client-> execute ('select count (1) From apilog'); <br/> var_dump ($ Client-> fetchall (); <br/> $ transport-> close (); </P> <p >?>

 

Save the above Code as the WWW path/var/www/html/from test. php to Apache and put the dependency package of PHP to/var/www/html/hivephp/thrift/

PHP connection to hive lib package http://download.csdn.net/source/3409880

Open the browser and browse http: // localhost/test. php to view the query results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.