Search:

Search all manuals
Search this manual
Manual
Couchbase Developer's Guide 1.8
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
4 Storing Data
Chapter Sections
Chapters

4.3. Loading Data

During development or production you will probably want to add application-specific seed data into Couchbase. This may be data you use to test your application during development, or it may be application-specific content that is pre-populated, such as catalog data.

In general, you need three elements in place to do a bulk upload:

The following PHP example demonstrates a bulk set of sample data on beers and breweries. Sample code and data for this example are at Github: import.php and beer and brewey sample data.

First, here is an example of a JSON record for a beer. This particular beer is in the beer_#17_Cream_Ale.json file in the beer-sample/beer directory.

{
	"_id":"beer_#17_Cream_Ale",
	"brewery":"Big Ridge Brewing",
	"name":"#17 Cream Ale",
	"category":"North American Lager",
	"style":"American-Style Lager",
	"updated":"2010-07-22 20:00:20"
}

We also have brewery data with each brewery in a JSON file located in beer-sample/breweries. Finally we create the script that reads in the directories and stores each file as a record in Couchbase Server:

<?php

// Set up Couchbase client object
try {
  $cb = new Couchbase(COUCHBASE_HOST.':'.COUCHBASE_PORT, COUCHBASE_USER, COUCHBASE_PASSWORD, COUCHBASE_BUCKET);
} catch (ErrorException $e) {
  die($e->getMessage());
}

// import a directory
function import($cb, $dir) {
  $d = dir($dir);
  while (false !== ($file = $d->read())) {
    if (substr($file, -5) != '.json') continue;
    echo "adding $file\n";
    $json = json_decode(file_get_contents($dir . $file), true);
    unset($json["_id"]);
    echo $cb->set(substr($file, 0, -5), json_encode($json));
    echo "\n";
  }
}

// import beers and breweries
import($cb, 'beer-sample/beer/');
import($cb, 'beer-sample/breweries/');

?>

We first create a Couchbase client, then we declare an import function which will read in our files and write them to Couchbase Server. While the import function reads each file into memory, we repeat the same set of operations for each file. If the file is not a JSON file we convert it into JSON. We also omit the first attribute of '_id' from the file since we already provide unique file names and use the filename itself as a key; therefore we do not need this as a unique identifier. Then we store the value to Couchbase Server as JSON and use the filename, minus the .json file extension as the key for each record.