Install Dependencies
composer require php-riak/riak-client-pb
Ensure protoc and protoc-gen-php are installed globally or via composer install as per the README.
Generate Protocol Buffers
Run the generation script to create PHP classes from riak.proto:
php vendor/bin/protoc-gen-php.php -o src/ -i . riak.proto -Dmultifile
Place generated files in src/Generated/ (or your preferred directory).
First Use Case: Connecting to Riak
Use the generated classes with a Riak PB client (e.g., Riak\Client\PB):
use Riak\Client\PB\Connection;
use Riak\Client\PB\RiakPB;
$connection = new Connection('riak-node-host', 8087);
$client = new RiakPB($connection);
CRUD Operations
Use generated message classes (e.g., PutR, GetR, DelR) to interact with Riak:
// Put a key-value pair
$put = new \PutR();
$put->setBucket('my-bucket');
$put->setKey('my-key');
$put->setValue('my-value');
$client->send($put);
// Fetch a value
$get = new \GetR();
$get->setBucket('my-bucket');
$get->setKey('my-key');
$response = $client->send($get);
Handling Responses
Parse responses using generated classes (e.g., GetRResponse):
if ($response instanceof \GetRResponse) {
$value = $response->getValue();
}
Error Handling
Check for errors in responses (e.g., ErrorResp):
if ($response instanceof \ErrorResp) {
throw new \RuntimeException($response->getReason());
}
RiakPB client into services for testability.Connection objects for performance.Protocol Buffer Generation
protoc and protoc-gen-php are up-to-date. Mismatches may cause runtime errors.riak.proto or dependencies.Deprecated API
Error Handling
error_log($response->serializeToString());
Thread Safety
Connection to log raw PB messages:
$connection->setLogger(function ($message) {
error_log($message);
});
protoc --decode_raw to inspect binary responses:
protoc --decode_raw='.GetRResponse' < response.bin
RiakPB to add logic (e.g., auth, metrics):
class AuthRiakPB extends RiakPB {
public function send($request) {
$request->setAuthToken('your-token');
return parent::send($request);
}
}
MultiPutR):
$batch = new \MultiPutR();
$batch->addPut($put1);
$batch->addPut($put2);
$client->send($batch);
Connection with a custom socket implementation (e.g., for gRPC).How can I help you explore Laravel packages today?