PredictionIO使用PHP的SDK收集数据

PredictionIO 安装:http://php2012web.blog.51cto.com/5585213/1631301

官方收集数据文档地址:https://docs.prediction.io/datacollection/eventapi/

或 https://docs.prediction.io/templates/recommendation/quickstart/
 

PredictionIO收集数据支持:Python,PHP,Ruby,Java,REST(API);其中 TEST 是原生接口(即直接通过URL地址访问服务器API)。

PredictionIO收集数据需要创建一个应用(类似数据库中创建一个库)。


进入PredictionIO安装目录;

或者把PredictionIO加载到系统PATH中

1
PATH=$PATH: /PredictionIO 安装目录 /bin export  PATH


PredictionIO数据收集应用是通过事件服务器 eventserver 接收,然后存储(默认使用Apache HBase作为数据存储),所以要正常使用API必需启动 eventserver服务,当然通过 pio-start-all 默认所有相关服务都会启动,无需使用下面的命令再次启动。

启动eventserver事件服务器:

pio eventserver [--ip <value>] [--port <value>] [--stats]

参数 --ip 是指定访问IP地址,默认为localhost。

     --port 是指定访问端口号,默认为 7070。

     --stats  是查看状态


创建应用

1
pio app new MyAppName

如:

1
2
3
4
5
6
7
8
[root@localhost /] # pio app new user
[WARN] [NativeCodeLoader] Unable to load native-hadoop library  for  your platform... using  builtin -java classes where applicable
[INFO] [HBLEvents] The table predictionio_eventdata:events_2 doesn't exist yet. Creating now...
[INFO] [App$] Initialized Event Store  for  this app ID: 2.
[INFO] [App$] Created new app:
[INFO] [App$]       Name: user
[INFO] [App$]         ID: 2
[INFO] [App$] Access Key: 1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1

注意:MyAppName 是当前应用的名称(类似数据库的库名),在SDK中 Name 与 Access Key是会使用到。


查看所有应用

1
2
3
4
[root@localhost /] # pio app list
[INFO] [App$]                 Name |   ID |                                                       Access Key | Allowed Event(s)
[INFO] [App$]                 user |    1 | 1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1 | (all)
[INFO] [App$] Finished listing 1 app(s).


删除应用

1
pio app delete MyAppName


只删除应用数据

1
pio app data-delete MyAppName

如:

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost /] # pio app data-delete user
[INFO] [App$] The data of the following app will be deleted. Are you sure?
[INFO] [App$]     App Name: user
[INFO] [App$]       App ID: 1
[INFO] [App$]  Description: None
Enter  'YES'  to proceed: YES
[WARN] [NativeCodeLoader] Unable to load native-hadoop library  for  your platform... using  builtin -java classes where applicable
[INFO] [HBLEvents] Removing table predictionio_eventdata:events_2...
[INFO] [App$] Removed Event Store  for  this app ID: 2
[INFO] [HBLEvents] The table predictionio_eventdata:events_2 doesn't exist yet. Creating now...
[INFO] [App$] Initialized Event Store  for  this app ID: 2.
[INFO] [App$] Done.

注意:中间会要求确认操作,输入YES一定要大写。


eventserver事件服务器API:


API访问地址: http://localhost:7070/events.json?accessKey=yourAccessKeyString

        yourAccessKeyString 是访问应用的 Access Key 值,当PHP SDK不能正常访问时,可以直接在浏览器中打开对应的API地址,查看服务器是否正常。


API参数:(所有名称的避开使用 $或 pio_ 开头,如"$set"或"pio_set",红色的参数名是要注意的,参数是以POST方式操作)

参数名数据类型要求功能说明
event字符串必需事件名称或特殊事件名
entityType字符串必需实体类型(类似关系数据库的表名)
entityId字符串必需实体ID(类似关系数据库的ID,必需保证唯一)
targetEntityType字符串可选与之关联的实体类型
targetEntityId字符串可选与之关系实体类型ID(类似关系数据库的关联ID)
propertiesJSON
可选该记录的相关所有属性集,以【名/值】对形式存在。
eventTime字符串可选

记录生成时间,默认是服务器当前时间,实际应用中最好直接这个时间。必需以ISO 8601格式

(如 2004-12-13T21:39:45.618Z,或2014-09-09T16:17:42.937-08:00)


PHP SDK 安装包及规范文档:https://packagist.org/packages/predictionio/predictionio

GIT库地址:

规范文档:https://github.com/PredictionIO/PredictionIO-PHP-SDK.git

安装包:https://github.com/PredictionIO/PredictionIO-PHP-SDK/tree/master


要求:

git ,php>=5.4.0 ,composer


PredictionIO 需要PHP的 cURL 扩展,编译时需要添加 --with-curl[=DIR] 选项([=DIR]是指定libcurl安装目录需要包含lib和include的目录路径。在include目录中必须有一个名为curl,包含了easy.h和curl.h的文件夹。lib文件夹里应该有一个名为libcurl.a的文件)。如果没有指定libcurl安装目录,则使用默认安装目录。

安装使用composer 需要PHP的openssl扩展,编译时需要添加 --with-openssl[=DIR] 选项。


