基于PHP扩展Phpy实现Apache ORC高效读写,搞定大数据处理

大数据 Hadoop
Apache ORC 是一种专为 Hadoop 生态系统设计的列式存储文件格式,它具备高效的数据压缩和快速的查询性能。由于采用列式存储,ORC 格式在处理大规模数据时能够显著减少 I/O 开销,提高数据处理效率,广泛应用于大数据处理框架如 Apache Hive、Apache Spark 等。

概述

用于读取和写入 Apache ORC 文件格式的 PHP 库。它使用 swoole/phpy 扩展来调用 Python 模块 PyORC 以实现相关功能。

Apache ORC

Apache ORC 是一种专为 Hadoop 生态系统设计的列式存储文件格式,它具备高效的数据压缩和快速的查询性能。由于采用列式存储,ORC 格式在处理大规模数据时能够显著减少 I/O 开销,提高数据处理效率,广泛应用于大数据处理框架如 Apache Hive、Apache Spark 等。

特性:

  • 读取 ORC 文件:PyORC 允许 Python 程序从 ORC 文件中读取数据。它支持按行或按列读取数据,并且可以根据需要选择读取特定的列,从而避免不必要的数据加载,提升读取效率。
  • 写入 ORC 文件:能够将 Python 中的数据(如列表、字典等)以 ORC 格式写入文件。在写入过程中,可以指定数据的模式(schema),包括列名、数据类型等,以确保数据的正确存储。
  • 数据模式处理:支持对 ORC 文件的数据模式进行解析和操作。可以获取文件的模式信息,也可以自定义模式来创建新的 ORC 文件。

Swoole Phpy

phpy 是识沃团队最新推出的开源项目,目标是为 PHP 引入 Python 生态,来弥补 PHP 生态的空缺和不足。phpy 使得 PHP 可以调用所有 Python 的包。

它为 PHP 开发者提供了一种在 PHP 代码里调用 Python 模块和函数的能力,搭建起了 PHP 和 Python 这两种编程语言之间的桥梁,极大地扩展了 PHP 的功能边界。

特性:

  1. 无缝调用 Python 代码:借助 phpy 扩展,PHP 开发者能够在 PHP 脚本里直接调用 Python 编写的函数、类和模块。这意味着可以利用 Python 丰富的科学计算、机器学习、数据分析等领域的库,比如 NumPy、SciPy、TensorFlow 等,而无需离开 PHP 开发环境。
  2. 数据类型映射:phpy 实现了 PHP 和 Python 数据类型之间的自动映射。例如,PHP 的数组可以自动转换为 Python 的列表或字典,PHP 的标量类型(如整数、浮点数、字符串等)也能正确地转换为 Python 对应的类型,反之亦然。这使得在两种语言之间传递数据变得非常方便。
  3. 高性能:phpy 在设计上注重性能优化,通过高效的底层实现,减少了 PHP 和 Python 之间的交互开销,确保数据传递和函数调用的高效性。

安装

Composer安装

composer require workbunny/php-orc

查看帮助

./vendor/bin/php-orc --help
Description:
List commands

Usage:
list [options] [--] [<namespace>]

Arguments:
namespace             Thenamespacename

Options:
      --raw             Tooutputrawcommandlist
      --format=FORMAT   Theoutputformat (txt, xml, json, ormd) [default: "txt"]
      --short           Toskipdescribingcommands' arguments
  -h, --help            Displayhelpforthegivencommand. Whennocommandisgivendisplayhelpforthelistcommand
      --silent          Donotoutputanymessage
  -q, --quiet           Onlyerrorsaredisplayed. Allotheroutputissuppressed
  -V, --version         Displaythisapplicationversion
      --ansi|--no-ansiForce (ordisable --no-ansi) ANSIoutput
  -n, --no-interactionDonotaskanyinteractivequestion
  -v|vv|vvv, --verboseIncreasetheverbosityofmessages: 1 fornormaloutput, 2 formoreverboseoutputand 3 fordebug

依赖环境安装

安装Python

通过.vendor/bin/php-orc install:python安装

