HarperDB recommends utilizing HarperDB Applications for defining databases, tables, and other functionality. However, this guide is a great way to get started using on the HarperDB Operations API.
Create dog Table
We first need to create a table. Since our company is named after our CEO's dog, lets create a table to store all our employees' dogs. We'll call this table, dogs.
Tables in HarperDB are schema-less, so we don't need to add any attributes other than a primary_key (in pre 4.2 versions this was referred to as the hash_attribute) to create this table.
HarperDB does offer a database parameter that can be used to hold logical groupings of tables. The parameter is optional and if not provided the operation will default to using a database named data.
If you receive an error response, make sure your Basic Authentication user and password match those you entered during the installation process.
Now that we have a table to store our dog data, we also want to create a table to track known breeds. Just as with the dog table, the only attribute we need to specify is the primary_key.
We're ready to add some dog data. Penny is our CTO's pup, so she gets ID 1 or we're all fired. We are specifying attributes in this call, but this doesn't prevent us from specifying additional attributes in subsequent calls.
{"message":"inserted 1 of 1 records","inserted_hashes": [1 ],"skipped_hashes": []}
Insert Multiple Dogs
Let's add some more Harper doggies! We can add as many dog objects as we want into the records collection. If you're adding a lot of objects, we would recommend using the .csv upload option (see the next section where we populate the breed table).
{"message":"inserted 12 of 12 records","inserted_hashes": [2,3,4,5,6,7,8,9,10,11,12,13 ],"skipped_hashes": []}
Bulk Insert Breeds Via CSV
We need to populate the 'breed' table with some data so we can reference it later. For larger data sets, we recommend using our CSV upload option.
Each header in a column will be considered as an attribute, and each row in the file will be a row in the table. Simply specify the file path and the table to upload to, and HarperDB will take care of the rest. You can pull the breeds.csv file from here: https://s3.amazonaws.com/complimentarydata/breeds.csv
Here's a more complex SQL command joining the breed table with the dog table. We will also pull only the pups belonging to Kyle, Zach, and Stephen.
Body
{"operation":"sql","sql":"SELECT d.id, d.dog_name, d.owner_name, b.name, b.section FROM data.dog AS d INNER JOIN data.breed AS b ON d.breed_id = b.id WHERE d.owner_name IN ('Kyle', 'Zach', 'Stephen') AND b.section = 'Mutt' ORDER BY d.dog_name"}
Response: 200
[ {"id":4,"dog_name":"Billy","owner_name":"Zach","name":"LABRADOR / GREAT DANE MIX","section":"Mutt" }, {"id":8,"dog_name":"Gemma","owner_name":"Stephen","name":"SHORT HAIRED SETTER MIX","section":"Mutt" }, {"id":2,"dog_name":"Harper","owner_name":"Stephen","name":"HUSKY MIX","section":"Mutt" }, {"id":5,"dog_name":"Rose Merry","owner_name":"Zach","name":"TERRIER MIX","section":"Mutt" }]