PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/repo2/files/resources/apache-ignite-fabric-1.5.0.final-bin/examples/rest/http-rest-example.php

https://gitlab.com/piotrekwv/hadoopignite
PHP | 144 lines | 50 code | 35 blank | 59 comment | 3 complexity | 2c5b6e700162cc31643b1adfca4bba22 MD5 | raw file
  1. #!/bin/php
  2. <?php
  3. /*
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership.
  7. * The ASF licenses this file to You under the Apache License, Version 2.0
  8. * (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * To execute this script you will have to enable optional `ignite-rest-http` module -
  21. * copy `libs/optional/ignite-rest-http` folder into `libs` (one level up).
  22. *
  23. * After that start up an instance of Ignite with cache enabled.
  24. * You can use configuration from examples/config folder as follows:
  25. * ----
  26. * ${IGNITE_HOME}/bin/ignite.sh examples/config/example-cache.xml
  27. * ----
  28. *
  29. * Make sure you have correctly specified $CACHE_NAME script global variable
  30. * accordingly to your configuration. In mentioned above example-cache.xml we
  31. * have only default cache configured.
  32. */
  33. // Make sure CURL is present.
  34. if (!function_exists('curl_init'))
  35. die('CURL not supported. (introduced in PHP 4.0.2)');
  36. // Make sure json_decode is present (PHP 5.2.0+).
  37. if (!function_exists('json_decode'))
  38. die('JSON not supported. (introduced in PHP 5.2.0)');
  39. $URL = 'http://localhost:8080/ignite?';
  40. // Cache name to use (null or empty string for default cache).
  41. $CACHE_NAME = null;
  42. /**
  43. * Creates URL parameters.
  44. *
  45. * @param $map URL parameters.
  46. * @return void
  47. */
  48. function makeUrlParams($map) {
  49. $urlParams = "";
  50. foreach($map as $key => $value) $urlParams .= $key . '=' . urlencode($value) . '&';
  51. return rtrim($urlParams, '& ');
  52. }
  53. /**
  54. * Sets REST command over HTTP.
  55. *
  56. * @param $query Query parameters.
  57. * @param $post Post data.
  58. * @return void
  59. */
  60. function execute($query, $post) {
  61. global $URL;
  62. $cmd = $query['cmd'];
  63. $query = makeUrlParams($query);
  64. $post = makeUrlParams($post);
  65. $api = $URL . $query;
  66. echo "Executing command '$cmd' with url '$api' ...\n\n";
  67. // Initiate curl object.
  68. $request = curl_init($api);
  69. curl_setopt($request, CURLOPT_HEADER, 0); // Set to 0 to eliminate header info from response.
  70. curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1).
  71. curl_setopt($request, CURLOPT_POSTFIELDS, $post); // Use HTTP POST to send form data.
  72. // Uncomment if you get no gateway response and are using HTTPS.
  73. // curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
  74. // Execute curl post and store results in $response.
  75. $response = (string)curl_exec($request);
  76. // Close curl object.
  77. curl_close($request);
  78. if (!$response)
  79. die('Nothing was returned. Do you have a connection to Ignite Jetty server?');
  80. echo "Response received from Ignite: $response\n\n";
  81. // JSON decoder.
  82. $result = json_decode($response, true);
  83. // The entire result printed out.
  84. echo "JSON result parsed into array:\n";
  85. print_r($result);
  86. echo str_repeat("-", 20) . "\n\n";
  87. }
  88. $rand = rand(1, 1000);
  89. $key = "mykey-" . $rand; // Cache key.
  90. $val = "myval-" . $rand; // Cache value.
  91. $clientId = "2128506-ad94-4a21-a711-" . $rand; // Client id.
  92. // Get command (will not return data, as cache is empty).
  93. execute(array('cmd' => 'get', 'key' => $key, 'clientId' => $clientId, 'cacheName' => $CACHE_NAME), array());
  94. // Put command.
  95. execute(array('cmd' => 'put', 'key' => $key, 'clientId' => $clientId, 'cacheName' => $CACHE_NAME), array('val' => $val));
  96. // Get command (will return value 'myval' stored in cache by 'put' command above).
  97. execute(array('cmd' => 'get', 'key' => $key, 'clientId' => $clientId, 'cacheName' => $CACHE_NAME), array());
  98. $rand = rand(1, 1000);
  99. $key1 = "mykey-" . $rand; // Cache key.
  100. $val1 = "myval-" . $rand; // Cache value.
  101. $rand = rand(1, 1000);
  102. $key2 = "mykey-" . $rand; // Cache key.
  103. $val2 = "myval-" . $rand; // Cache value.
  104. // Put all command.
  105. execute(array('cmd' => 'putall', 'k1' => $key1, 'k2' => $key2, 'clientId' => $clientId, 'cacheName' => $CACHE_NAME),
  106. array('v1' => $val1, 'v2' => $val2));
  107. // Get all command.
  108. execute(array('cmd' => 'getall', 'k1' => $key1, 'k2' => $key2, 'clientId' => $clientId, 'cacheName' => $CACHE_NAME), array());
  109. ?>