sumitasok
7/1/2015 - 12:47 PM

Blog.md

// r.db("arkinventory").table("reportsdata").insert(
{
	"order_id": "session-id-4321",
	"kart_id": "kart-id-4321",
	"entities": [
		{
			"entity_id": "54d68a99-3ae9-4b2c-9de3-5ee55f57d3a5",
			"quantity": 1,
			"price":  1000.00,
			"intial_amount": 1000.00,
			"final_amount": 800.00,
			"offer_applied": {
				"offer_id": "PERC20",
				"discount_amount": 200,
			}
		},
		{
			"entity_id": "cbdd6ccd-ae7a-45c9-9627-a3322b2d4cc1",
			"quantity": 2,
			"price":  1000.00,
			"intial_amount": 2000.00,
			"final_amount": 1960.00,
			"offer_applied": {
				"offer_id": "CASH20",
				"discount_amount": 20,
			}
		}

	],
	"intial_amount": 3000,
	"final_amount": 2760,
	"attendees": [
		{
			"ticketId": "ticket-id-4321",
			"attendees": [
				{
					"firstname": "Sumit",
					"lastname": "Asok",
					"email": "sumitasok@gmail.com",
					"mobile": "9895865899"
				}
			]
		},
		{
			"ticketId": "ticket-id-7723",
			"attendees": [
				{
					"firstname": "Achu",
					"lastname": "Kuttan",
					"email": "achu.kuttan@gmail.com",
					"mobile": "9897668222"
				}
			]
		}
	],
	"payee":	{
		"firstname": 'Sumit',
		"lastname": 'Asok',
		"email": 'sumitasok@gmail.com',
		"mobile": '9895865899',
		"address_1": 'Baker STreet',
		"address_2": 'Inglando',
		"city": 'Stathis',
		"state": 'Kerala',
		"country": 'India',
		"pincode": '680664',
		"pay_mode": 'CC',
		"pay_id": 'mhpayid-oqpweipie'
	},
	"updated_at": '2013-07-13 16:32:48 -0700'
}
// )
conn = r.connect(host="localhost", port=28015, db="arkinventory", auth_key="", timeout=20)

r.table("reportsdata").
	concat_map(lambda attendees: [attendees['attendees']]).
		concat_map(lambda attendees: attendees['attendees']).sample(4).run(conn)
		
r.table("reportsdata").
	concat_map(lambda attendees: attendees['attendees']).
	filter({'ticketId': "ticket-id-4321"}).sample(3).run(conn)
	
r.table("reportsdata").concat_map(lambda attendees: attendees['attendees']).
	concat_map(lambda attendees: attendees['attendees']).
	filter(r.row["email"] == "sumitasok@gmail.com").sample(4).run(conn)
	
# fetch list of attendees who have bouth tickets for this ticketId
r.table("reportsdata").concat_map(lambda attendees: attendees['attendees']).
	filter(r.row["ticketId"] == "ticket-id-4321").
	concat_map(lambda attendees:attendees['attendees']).sample(4).run(conn)
	
r.table("reportsdata").filter(r.row["kart_id"] == "kart-id-4321").sample(4).count().run(conn)

r.table("reportsdata").filter(r.row["updated_at"] > "2012-01-01" and r.row["updated_at"] < "2015-01-01").sample(4).run(conn)

r.table("reportsdata").filter(r.row["updated_at"] > "2013-07-12" and r.row["updated_at"] < "2013-08-14").
	concat_map(lambda attendees: attendees['attendees']).
	filter(r.row["ticketId"] == "ticket-id-4321").
	concat_map(lambda attendees:attendees['attendees']).sample(4).run(conn)
	
r.table("reportsdata").concat_map(lambda report: report["entities"]).
	group("entity_id").
	reduce(lambda left, right: {
		"entity_id":left["entity_id"],"quantity" : left["quantity"]+right["quantity"], "price": left["price"]+right["price"]
	}).run(conn)
	
r.table("reportsdata").concat_map(lambda report: report["entities"]).
	group("entity_id").
	reduce(lambda left, right: {
		"entity_id":left["entity_id"],"quantity" : left["quantity"]+right["quantity"], "price": left["price"]+right["price"], "final_amount": left["final_amount"]+right["final_amount"]
	}).run(conn)
	