PHP部分./configure时缺失包报错:

1
2
3
checking  for  cURL  in  default path... not found
configure: error: Please reinstall the libcurl distribution -
easy.h should be  in  /include/curl/

缺少  curl-devel


1
2
3
checking libxml2  install  dir ... no
checking  for  xml2-config path... 
configure: error: xml2-config not found. Please check your libxml2 installation.

缺少  libxml2-devel


1
2
3
checking  for  RAND_egd... no
checking  for  pkg-config...  /usr/bin/pkg-config
configure: error: Cannot  find  OpenSSL's <evp.h>

缺少  openssl-devel


安装git: http://git-scm.com/downloads

或者直接使用

1
yum  install  git


安装composer:(composer 是基于PHP的一个脚本程序)

下载地址:https://getcomposer.org/

安装:

1
curl -sS https: //getcomposer .org /installer  | php

1
php -r  "readfile('https://getcomposer.org/installer');"  | php

如果报如下类似错误:

1
2
curl: (56) SSL  read : errno -5961
Parse error: syntax error, unexpected end of  file , expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)  in  - on line 885


则可以先下载再执行安装:

1
2
3
wget https: //getcomposer .org /installer
chmod  777 installer
. /installer

如果报如下类似错误:

1
Download failed: file_get_contents(https: //getcomposer .org /composer .phar): failed to  open  stream: HTTP request failed!

那就手动下载

1
wget https: //getcomposer .org /composer .phar


说明:其实composer就是名为composer.phar一个PHP脚本程序,所谓的安装只是把这个脚本文件给下载出来。官方网站上说的局部安装则是把这个脚本文件下载到对应的待处理依赖包(含有composer.json文件)目录下再执行这个脚本,而全局安装则是把这个脚本添加到系统的PATH目录下,好让这个脚本在任意地方调用而时不受脚本所在目录影响。

全局处理可有多种方式:

例:

永久生效:(把composer直接移动到PHP的默认安装启动目录下,当然也可以指定到其它PATH目录)

1
mv  composer.phar  /usr/local/bin/composer

临时生效:(把composer追加到当前终端PATH目录中,当这个终端关闭后,操作失效,即无法再任意地方调用composer,注意composer得先修改下文件名,/var/composer/bin是composer所在目录)

1
2
mv  composer.phar composer
PATH=$PATH: /var/composer/bin ; export  PATH

注意:全局方式安装只是针对会多次使用到composer时提供一种方便,而不用每次使用composer都要下载composer.phar或移动到待依赖处理包所在目录下;而局部方式则是针对使用次数少时的一种临时处理方案,用完就不要,或者保存起来,下次用再复制过去。


下载PHP SDK包:(这个包下载后不能直接使用,必需使用composer处理依赖并下载对应的文件)

1
git clone https: //github .com /PredictionIO/PredictionIO-PHP-SDK  /var/tmp/sdk


SDK包依赖处理:

进入SDK下载所在目录

根据安装composer的方式,选择下载依赖文件:(局部处理方式,必需先下载composer.phar到当前SDK目录下)

1
php composer.phar  install

或(全局处理方式)

1
composer  install

注意:处理依赖会下载一批php文件,有时会因为网络问题报错,只能再重新调用依赖安装命令。


如下类似错,大部分是网络异常,下载文件失败。可以手动查看下对应的文件是否能成功下载。

1
2
3
4
5
   [Composer\Repository\RepositorySecurityException]                            
   The contents of https: //packagist .org /p/provider-latest $62dd7ff0a317764ec6e  
   27022b357464cbbf1275096f9028d1b0a03925c845ac2.json  do  not match its signatu  
   re. This should indicate a  man - in -the-middle attack. Try running composer a  
   gain and report this  if  you think it is a mistake.


PHP SDK收集数据(在PHP SDK安装包目录下)

创建收集文件(收集使用SDK的 EventClient 对象)

1
vim user.php


写入代码:(收集数据,是尽可能多收集,以便后续使用)

注意:Access Key的值需要与当前创建应用相同,可以使用 pio app list 查看。event,entityType,entityId 这几个字段必填且不能为空。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*   加载自动加载文件,
  *   命名空间与目录对应列表:(以当前PHP SDK安装目录为根目录)
  *   predictionio         /src/predictionio
  *   React\Promise        /vendor/react/promise/src
  *   GuzzleHttp\Stream    /vendor/guzzlehttp/streams/src
  *   GuzzleHttp\Ring      /vendor/guzzlehttp/ringphp/src
  *   GuzzleHttp           /vendor/guzzlehttp/guzzle/src
  *   
  * 注意:默认在/src/predictionio 目录下生成的 EventClient.php 只是添加部分功能,实际应用中可以去扩展这个文件。
*/
require_once ( "vendor/autoload.php" );
 
use  predictionio\EventClient;
 
//创建应用事件客户端对象,注意第一个参数是PredictionIO生成应用的Access Key ,第二个参数是访问API的地址(可以不传默认是本地,端口号是7070)。
$client  new  EventClient( '1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1' , 'http://127.0.0.1:7070' );
 
// 用户行为
$client ->createEvent( array (
    'event'  =>  'user' ,                 //事件名
    'entityType'  =>  'action' ,          //实体类型,类似表名,可以根据需求定义。
    'entityId'  => 121,                 //这条记录的ID值,最好保证唯一。
    'targetEntityType'  =>  'entity' ,    //关联实体类型,类似关联表名。
    'targetEntityId'  => 1,             //关联实体类型的ID,类似关联表记录的ID
    'properties'  =>  array (             //该记录的数据体,根据实际要求添加,尽可能的多收集标识。
        'url' =>  'http://PredictionIO.com/' ,
        'referer' =>  'http://PredictionIO.com/list/' ,
        'platform' =>  'pc' ,
        )
));
 
// 用户数据
$client ->createEvent( array (
    'event'  =>  'user' ,                 //事件名
    'entityType'  =>  'entity' ,          //实体类型,即表名。
    'entityId'  => 1,                   //实体类型记录ID。
    'properties'  =>  array (             //该记录的数据体。
        'name' =>  '张三' ,
        'age' =>  '15' ,
        'sex' =>  '男' ,
        )
));

保存后通过PHP命令行运行:

1
php user.php


如果报如下错:(可以重新启动下PredictionIO,可以查看:https://docs.prediction.io/resources/faq/

1
2
3
4
5
6
Fatal error: Uncaught exception  'predictionio\PredictionIOAPIError'  with message  'Client error response [url] http://127.0.0.1:7070/events.json?accessKey=1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1 [status code] 401 [reason phrase] Unauthorized'  in  /var/tmp/sdk/src/predictionio/BaseClient .php:59
Stack trace:
#0 /var/tmp/sdk/src/predictionio/EventClient.php(249): predictionio\BaseClient->sendRequest('POST', '/events.json?ac...', '{"event":"user"...')
#1 /var/tmp/sdk/test.php(31): predictionio\EventClient->createEvent(Array)
#2 {main}
   thrown  in  /var/tmp/sdk/src/predictionio/BaseClient .php on line 59

其它错误可以检查下PHP版本,curl扩展等是否达到要求。



收集数据调试(这些功能并不包含在SDK中)


查看是所有当前应用数据命令:(注意 accessKey 是所创建应用的 access Key 值)

1
curl -i -X GET http: //localhost :7070 /events .json?accessKey=1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1


正确的结果:

1
2
3
4
5
6
7
8
HTTP /1 .1 200 OK
Server: spray-can /1 .3.2
Date: Wed, 06 May 2015 07:15:32 GMT
Content-Type: application /json ; charset=UTF-8
Content-Length: 594
 
[{ "eventId" : "rhfb-lPDpW1R6kTf14pTiQAAAU0oEv2ygw0_hXPBpTY" , "event" : "user" , "entityType" : "entity" , "entityId" : "1" , "properties" :{ "name" : "张三" , "age" : "15" , "sex" : "男" }, "eventTime" : "2015-05-06T07:14:02.802Z" , "creationTime" : "2015-05-06T07:14:02.802Z" },
{ "eventId" : "2cmHXLcgockKGfq9gVNRPAAAAU0oEvrQlk0XBQuon0Q" , "event" : "user" , "entityType" : "action" , "entityId" : "121" , "targetEntityType" : "entity" , "targetEntityId" : "1" , "properties" :{ "url" : "http://PredictionIO.com/" , "referer" : "http://PredictionIO.com/list/" , "platform" : "pc" }, "eventTime" : "2015-05-06T07:14:02.064Z" , "creationTime" : "2015-05-06T07:14:02.064Z" }]


查看指定事件数据命令:(注意:<eventId>是要查询的事件ID,通过上面的命令中可以得到对应的事件ID,accessKey 同上)

1
curl -i -X GET http: //localhost :7070 /events/ <eventId>.json?accessKey=1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1

如:

1
curl -i -X GET http: //localhost :7070 /events/rhfb-lPDpW1R6kTf14pTiQAAAU0oEv2ygw0_hXPBpTY .json?accessKey=1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1


删除指定事件数据命令:(注意删除与查询事件数据只是请求方式不同,一个是GET一个是DELETE,参数均相同)

1
curl -i -X DELETE http: //localhost :7070 /events/ <eventId>.json?accessKey=1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1


其它API参数列表:

startTime   开始时间,ISO8601格式。过滤条件为 eventTime>=startTime。

untilTime   指定时间以前,ISO8601格式。过滤条件为 eventTime<untilTime。

entityType  指定实体类型。

entityId    指定实体ID

limit       限制提取记录数,默认为20

reversed    默认是false (从小到大,以creationTime时间记录)


例如:(注意在命令行中参数出现 & 符号时这个参数必需加引号)

1
curl -i -X GET  "http://localhost:7070/events.json?accessKey=1rqDilLgSOjVIKI2SAiLSIsHecSHfq4b0KrGRh8RAbIcXL8dpjOwyTIgxEs6QiU1&reversed=true&entityType=user&entityId=1"
本文转自  ttlxihuan    51CTO博客,原文链接:http://blog.51cto.com/php2012web/1643957
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值