PageRenderTime 34ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/manual/source/datacollection/eventapi.html.md

https://gitlab.com/ggsaavedra/PredictionIO
Markdown | 412 lines | 333 code | 79 blank | 0 comment | 0 complexity | 349f06a238686f1f32d1a4b36b4a234a MD5 | raw file
  1. ---
  2. title: Collecting Data through REST/SDKs
  3. ---
  4. **Event Server** is designed to collect data into PredictionIO in an event-based
  5. style. Once the Event Server is launched, your application can send data to it
  6. through its **Event API** with HTTP requests or with `EventClient`s of
  7. PredictionIO's SDKs.
  8. INFO: All PredictionIO-compliant engines support accessing the Event Store (i.e. the
  9. data store of Event Server) through [PredictionIO's Storage
  10. API](http://docs.prediction.io/api/current/index.html#io.prediction.data.storage.package).
  11. ## Launching the Event Server
  12. INFO: Before launching the Event Server, make sure that your event data store
  13. backend is properly configured and is running. By default, PredictionIO uses
  14. Apache HBase, and a quick configuration can be found
  15. [here](/install/install-linux.html#hbase). Please allow a minute
  16. (usually less than 30 seconds) after HBase is started for its initialization to
  17. complete before starting the Event Server.
  18. Everything about PredictionIO can be done through the `pio` command. Please add
  19. PIO binary command path to to your `PATH` first. Assuming PredictionIO is
  20. installed at `/home/yourname/PredictionIO/`, you can run
  21. ```
  22. $ PATH=$PATH:/home/yourname/PredictionIO/bin; export PATH
  23. ```
  24. To start the event server, run
  25. ```
  26. $ pio eventserver
  27. ```
  28. INFO: By default, the Event Server is bound to 0.0.0.0, which serves global
  29. traffic. To tighten security, you may use `pio eventserver --ip 127.0.0.1` to
  30. serve only local traffic.
  31. ### Check Server Status
  32. ```
  33. $ curl -i -X GET http://localhost:7070
  34. ```
  35. Sample response:
  36. ```
  37. HTTP/1.1 200 OK
  38. Server: spray-can/1.2.1
  39. Date: Wed, 10 Sep 2014 22:37:30 GMT
  40. Content-Type: application/json; charset=UTF-8
  41. Content-Length: 18
  42. {"status":"alive"}
  43. ```
  44. ### Generating App ID and Access Key
  45. First, you need to create a new app in the Event Server. You will later send data into it.
  46. ```
  47. $ pio app new MyTestApp
  48. ```
  49. > You can replace `MyTestApp` with name of your App.
  50. Take note of the *Access Key* and *App ID* generated. You need the *Access Key*
  51. to use the Event API. You should see something like the following output:
  52. ```
  53. [INFO] [App$] Created new app:
  54. [INFO] [App$] Name: MyTestApp
  55. [INFO] [App$] ID: 6
  56. [INFO] [App$] Access Key: WPgcXKd42FPQpZHVbVeMyqF4CQJUnXQmIMTHhX3ZUrSzvy1KXJjdFUrslifa9rnB
  57. ```
  58. ### Creating Your First Event
  59. You may connect to the Event Server with HTTP request or by using one of many
  60. **PredictionIO SDKs**.
  61. For example, the following shows how one can create an event involving a single entity.
  62. Replace the value of `accessKey` by the *Access Key* generated for your App.
  63. <div class="tabs">
  64. <div data-tab="Raw HTTP" data-lang="bash">
  65. ```bash
  66. $ curl -i -X POST http://localhost:7070/events.json?accessKey=WPgcXKd42FPQpZHVbVeMyqF4CQJUnXQmIMTHhX3ZUrSzvy1KXJjdFUrslifa9rnB \
  67. -H "Content-Type: application/json" \
  68. -d '{
  69. "event" : "my_event",
  70. "entityType" : "user"
  71. "entityId" : "uid",
  72. "properties" : {
  73. "prop1" : 1,
  74. "prop2" : "value2",
  75. "prop3" : [1, 2, 3],
  76. "prop4" : true,
  77. "prop5" : ["a", "b", "c"],
  78. "prop6" : 4.56
  79. }
  80. "eventTime" : "2004-12-13T21:39:45.618-07:00"
  81. }'
  82. ```
  83. </div>
  84. <div data-tab="PHP SDK" data-lang="php">
  85. ```php
  86. <?php
  87. require_once("vendor/autoload.php");
  88. use predictionio\EventClient;
  89. $accessKey = 'YOUR_ACCESS_KEY';
  90. $client = new EventClient($accessKey);
  91. $response = $client->createEvent(array(
  92. 'event' => 'my_event',
  93. 'entityType' => 'user',
  94. 'entityId' => 'uid',
  95. 'properties' => array('prop1' => 1,
  96. 'prop2' => 'value2',
  97. 'prop3' => array(1,2,3),
  98. 'prop4' => true,
  99. 'prop5' => array('a','b','c'),
  100. 'prop6' => 4.56
  101. ),
  102. 'eventTime' => '2004-12-13T21:39:45.618-07:00'
  103. ));
  104. ?>
  105. ```
  106. </div>
  107. <div data-tab="Python SDK" data-lang="python">
  108. ```python
  109. from predictionio import EventClient
  110. from datetime import datetime
  111. import pytz
  112. client = EventClient('YOUR_ACCESS_KEY', "http://localhost:7070")
  113. first_event_properties = {
  114. "prop1" : 1,
  115. "prop2" : "value2",
  116. "prop3" : [1, 2, 3],
  117. "prop4" : True,
  118. "prop5" : ["a", "b", "c"],
  119. "prop6" : 4.56 ,
  120. }
  121. first_event_time = datetime(
  122. 2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain'))
  123. first_event_response = client.create_event(
  124. event="my_event",
  125. entity_type="user",
  126. entity_id="uid",
  127. properties=first_event_properties,
  128. event_time=first_event_time,
  129. )
  130. ```
  131. </div>
  132. <div data-tab="Ruby SDK" data-lang="ruby">
  133. ```ruby
  134. require 'predictionio'
  135. event_client = PredictionIO::EventClient.new('YOUR_ACCESS_KEY')
  136. event_client.create_event('my_event', 'user', 'uid',
  137. 'eventTime' => '2004-12-13T21:39:45.618-07:00',
  138. 'properties' => { 'prop1' => 1,
  139. 'prop2' => 'value2',
  140. 'prop3' => [1, 2, 3],
  141. 'prop4' => true,
  142. 'prop5' => %w(a b c),
  143. 'prop6' => 4.56 })
  144. ```
  145. </div>
  146. <div data-tab="Java SDK" data-lang="java">
  147. ```java
  148. (coming soon)
  149. ```
  150. </div>
  151. </div>
  152. For example, the following shows how one can create an event involving two entities (with
  153. `targetEntity`).
  154. <div class="tabs">
  155. <div data-tab="Raw HTTP" data-lang="bash">
  156. ```bash
  157. $ curl -i -X POST http://localhost:7070/events.json?accessKey=WPgcXKd42FPQpZHVbVeMyqF4CQJUnXQmIMTHhX3ZUrSzvy1KXJjdFUrslifa9rnB \
  158. -H "Content-Type: application/json" \
  159. -d '{
  160. "event" : "my_event",
  161. "entityType" : "user",
  162. "entityId" : "uid",
  163. "targetEntityType" : "item",
  164. "targetEntityId" : "iid",
  165. "properties" : {
  166. "someProperty" : "value1",
  167. "anotherProperty" : "value2"
  168. },
  169. "eventTime" : "2004-12-13T21:39:45.618Z"
  170. }'
  171. ```
  172. </div>
  173. <div data-tab="PHP SDK" data-lang="php">
  174. ```php
  175. <?php
  176. require_once("vendor/autoload.php");
  177. use predictionio\EventClient;
  178. $accessKey = 'YOUR_ACCESS_KEY';
  179. $client = new EventClient($accessKey);
  180. $response = $client->createEvent(array(
  181. 'event' => 'my_event',
  182. 'entityType' => 'user',
  183. 'entityId' => 'uid',
  184. 'targetEntityType' => 'item',
  185. 'targetEntityId' => 'iid',
  186. 'properties' => array('someProperty'=>'value1',
  187. 'anotherProperty'=>'value2'),
  188. 'eventTime' => '2004-12-13T21:39:45.618Z'
  189. ));
  190. ?>
  191. ```
  192. </div>
  193. <div data-tab="Python SDK" data-lang="python">
  194. ```python
  195. # Second Event
  196. second_event_properties = {
  197. "someProperty" : "value1",
  198. "anotherProperty" : "value2",
  199. }
  200. second_event_response = client.create_event(
  201. event="my_event",
  202. entity_type="user",
  203. entity_id="uid",
  204. target_entity_type="item",
  205. target_entity_id="iid",
  206. properties=second_event_properties,
  207. event_time=datetime(2014, 12, 13, 21, 38, 45, 618000, pytz.utc))
  208. ```
  209. </div>
  210. <div data-tab="Ruby SDK" data-lang="ruby">
  211. ```ruby
  212. require 'predictionio'
  213. event_client = PredictionIO::EventClient.new('YOUR_ACCESS_KEY')
  214. event_client.create_event('my_event', 'user', 'uid',
  215. 'targetEntityType' => 'item',
  216. 'targetEntityId' => 'iid',
  217. 'eventTime' => '2004-12-13T21:39:45.618Z',
  218. 'properties' => { 'someProperty' => 'value1',
  219. 'anotherProperty' => 'value2' })
  220. ```
  221. </div>
  222. <div data-tab="Java SDK" data-lang="java">
  223. ```java
  224. (coming soon)
  225. ```
  226. </div>
  227. </div>
  228. Sample response:
  229. ```
  230. HTTP/1.1 201 Created
  231. Server: spray-can/1.2.1
  232. Date: Wed, 10 Sep 2014 22:51:33 GMT
  233. Content-Type: application/json; charset=UTF-8
  234. Content-Length: 41
  235. {"eventId":"AAAABAAAAQDP3-jSlTMGVu0waj8"}
  236. ```
  237. ## Using Event API
  238. ### Event Creation API
  239. URL: `http://localhost:7070/events.json?accessKey=yourAccessKeyString`
  240. Query parameters:
  241. Field | Type | Description
  242. :---- | :----| :-----
  243. `accessKey` | String | The Access Key for your App
  244. The event creation support many commonly used data. POST request body:
  245. Field | Type | Description
  246. :---- | :----| :-----
  247. `event` | String | Name of the event.
  248. | | (Examples: "sign-up", "rate", "view", "buy").
  249. | | **Note**: All event names start with "$" and "pio_" are reserved
  250. | | and shouldn't be used as your custom event name (eg. "$set").
  251. `entityType` | String | The entity type. It is the namespace of the entityId and
  252. | | analogous to the table name of a relational database. The
  253. | | entityId must be unique within same entityType.
  254. | | **Note**: All entityType names start with "$" and "pio_" are
  255. | | reserved and shouldn't be used.
  256. `entityId` | String | The entity ID. `entityType-entityId` becomes the unique
  257. | | identifier of the entity. For example, you may have entityType
  258. | | named `user`, and different entity IDs, say `1` and `2`. In this
  259. | | case, `user-1` and `user-2` uniquely identifies | these two
  260. | | entities.
  261. `targetEntityType` | String | (Optional) The target entity type.
  262. | | **Note**: All entityType names start with "$" and "pio_"
  263. | | are reserved and shouldn't be used.
  264. `targetEntityId` | String | (Optional) The target entity ID.
  265. `properties` | JSON | (Optional) See **Note About Properties** below
  266. | | **Note**: All peroperty names start with "$" and "pio_"
  267. | | are reserved and shouldn't be used as keys inside `properties`.
  268. `eventTime` | String | (Optional) The time of the event. Although Event Server's
  269. | | current system time and UTC timezone will be used if this is
  270. | | unspecified, it is highly recommended that this time should be
  271. | | generated by the client application in order to accurately
  272. | | record the time of the event.
  273. | | Must be in ISO 8601 format (e.g.
  274. | | `2004-12-13T21:39:45.618Z`, or `2014-09-09T16:17:42.937-08:00`).
  275. ## Note About Properties
  276. Note that `properties` can be:
  277. 1. Associated with an *generic event*: The `properties` field provide additional information about this event
  278. 2. Associated with an *entity*: The `properties` field is used to record the changes of an entity's properties with special events `$set`, `$unset` and `$delete`.
  279. Please see the [Events Modeling](/datacollection/eventmodel/) for detailed explanation.
  280. ## Debugging Recipes
  281. WARNING: The following API are mainly for development or debugging purpose
  282. only. They should not be supported by SDK nor used by real application under
  283. normal circumstances and they are subject to changes.
  284. INFO: Instead of using `curl`, you can also install JSON browser plugins such as **JSONView** to pretty-print the JSON on your browser. With the browser plugin you can make the `GET` queries below by passing in the URL. Plugins like **Postman - REST Client** provide a more advanced interface for making queries.
  285. The `accessKey` query parameter is mandatory.
  286. Replace `<your_accessKey>` and `<your_eventId>` by a real one in the following:
  287. ### Get an Event
  288. ```
  289. $ curl -i -X GET http://localhost:7070/events/<your_eventId>.json?accessKey=<your_accessKey>
  290. ```
  291. ### Delete an Event
  292. ```
  293. $ curl -i -X DELETE http://localhost:7070/events/<your_eventId>.json?accessKey=<your_accessKey>
  294. ```
  295. ### Get Events of an App
  296. ```
  297. $ curl -i -X GET http://localhost:7070/events.json?accessKey=<your_accessKey>
  298. ```
  299. INFO: By default, it returns at most 20 events. Use the `limit` parameter to specify how many events returned (see below). Use cautiously!
  300. In addition, the following *optional* parameters are supported:
  301. - `startTime`: time in ISO8601 format. Return events with `eventTime >= startTime`.
  302. - `untilTime`: time in ISO8601 format. Return events with `eventTime < untilTime`.
  303. - `entityType`: String. The entityType. Return events for this `entityType` only.
  304. - `entityId`: String. The entityId. Return events for this `entityId` only.
  305. - `event`: String. The event name. Return events with this name only.
  306. - `targetEntityType`: String. The targetEntityType. Return events for this `targetEntityType` only.
  307. - `targetEntityId`: String. The targetEntityId. Return events for this `targetEntityId` only.
  308. - `limit`: Integer. The number of record events returned. Default is 20. -1 to
  309. get all.
  310. - `reversed`: Boolean. **Must be used with both `entityType` and `entityId` specified**, returns events in reversed chronological order. Default is false.
  311. WARNING: If you are using <code>curl</code> with the <code>&</code> symbol, you should quote the entire URL by using single or double quotes.
  312. WARNING: Depending on the size of data, you may encounter timeout when querying with some of the above filters. Event server uses `entityType` and `entityId` as the key so any query without both `entityType` and `entityId` specified might result in a timeout.
  313. For example, get all events of an app with `eventTime >= startTime`
  314. ```
  315. $ curl -i -X GET "http://localhost:7070/events.json?accessKey=<your_accessKey>&startTime=<time in ISO8601 format>"
  316. ```
  317. For example, get all events of an app with `eventTime < untilTime`:
  318. ```
  319. $ curl -i -X GET "http://localhost:7070/events.json?accessKey=<your_accessKey>&untilTime=<time in ISO8601 format>"
  320. ```
  321. For example, get all events of an app with `eventTime >= startTime` and `eventTime < untilTime`:
  322. ```
  323. $ curl -i -X GET "http://localhost:7070/events.json?accessKey=<your_accessKey>&startTime=<time in ISO8601 format>&untilTime=<time in ISO8601 format>"
  324. ```
  325. For example, get all events of a specific entity with `eventTime < untilTime`:
  326. ```
  327. $ curl -i -X GET "http://localhost:7070/events.json?accessKey=<your_accessKey>&entityType=<your_entityType>&entityId=<your_entityId>&untilTime=<time in ISO801 format>"
  328. ```
  329. ### Delete All Events of an App
  330. Please use the following CLI command:
  331. ```
  332. $ pio app data-delete <your_app_name>
  333. ```