./vendor/bin/php-orc install:python
[>] Checking and installing Python 3.10+ ...
[i] Python 3.10+ is already installed.
[√] Python installation complete.

安装成功后,查看安装成功的python版本号

# python -V
Python 3.12.8

安装PHPy拓展

通过.vendor/bin/php-orc install:phpy安装

./vendor/bin/php-orc install:phpy
[?] Please specify the installation directory (default: .runtime): 

[>] Downloading the latest source code ...
Cloning into '/var/www/webman/workerman5.1/.runtime/swoole_phpy_latest'...
remote: Enumerating objects: 827, done.
remote: Counting objects: 100% (827/827), done.
remote: Compressing objects: 100% (516/516), done.
remote: Total 827 (delta 327), reused 642 (delta 289), pack-reused 0 (from 0)
Receiving objects: 100% (827/827), 560.36 KiB | 1.74 MiB/s, done.
Resolving deltas: 100% (327/327), done.
Updating files: 100% (749/749), done.
warning: the following paths have collided (e.g. case-sensitive paths
on a case-insensitive filesystem) and only one from the same
colliding group is in the working tree:
'lib/python/ast/BoolOp.php'
'lib/python/ast/boolop.php'
'lib/python/ast/ExceptHandler.php'
'lib/python/ast/excepthandler.php'
'lib/python/ast/Expr.php'
'lib/python/ast/expr.php'
'lib/python/ast/Mod.php'
'lib/python/ast/mod.php'
'lib/python/ast/Slice.php'
'lib/python/ast/slice.php'
'lib/python/ast/UnaryOp.php'
'lib/python/ast/unaryop.php'
[>] Installing dependencies...
[>] fetch https://mirrors.ustc.edu.cn/alpine/v3.21/main/x86_64/APKINDEX.tar.gz
[>] fetch https://mirrors.ustc.edu.cn/alpine/v3.21/community/x86_64/APKINDEX.tar.gz
[>] OK: 422 MiB in 104 packages
...
[√] PHPy installation completed successfully.

安装成功后,查看安装成功的phpy扩展和版本号

# php -m |grep phpy
phpy

查看phpy版本号

# php --ri phpy

phpy

phpy support => enabled
Copyright => 上海识沃网络科技有限公司
Email => service@swoole.com
Website => https://www.swoole.com/
Extension Version => 1.0.10
Python Version => 3.12.8 (main, Dec  7 2024, 05:56:13) [GCC 14.2.0]

安装pyorc模块

通过.vendor/bin/php-orc install:pyorc安装

./vendor/bin/php-orc install:pyorc
[>] Checking and installing Python PyORC ...
[>] Upgrading pip ...
[i] pip is already up-to-date.
[>] Installing TZData ...
WARNING: Package(s) not found: tzdata
[>] Collecting tzdata
[>] Downloading tzdata-2025.1-py2.py3-none-any.whl.metadata (1.4 kB)
[>] Downloading tzdata-2025.1-py2.py3-none-any.whl (346 kB)
[>] Installing collected packages: tzdata
[>] Successfully installed tzdata-2025.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable.It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.
[>] Installing PyORC-latest ...
WARNING: Package(s) not found: pyorc
[>] Collecting pyorc
[>] Downloading pyorc-0.10.0-cp312-cp312-musllinux_1_2_x86_64.whl.metadata (3.0 kB)
[>] Downloading pyorc-0.10.0-cp312-cp312-musllinux_1_2_x86_64.whl (3.8 MB)
[>] ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.8/3.8 MB 8.1 MB/s eta 0:00:00
[>] Installing collected packages: pyorc
[>] Successfully installed pyorc-0.10.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable.It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.
[√] Python and PyORC installation complete.

开源技术小栈

安装过程如果提示以下错误:

[>] Checking and installing Python PyORC ...
[>] Upgrading pip ...
sh: pip: not found

请执行以下命令,安装 pip

apk update
apk add py3-pip

执行案例

PHPY案例

os.php文件

<?php
/**
 * @desc 打印操作系统信息
 * @author Tinywan(ShaoBo Wan)
 */
