In [9]:
#some imports
import json

from couchbase.bucket import Bucket
from couchbase.n1ql import N1QLQuery
import couchbase

In [2]:
#we connect to the beer-sample bucket change the ip address to your needs
cb = Bucket('couchbase://172.17.0.2/beer-sample')

In [3]:
#let's make a query

query = N1QLQuery("SELECT * " +\
                  "FROM `beer-sample` " +\
                  "WHERE brewery_id=$brewery_id",
                  brewery_id="21st_amendment_brewery_cafe")

for row in cb.n1ql_query(query):
    print(json.dumps(row, indent=2))


{
  "beer-sample": {
    "abv": 7.2,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "North American Ale",
    "description": "Deep golden color. Citrus and piney hop aromas. Assertive malt backbone supporting the overwhelming bitterness. Dry hopped in the fermenter with four types of hops giving an explosive hop aroma. Many refer to this IPA as Nectar of the Gods. Judge for yourself. Now Available in Cans!",
    "ibu": 0,
    "name": "21A IPA",
    "srm": 0,
    "style": "American-Style India Pale Ale",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 5,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "North American Ale",
    "description": "Deep black color, toasted black burnt coffee flavors and aroma. Dispensed with Nitrogen through a slow-flow faucet giving it the characteristic cascading effect, resulting in a rich dense creamy head.",
    "ibu": 0,
    "name": "563 Stout",
    "srm": 0,
    "style": "American-Style Stout",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 5.2,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "North American Ale",
    "description": "Rich golden hue color. Floral hop with sweet malt aroma. Medium mouth feel with malt sweetness, hop quenching flavor and well-balanced bitterness.",
    "ibu": 0,
    "name": "Amendment Pale Ale",
    "srm": 0,
    "style": "American-Style Pale Ale",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 3.6,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "British Ale",
    "description": "An American session beer. Loaded with hop character and a malty presence, but lower in alcohol.",
    "ibu": 0,
    "name": "Bitter American",
    "srm": 0,
    "style": "Special Bitter or Best Bitter",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 9.8,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "North American Ale",
    "description": "Deep, golden, rich malt flavor huge citrus, fruity grassy, ethanol sweetness aroma with a profound bitterness, yet balanced malt back bone with grapefruit, mellow citric overtones. Dry hopped three times in the fermenter. Brewed with over 65 lbs of hops for 300 gallons of beer. The beer to bring world peace and end the war. Bronze Medal - 2006 Imperial IPA Festival at the Bistro in Hayward, California.",
    "ibu": 0,
    "name": "Double Trouble IPA",
    "srm": 0,
    "style": "Imperial or Double India Pale Ale",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 5.5,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "Irish Ale",
    "description": "Deep toffee color with rich roasty and subtle hop aroma. Chocolate flavors dominate the palate and interact with back-end sweetness.",
    "ibu": 0,
    "name": "General Pippo's Porter",
    "srm": 0,
    "style": "Porter",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 5.8,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "North American Ale",
    "description": "Deep amber color. Subtle hop floral nose intertwined with sweet crystal malt aromas. Rich malt flavors supporting a slight bitterness finish.",
    "ibu": 0,
    "name": "North Star Red",
    "srm": 0,
    "style": "American-Style Amber/Red Ale",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 5.9,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "North American Ale",
    "description": "Deep black color. Chocolate milk color head, providing an array of Belgian lace. Toffee and light roasty aromas and flavors. A malty sweet taste is evident but, this rich oatmeal based stout finishes dry. Made with 20 lbs. of oysters, in the boil, from our good friends at Hog Island Oyster Company.",
    "ibu": 0,
    "name": "Oyster Point Oyster Stout",
    "srm": 0,
    "style": "American-Style Stout",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 5.2,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "British Ale",
    "description": "Traditional English E.S.B. made with English malt and hops. Fruity aroma with an imparted tart flavor brought about by replicating the water profile in England at Burton on Trent.",
    "ibu": 0,
    "name": "Potrero ESB",
    "srm": 0,
    "style": "Special Bitter or Best Bitter",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 5,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "North American Ale",
    "description": "Light golden color. Sweet dry aroma with crisp, clear bitterness. Brewed with imported German hops.The perfect beer to have when you'd like to have more than one.",
    "ibu": 0,
    "name": "South Park Blonde",
    "srm": 0,
    "style": "Golden or Blonde Ale",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}
{
  "beer-sample": {
    "abv": 5.5,
    "brewery_id": "21st_amendment_brewery_cafe",
    "category": "Belgian and French Ale",
    "description": "The definition of summer in a pint glass. This unique, American-style wheat beer, is brewed with 400 lbs. of fresh pressed watermelon in each batch. Light turbid, straw color, with the taste and essence of fresh watermelon. Finishes dry and clean. Now Available in Cans!",
    "ibu": 0,
    "name": "Watermelon Wheat",
    "srm": 0,
    "style": "Belgian-Style Fruit Lambic",
    "type": "beer",
    "upc": 0,
    "updated": "2010-07-22 20:00:20"
  }
}

In [4]:
#let's have a look to a specific object

rv = cb.get('21st_amendment_brewery_cafe')
print(json.dumps(rv.value, indent=2))


{
  "name": "21st Amendment Brewery Cafe",
  "city": "San Francisco",
  "state": "California",
  "code": "94107",
  "country": "United States",
  "phone": "1-415-369-0900",
  "website": "http://www.21st-amendment.com/",
  "type": "brewery",
  "updated": "2010-10-24 13:54:07",
  "description": "The 21st Amendment Brewery offers a variety of award winning house made brews and American grilled cuisine in a comfortable loft like setting. Join us before and after Giants baseball games in our outdoor beer garden. A great location for functions and parties in our semi-private Brewers Loft. See you soon at the 21A!",
  "address": [
    "563 Second Street"
  ],
  "geo": {
    "accuracy": "ROOFTOP",
    "lat": 37.7825,
    "lon": -122.393
  }
}

In [5]:
#let's play with the metadata and N1QL

query = N1QLQuery("SELECT meta(beer).id,meta(beer).cas, meta(beer) " +\
                  "FROM `beer-sample` beer " +\
                  "WHERE name=$name",
                  name="563 Stout")

for row in cb.n1ql_query(query):
    print(json.dumps(row, indent=2))


{
  "$1": {
    "cas": 1501004091080572928,
    "flags": 33554438,
    "id": "21st_amendment_brewery_cafe-563_stout",
    "type": "json"
  },
  "cas": 1501004091080572928,
  "id": "21st_amendment_brewery_cafe-563_stout"
}

In [10]:
#let's store an object

toto={'name':'toto','description':'this is the best'}
try:
    result = cb.insert('toto', toto,format=couchbase.FMT_JSON)
    if not result.success:
        print("problem with insert")
except couchbase.exceptions.KeyExistsError:
    print("toto already exist, we have to delete it first")
    result=cb.remove('toto')
    print("toto removed")
    if not result.success:
        print("problem with deleting toto")
    result = cb.insert('toto', toto,format=couchbase.FMT_JSON)
    if not result.success:
        print("problem with insert")

#check the result
print(json.dumps(cb.get('toto').value, indent=2))


toto already exist, we have to delete it first
toto removed
{
  "name": "toto",
  "description": "this is the best"
}

In [11]:
query = N1QLQuery("SELECT * " +\
                  "FROM `beer-sample`  " +\
                  "WHERE name=$name",
                  name="toto")

for row in cb.n1ql_query(query):
    print(json.dumps(row, indent=2))


{
  "beer-sample": {
    "description": "this is the best",
    "name": "toto"
  }
}

In [12]:
query = N1QLQuery("SELECT name " +\
                  "FROM `beer-sample`  " +\
                  "WHERE abv>$abv",
                  abv=30)

for row in cb.n1ql_query(query):
    print(json.dumps(row, indent=2))


{
  "name": "Tactical Nuclear Penguin"
}
{
  "name": "Vetter 33"
}
{
  "name": "Norfolk Nog Old Dark Ale"
}

In [ ]: