PHPackages                             automattic/php-thrift-sql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. automattic/php-thrift-sql

ActiveLibrary[Database &amp; ORM](/categories/database)

automattic/php-thrift-sql
=========================

A PHP library for connecting to Hive or Impala over Thrift

v0.3.1(6y ago)11467.4k↓76.4%41[6 issues](https://github.com/Automattic/php-thrift-sql/issues)1GPL-2.0-or-laterPHPPHP &gt;=5.5.0

Since Feb 17Pushed 5y ago119 watchersCompare

[ Source](https://github.com/Automattic/php-thrift-sql)[ Packagist](https://packagist.org/packages/automattic/php-thrift-sql)[ Docs](https://github.com/Automattic/php-thrift-sql)[ RSS](/packages/automattic-php-thrift-sql/feed)WikiDiscussions master Synced yesterday

READMEChangelog (8)Dependencies (1)Versions (7)Used By (1)

PHP ThriftSQL
=============

[](#php-thriftsql)

The `ThriftSQL.phar` archive aims to provide access to SQL-on-Hadoop frameworks for PHP. It bundles Thrift and various service packages together and exposes a common interface for running queries over the various frameworks.

Currently the following engines are supported:

- *Hive* -- Over the HiveServer2 Thrift interface, SASL is enabled by default so username and password must be provided however this can be turned off with the `setSasl()` method before calling `connect()`.
- *Impala* -- Over the Impala Service Thrift interface which extends the Beeswax protocol.

Version Compatibility
---------------------

[](#version-compatibility)

This library is currently compiled against the Thrift definitions of the following database versions:

- Apache Hive `1.1.0` ([Mar 2015](https://github.com/apache/hive/tree/release-1.1.0))
- Apache Impala `2.12.0` ([Apr 2018](https://github.com/apache/impala/tree/2.12.0))

Using the compiler and base PHP classes of:

- Apache Thrift `0.12.0` ([Oct 2018](https://github.com/apache/thrift/tree/v0.12.0))

Usage Example
-------------

[](#usage-example)

The recommended way to use this library is to get results from Hive/Impala via the memory efficient iterator which will keep the connection open and scroll through the results a couple rows at a time. This allows the processing of large result datasets one record at a time minimizing PHP's memory consumption.

```
// 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();
```

The downside to using the memory efficient iterator is that we can only iterate over the result set once. If a second `foreach` is called on the same iterator object an exception is thrown by default to prevent the same query from executing on Hive/Impala again as results are not cached within the PHP client. This can be turned off however be aware iterating over the same iterator object may produce different results as the query is rerun.

Consider the following example:

```
// 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";
}
```

Which will output something like:

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

```

If the result set is small and it would be easier to load all of it into PHP memory the `queryAndFetchAll()` method can be used which will return a plain numeric multidimensional array of the full result set.

```
// 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 );
```

Developing &amp; Contributing
-----------------------------

[](#developing--contributing)

In order to rebuild this library you will need [Composer](https://getcomposer.org/) to install dev dependencies and [Apache Thrift](https://thrift.apache.org/) to compile client libraries from the Thrift interface definition files.

Once dev tools are installed, make sure you get all git submodules:

```
$ git submodule init

```

And then the phar can be rebuilt using `make`:

```
$ make clean && make phar

```

NOTE: If you get a `BadMethodCallException`, it may come from any of [the reasons mentioned in the PHP doc](https://www.php.net/manual/en/phar.compressfiles.php#refsect1-phar.compressfiles-errors), or even a low soft limit on open file descriptors since `Phar::compressfiles` keeps [all files opened until it writes the compressed phar](https://github.com/netz98/n98-magerun/issues/714#issuecomment-269224969).

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community29

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~325 days

Recently: every ~136 days

Total

6

Last Release

2529d ago

PHP version history (2 changes)v0.1.2PHP &gt;=5.3.0

v0.3.0PHP &gt;=5.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7c5869ecbb8e0eac7e8b8e0f3cf7bdd8d5fcdc4abc10a72281872c53f8639d44?d=identicon)[automattic](/maintainers/automattic)

![](https://avatars.githubusercontent.com/u/373804?v=4)[Xiao Yu](/maintainers/xyu)[@xyu](https://github.com/xyu)

---

Top Contributors

[![xyu](https://avatars.githubusercontent.com/u/373804?v=4)](https://github.com/xyu "xyu (159 commits)")[![coreh](https://avatars.githubusercontent.com/u/418473?v=4)](https://github.com/coreh "coreh (6 commits)")[![bperson](https://avatars.githubusercontent.com/u/1145270?v=4)](https://github.com/bperson "bperson (3 commits)")[![joostshao](https://avatars.githubusercontent.com/u/3615951?v=4)](https://github.com/joostshao "joostshao (1 commits)")[![withinboredom](https://avatars.githubusercontent.com/u/1883296?v=4)](https://github.com/withinboredom "withinboredom (1 commits)")

---

Tags

databasehiveimpalaphpsqlthriftdatabasesqlthrifthiveImpala

### Embed Badge

![Health badge](/badges/automattic-php-thrift-sql/health.svg)

```
[![Health](https://phpackages.com/badges/automattic-php-thrift-sql/health.svg)](https://phpackages.com/packages/automattic-php-thrift-sql)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k605.0M6.8k](/packages/doctrine-dbal)[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[catfan/medoo

The lightweight PHP database framework to accelerate development

5.0k1.6M204](/packages/catfan-medoo)[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k6.0M77](/packages/ifsnop-mysqldump-php)[usmanhalalit/pixie

A lightweight, expressive, framework agnostic query builder for PHP.

6882.2M16](/packages/usmanhalalit-pixie)[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4883.1M39](/packages/aura-sqlquery)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