declare(strict_types=1);

function main(): void
{
    $m = PyCore::import("os");
    var_dump($m instanceof PyObject);
    $rs = $m->uname();
    echo $rs . PHP_EOL;
    echo $rs->version . PHP_EOL;
}

main();

执行输出

# php os.php 
bool(true)
posix.uname_result(sysname='Linux', nodename='b6fb15fcfed9', release='5.10.102.1-microsoft-standard-WSL2', version='#1 SMP Wed Mar 2 00:30:59 UTC 2022', machine='x86_64')
#1 SMP Wed Mar 2 00:30:59 UTC 2022

PyORC案例

文件读取 reader.php

<?php
/**
 * @desc 文件读取
 * @author Tinywan(ShaoBo Wan)
 * @date 2025/2/26 22:51
 */
declare(strict_types=1);

usefunctionWorkbunny\PhpOrc\open;

require_once__DIR__ . '/vendor/autoload.php';

$reader = new \Workbunny\PhpOrc\Reader(open(__DIR__ . '/vendor/workbunny/php-orc/examples/example-php.orc','rb'));
// header schema
var_dump(
// 总行数
    $reader->count(),
    // 头格式
    $reader->schema(),
    // 压缩类型
    $reader->compression(),
    // 元数据
    $reader->userMetadata(),
    // 写入方式
    $reader->writerId(),
    // 写入版本类型
    $reader->writerVersion(),
    // 基座版本
    $reader->softwareVersion(),
    // 格式版本
    $reader->formatVersion()
);
// 迭代
$reader->iteration(function ($i, $row) {
    var_dump($i, $row);
});

执行输出

# php reader.php 
int(3)
string(52) "struct<id:int,group:string,name:string,email:string>"
int(0)
array(3) {
'group' =>
  string(9) "workbunny"
'user' =>
  string(9) "chaz6chez"
'email' =>
  string(25) "chaz6chez1993@outlook.com"
}
string(14) "ORC_CPP_WRITER"
int(6)
string(13) "ORC C++ 2.1.0"
array(2) {
  [0] =>
  int(0)
  [1] =>
  int(12)
}
int(0)
array(4) {
  [0] =>
  int(1)
  [1] =>
  string(9) "workbunny"
  [2] =>
  string(5) "test1"
  [3] =>
  string(19) "test1@workbunny.com"
}
int(1)
array(4) {
  [0] =>
  int(2)
  [1] =>
  string(9) "workbunny"
  [2] =>
  string(5) "test2"
  [3] =>
  string(19) "test2@workbunny.com"
}
int(2)
array(4) {
  [0] =>
  int(3)
  [1] =>
  string(9) "workbunny"
  [2] =>
  string(5) "test3"
  [3] =>
  string(19) "test3@workbunny.com"
}

责任编辑:武晓燕 来源: 开源技术小栈
相关推荐

2025-02-05 08:13:48

Go语言范式

2018-12-07 14:50:35

大数据数据采集数据库

2020-11-02 15:56:04

大数据数据库技术

2023-08-25 15:13:16

大数据云计算

2016-11-14 18:17:42

Apache Beam大数据

2021-07-20 15:37:37

数据开发大数据Spark

2017-07-21 14:22:17

大数据大数据平台数据处理

2023-07-12 12:02:06

WOT大数据流式数据湖

2021-04-02 12:55:14

数据处理Apache Falc开发

2020-07-22 08:13:22

大数据

2022-11-17 11:52:35

pandasPySpark大数据

2012-11-28 20:56:03

浪潮一体机浪潮大数据

2023-10-05 12:43:48

数据处理

2023-11-29 13:56:00

数据技巧

2015-10-16 09:50:10

2015-11-09 09:58:31

大数据Lambda架构

2018-03-08 12:17:38

大数据HPCHadoop

2024-05-07 08:16:17

2016-02-17 09:55:25

SMACK数据处理可扩展架构

2011-09-01 15:12:43

SQL ServerHadoop
点赞
收藏

51CTO技术栈公众号