cylab/php-spark
Composer 安装命令:
composer require cylab/php-spark
包简介
A wrapper around arrays that mimics the MapReduce methods of Apache Spark
README 文档
README
php-spark is a wrapper around arrays that mimics the MapReduce API of Apache Spark.
$data = new Dataset([1, 2, 3, 4]);
$result = $data
->map(function ($v) {
return 2 * $v;
})
->reduce(function ($v, $agg) {
return $agg + $v;
});
$result == 20;
php-spark is NOT a PHP driver for Apache Spark (and I wish this would exist).
Dataset
A dataset is an immutable array of data. It is the equivalent of Spark RDD (Resilient Distributed Dataset).
use Cylab\Spark\Dataset;
$d = new Dataset([1, 2, 3, 4]);
var_dump($d->collect());
Transformations
Transformations return another dataset.
| Method | Description |
|---|---|
| map(func) | Return a new distributed dataset formed by passing each element of the source through a function func. |
| distinct() | Return a new dataset that contains the distinct elements of the source dataset. |
| reduceByKey(func) | When called on a dataset of (K, V) pairs, returns a dataset of (K, V) pairs where the values for each key are aggregated using the given reduce function func, which must be of type (V,V) => V. |
| groupByKey() | When called on a dataset of (K, V) pairs, returns a dataset of (K, V[]) pairs. |
Actions
Actions return other types of result.
| Method | Description |
|---|---|
| reduce(func) | Aggregate the elements of the dataset using a function func (which takes two arguments and returns one). |
| collect() | Return all the elements of the dataset as an array. |
| count() | Return the number of elements in the dataset. |
| first() | Return the first element of the dataset (similar to take(1)). |
| take(n) | Return an array with the first n elements of the dataset. |
Map
Map applies the provided function to all elements in the dataset and returns a new dataset containing the result of the map operation.
$d2 = $d->map(function ($v) { return 2 * $v; });
Reduce
The reduce function you provide must take two parameters: the current value and the aggregated value.
use Cylab\Spark\Dataset;
$d = new Dataset([1, 2, 3, 4]);
$result = $d->reduce(function ($v, $agg) {
return $agg + $v;
});
$result == 10;
Tuple
Some methods expect the dataset to contain a list of <key, value> tuples.
use Cylab\Spark\Dataset;
use Cylab\Spark\Tuple;
$strings = ["foe", "bar", "foe"];
$d = new Dataset ($strings);
$d2 = $d->map(function($s) { return new Tuple($s, 1); });
ReduceByKey
For this method to work, the input dataset must be a list of <key, value> tuples. The reduce function is then applied to all elements with the same key.
$counts = $d2->reduceByKey(function ($count, $sum) {
return $sum + $count;
});
First
Get the first element of a dataset.
// Tuple<"foe", 2>
var_dump($counts->first());
Future work
- flatMap
- sample
- union
- intersection
- aggregateByKey
- sortByKey
- join
- cartesian
- takeSample
- takeOrdered
- countByKey
- saveAsObjectFile
- saveAsJsonFile
cylab/php-spark 适用场景与选型建议
cylab/php-spark 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 780 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cylab/php-spark 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cylab/php-spark 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 780
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-18