PHP code example of jiangyunan / riak

1. Go to this page and download the library: Download jiangyunan/riak library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jiangyunan / riak example snippets


use Basho\Riak;
use Basho\Riak\Node;
use Basho\Riak\Command;

$node = (new node\Builder)
    ->atHost('127.0.0.1')
    ->onPort(8087)
    ->build();
$riak = new Riak([$node], [], new Riak\Api\Pb());
$bucket = new Riak\Bucket("carnoc");

$key = "date.txt";
$location = new Riak\Location($key, $bucket);

$dateString = Date("H:i:s");

$dataObject = new Riak\RObject($dateString);
$dataObject->setContentType("text/plain");

$storeCommand = (new Command\Builder\StoreObject($riak))
    ->withObject($dataObject)
    ->atLocation($location)
    ->build();
$response = $storeCommand->execute();

$fetchCommand = (new Command\Builder\FetchObject($riak))
    ->atLocation($location)
    ->build();
$response = $fetchCommand->execute();

if($response->getCode() == 200){
    $dataObject = $response->getObject();
    $contentType = $dataObject->getContentType();
    $data = [
        "type" => $contentType,
        "data" => $dataObject->getData(),
    ];
} else {
    $data = [];
}

$deleteCommand = (new Command\Builder\DeleteObject($riak))
    ->atLocation($location)
    ->build();
$response = $deleteCommand->execute();

return ($response->getCode() == 204);

// lib classes are e Basho\Riak\Node;
use Basho\Riak\Command;

// define the connection info to our Riak nodes
$nodes = (new Node\Builder)
    ->onPort(10018)
    ->buildCluster(['riak1.company.com', 'riak2.company.com', 'riak3.company.com',]);

// instantiate the Riak client
$riak = new Riak($nodes);

// build a command to be executed against Riak
$command = (new Command\Builder\StoreObject($riak))
    ->buildObject('some_data')
    ->buildBucket('users')
    ->build();
    
// Receive a response object
$response = $command->execute();

// Retrieve the Location of our newly stored object from the Response object
$object_location = $response->getLocation();