I recently started using Riak with PHP.
How exactly do I store JSON data?! Do I just push the JSON string into a Riak Object?!
The Riak PHP Client has the following for storing data:
require_once('riak-php-client/riak.php');
# Connect to Riak
$client = new RiakClient('127.0.0.1', 8098);
# Choose a bucket name
$bucket = $client->bucket('test');
# Supply a key under which to store your data
$person = $bucket->newObject('riak_developer_1', array(
'name' => "John Smith",
'age' => 28,
'company' => "Facebook"
));
# Save the object to Riak
$person->store();
This takes the form of key-array data. So in the case of JSON, would it be like below?
# Supply a key under which to store your data
$person = $bucket->newObject('riak_developer_1', '{"name": "John Smith", "age": "28", "company": "Facebook"}';
I’m trying to find the best way to store data in the event that I need to use Riak’s Search feature. I’m also unsure if map-reduce works better/faster this way?!
Thanks in advance.
The array is already being converted to JSON in
RiakObject->store()by default; you don’t have to do anything.If you look at the PHP client source, you’ll see
json_encode()is being used in thestore()method whenjsonizeisTRUE(Which is the case when you useRiakBucket->newObject()):https://github.com/basho/riak-php-client/blob/master/riak.php#L1513
If you were to to do the following in your browser (after storing your array):
You’d get back:
When you fetch the data back out of Riak using the PHP client it’s doing the reverse and you end up with your original PHP array.