automattic/php-thrift-sql

一个用于通过 Thrift 连接到 Hive 或 Impala 的 PHP 库

v0.3.1 2019-07-31 15:57 UTC

This package is auto-updated.

Last update: 2024-09-20 18:18:35 UTC


README

ThriftSQL.phar 存档旨在为 PHP 提供访问 SQL-on-Hadoop 框架的方法。它将 Thrift 和各种服务包捆绑在一起,并公开一个通用接口,用于在各种框架上运行查询。

目前支持以下引擎

  • Hive -- 通过 HiveServer2 Thrift 接口,默认启用 SASL,因此必须提供用户名和密码。然而,可以在调用 connect() 之前使用 setSasl() 方法将其关闭。
  • Impala -- 通过扩展 Beeswax 协议的 Impala 服务 Thrift 接口。

版本兼容性

该库目前编译针对以下数据库版本的 Thrift 定义

使用以下编译器和基础 PHP 类

使用示例

使用此库的推荐方法是使用内存高效的迭代器从 Hive/Impala 获取结果,这将保持连接打开并逐行滚动结果。这允许逐条记录处理大型结果数据集,最大限度地减少 PHP 的内存消耗。

// Load this lib
require_once __DIR__ . '/ThriftSQL.phar';

// Try out a Hive query via iterator object
$hive = new \ThriftSQL\Hive( 'hive.host.local', 10000, 'user', 'pass' );
$hiveTables = $hive
  ->connect()
  ->getIterator( 'SHOW TABLES' );

// Try out an Impala query via iterator object
$impala = new \ThriftSQL\Impala( 'impala.host.local' );
$impalaTables = $impala
  ->connect()
  ->setOption( 'MEM_LIMIT', '2gb' ) // optionally set some query options
  ->getIterator( 'SHOW TABLES' );

// Execute the Hive query and iterate over the result set
foreach( $hiveTables as $rowNum => $row ) {
  print_r( $row );
}

// Execute the Impala query and iterate over the result set
foreach( $impalaTables as $rowNum => $row ) {
  print_r( $row );
}

// Don't forget to close socket connection once you're done with it
$hive->disconnect();
$impala->disconnect();

使用内存高效迭代器的缺点是我们只能遍历结果集一次。如果在对同一迭代器对象调用第二个 foreach,默认情况下会抛出异常,以防止在 Hive/Impala 上再次执行相同的查询,因为 PHP 客户端中不缓存结果。但是,可以将其关闭。请注意,迭代同一迭代器对象可能会产生不同的结果,因为查询将重新执行。

考虑以下示例

// Connect to hive and get a rerun-able iterator
$hive = new \ThriftSQL\Hive( 'hive.host.local', 10000, 'user', 'pass' );
$results = $hive
  ->connect()
  ->getIterator( 'SELECT UNIX_TIMESTAMP()' )
  ->allowRerun( true );

// Execute the Hive query and get results
foreach( $results as $rowNum => $row ) {
  echo "Hive server time is: {$v[0]}\n";
}

sleep(3);

// Execute the Hive query a second time
foreach( $results as $rowNum => $row ) {
  echo "Hive server time is: {$v[0]}\n";
}

将输出类似的内容

Hive server time is: 1517875200
Hive server time is: 1517875203

如果结果集很小,并且将其全部加载到 PHP 内存中会更简单,可以使用 queryAndFetchAll() 方法,它将返回包含完整结果集的纯数字多维数组。

// Try out a small Hive query
$hive = new \ThriftSQL\Hive( 'hive.host.local', 10000, 'user', 'pass' );
$hiveTables = $hive
  ->connect()
  ->queryAndFetchAll( 'SHOW TABLES' );
$hive->disconnect();

// Print out the cached results
print_r( $hiveTables );
// Try out a small Impala query
$impala = new \ThriftSQL\Impala( 'impala.host.local' );
$impalaTables = $impala
  ->connect()
  ->queryAndFetchAll( 'SHOW TABLES' );
$impala->disconnect();

// Print out the cached results
print_r( $impalaTables );

开发 & 贡献

为了重新构建此库,您需要 Composer 来安装开发依赖项,以及 Apache Thrift 来从 Thrift 接口定义文件编译客户端库。

安装开发工具后,请确保获取所有 git 子模块

$ git submodule init

然后可以使用 make 重新构建 phar

$ make clean && make phar

注意:如果您收到 BadMethodCallException 异常,它可能来自 PHP 文档中提到的任何原因之一,或者甚至是因为 open 文件描述符的软限制较低,因为 Phar::compressfiles 会保留所有文件直到它写入压缩的 phar。