# Map
r.table("reportsdata").concat_map(lambda report: report["entities"]).
	group("entity_id").
	map(lambda entity: {
	"entity_id":entity["entity_id"],"quantity" : entity["quantity"], "price": entity["price"], "final_amount": entity["final_amount"], "discount_amount": entity["offer_applied"]["discount_amount"]
	}).run(conn)
	
# Reduce
r.table("reportsdata").concat_map(lambda report: report["entities"]).
	group("entity_id").map(lambda entity: {
		"entity_id":entity["entity_id"],"quantity" : entity["quantity"], "price": entity["price"], "final_amount": entity["final_amount"], "discount_amount": entity["offer_applied"]["discount_amount"]
	}).
	reduce(lambda left, right: {
		"entity_id":left["entity_id"],"quantity" : left["quantity"]+right["quantity"], "price": left["price"], "final_amount": left["final_amount"]+right["final_amount"], "discount_amount": left["discount_amount"]+right["discount_amount"]
	}).run(conn)
	
r.table("reportsdata").filter(r.row["updated_at"] > "2013-07-12" and r.row["updated_at"] < "2013-08-13").
	concat_map(lambda report: report["entities"]).
	group("entity_id").
	map(lambda entity: {
		"entity_id":entity["entity_id"],"quantity" : entity["quantity"], "price": entity["price"], "final_amount": entity["final_amount"], "discount_amount": entity["offer_applied"]["discount_amount"]
	}).
	reduce(lambda left, right: {
		"entity_id":left["entity_id"],"quantity" : left["quantity"]+right["quantity"], "price": left["price"], "final_amount": left["final_amount"]+right["final_amount"], "discount_amount": left["discount_amount"]+right["discount_amount"]
	}).run(conn)

This post deals with using rethinkdb with python repl

Do your installations

Note: in every python example, I have intended it wrongly for easy reading. Please make it into a single line, before pasting in the Repl. Try typing.

Get the Conn

[]

create your table

[]

Understand the Data. Nested Data. We need to fetch multiple kinds of results from this data.

[]

Insert the Data

[]

Concat Map:

Used to collect arrays of data inside various documents, under a same key. Map, can be used for the same, but that would give a nested array kind result. While Concat Map gives plain array.

Sample command sets the limit of results to be displayed.

[gist file="3-concat_map-single.py"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

lambda calls each row in the document, as attendees and returns the value of key "attendee" inside each row into the result array.

The Result would be

[gist file="3-concat_map-single_result.js"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

To further collect the list of attendee details alone nested inside the order/report, do one more Concat Map on the resulting data.

[gist file="3-concat_map.py"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

The Result would be

[gist file="3-concat_map_result.js"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

Filters:

Let us fetch the data with some conditions, so that, only data with a particular ticketId is displayed in result.

[gist file="4-concat_map_filter.py"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

The result would be like this

[gist file="4-concat_map_filter_result.js"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

To further reduce the list to have more conditions attach a filter to the resulting data. Try doing a count on this to see how many records have same email ID.

[gist file="5-concat_twice-filter.py"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

Now try fetching Attendees list who has bought a particular ticket

[gist file="6-fetch_by_ticketId.py"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

Result:

Only Sumit has bought one ticket of ID ticket-id-4321

[gist file="6-fetch_by_ticketId-result.js"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

Now you must be clear about how concat_map works and how to do a simple filter on data.

Do a filter on Date. So any record which was updated between "2012-01-01" and "2015-01-01" will be the result.

[gist file="7-filter_by_date.py"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

Now attach the same filter before all our other pipelines. So that first time, the filter is run, then on the resulting data concat_map is run, which return an array of tickets type and attendees on which run the filter to find out the particular ticketID you are looking for, and concat_map the attendees

[gist file="7-filter_by_date-concat.py"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]

The result would be, the array of users who has done booking ticket "ticket-id-4321" in the specified time interval.

[gist file="7-filter_by_date-concat_result.json"]https://gist.github.com/sumitasok/086792d3814ad229b3cb[/gist]