PHP 连接 Hive 执行 SQL 查询

Hive 中的 Thrift 脚本有两个版本的,一个有命名空间,一个没有命名空间,下面使用的是没有命名空间的版本。

无命名空间的PEAR

$ cd /opt/hive/lib/php/packages/
$ mv hive_service hive_service.bak
$ mv hive_service.bak/hive_service ./
$ cd /opt/hive/lib/php/packages/queryplan/queryplan
$ cp queryplan_types.php ../

$ cd /opt/hive/lib/php/packages/hive_metastore/hive_metastore
$ cp ThriftHiveMetastore.php ../

PHP

<?php
$GLOBALS['THRIFT_ROOT'] = '/opt/hive/lib/php';

require_once '/opt/hive/lib/php/autoload.php';
require_once '/opt/hive/lib/php/Thrift.php';
require_once '/opt/hive/lib/php/packages/fb303/FacebookService.php';
require_once '/opt/hive/lib/php/transport/TSocket.php';
require_once '/opt/hive/lib/php/protocol/TBinaryProtocol.php';
require_once '/opt/hive/lib/php/packages/hive_service/ThriftHive.php';

/**
 * @author chenliujin <liujin.chen@qq.com>
 * @since 2013-08-20
 */
class Hive
{
    /**
     * @author chenliujin <liujin.chen@qq.com>
     * @since 2013-08-20
     */
    public function query($sql)
    {

        try {
            // Set up the transport/protocol/client
            $transport = new TSocket('localhost', 10000);
            $transport->setSendTimeout(10000);
            $transport->setRecvTimeout(100000);
            $protocol = new TBinaryProtocol($transport);
            $client = new ThriftHiveClient($protocol);
            $transport->open();

            // run queries, metadata calls etc
            $client->execute($sql);
            $result = $client->fetchAll();

            $data = array();
            foreach ($result as $row) {
                $row = explode("\t", $row);
                $data[] = $row;
            }
            $transport->close();

            return $data;
        } catch (Exception $e) {
            return array();
        }
    }
}


FAQ

PHP Fatal error:  Uncaught exception 'HiveServerException' with message 'Query returned non-zero code: 40000, cause: FAILED: ParseException line 5:23 cannot recognize input near 'ab' ';' '<EOF>' in expression specification' in /opt/hive/lib/php/packages/hive_service/ThriftHive.php:618

When you write a query in the shell, you should use ';' at the end of the SQL statement, However, you CANNOT use ';' at the end of the SQL statement when you're using Java.

TSocket: timed out reading 4 bytes from localhost:10000

加大超时时间

$transport->setSendTimeout(1000000);
$transport->setRecvTimeout(100000000);
Hive执行SQL查询速度慢可能是由多种因素引起的,以下是一些常见的原因和解决策略: 1. **数据量大**:Hive基于Hadoop HDFS,处理大数据时会涉及磁盘I/O,如果数据集非常庞大,查询速度自然会变慢。可以考虑优化查询范围,只查询需要的部分,或者对数据进行分区和分桶以提高访问效率。 2. **索引不足**:如果表没有合适的索引,全表扫描可能会很慢。对于经常用于过滤条件的列,应该创建索引。 3. **查询复杂度高**:复杂的JOIN操作、子查询、窗口函数等可能导致性能下降。尽量简化查询逻辑,减少计算量。 4. **Hive Metastore性能瓶颈**:Metastore负责存储元数据,如果它过载或性能不佳,也可能影响查询速度。检查Metastore是否正常运行,如果有大量并发请求,考虑升级硬件或优化配置。 5. **资源限制**:确保Hadoop集群有足够的内存和CPU来支持Hive任务。调整YARN或Mesos的资源分配可以帮助提升性能。 6. **网络延迟**:如果数据分布在远程节点,网络延迟也可能影响查询速度。尝试将数据移动到本地节点,或优化网络环境。 7. **Hive缓存设置**:Hive的`set hive.auto.convert.join=true`和`set hive.exec.compress.output=true`之类的设置,有时会影响查询速度。检查并根据需求调整这些配置。 8. **检查表的存储格式**:文本格式(TEXTFILE)通常比二进制格式(ORC、PARQUET)慢。如果可行,可以考虑转换数据格式。 针对这些问题,需要具体情况分析,并进行针对性优化。同时,定期监控和维护Hive性能也是一个关键步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值