Riak 2.0

Introduction

by Randy Secrist / @randysecrist

New Features

  • Better Search
  • Data Types
  • Strong Consistency
  • Security
  • Better Config Management

Search

  • Document Indexing
  • Schemas
  • Simple Index Search
  • Full Text Search
  • Facet Queries
  • Highlighting

Yokozuna - High

  • Write it like Riak
  • Query it like Solr

 

Z References
Commit Log Search

Yokozuna - Lower

  • Distributed Solr
  • Active Anti Entropy
  • HTTP or Protocol Buffers

Yokozuna - Low

Riak JSON


git clone https://github.com/basho-labs/riak_json
							

Riak JSON Client Libraries

Node.js

git clone https://github.com/dmitrizagidulin/nrj
							
Python

git clone https://github.com/basho-labs/riak_json_python_client
							
Ruby

git clone https://github.com/basho-labs/riak_json_ruby_client
							
Riagent (RiakJson for Rails)

git clone https://github.com/dmitrizagidulin/riagent
							
Java

git clone https://github.com/basho-labs/riak_json_java_client
							

Getting Riak JSON

Build from Source


git clone https://github.com/basho/riak.git
git checkout ack-riak-json
make rel
make devrel
							

Vagrant VM w/ Virtual Box

Riak Configuration

etc/riak.conf


search = on
riak_json_http = on
						

Start Riak


ulimit -n 4096
bin/riak start
bin/riak ping
						

Java / Scala

Import

Java

import com.basho.riak.json.*;
							
Scala

import com.basho.riak.json._
import com.basho.riak.json.Field.Type._

import scala.beans.BeanProperty
import com.fasterxml.jackson.annotation.JsonIgnore;
							

Instantiate

Java

Client client = new Client("localhost", 10018);
client.createCollection("test_collection");
							
Scala

val client = new Client("localhost", 10018)
client.createCollection("test_collection")
							

Define Class

Scala

class MySquare (l:Int, w:Int) extends Document {
  def this() =  this(0, 0)
  @BeanProperty var key: String = _
  @BeanProperty var length: Int = l
  @BeanProperty var width: Int = w

  /* don't serialize this tuple */
  @JsonIgnore def getSize = Tuple2(length, width)
}
  							

Define Schema

Scala

val schema = new Schema.Builder()
  .addField(new Field("length", INTEGER).setRequired(true))
  .addField(new Field("width", INTEGER).setRequired(true))
  .addField(new Field("owner", STRING))
  .build()
  							

Associate Schema

Scala

collection.setSchema(schema)
  							

Getting Data In

Scala

val large_square = new MySquare(1024, 768)
val normal_square = new MySquare(640, 480)
collection.insert(large_square)
collection.insert(normal_square)
  							

Getting Data Out

Scala

// By Key
val result = collection.findByKey("9tT49FHJoQImObmPYgVPRcB56T2", classOf[MySquare])
result.getLength() => 1024
  							

Getting Data Out

Scala

// Find One
val q_string = "{\"length\": {\"$gt\": 300}}"
val query = new Query(q_string, classOf[MySquare]);
val single_result = collection.findOne(query)
  							

// All the Things!
val many_results = collection.findAll(query)

// documents are a java Collection
many_results.getDocuments().size() => 2

// info about the result set
many_results.numPages(); => 1 -- total pages in result set
many_results.getPage(); => 0 -- current page (zero-indexed)
many_results.perPage(); => results per page, defaults to 100

// extract into a more malable scala list if needed:
many_results.getDocuments.toArray(
  new Array[MySquare](many_results.getDocuments.size)
)
  							

Python


git clone http://github.com/basho/riak_json_python_client
cd riak_json_python_client
python setup.py install
  						

Import


from riakjson.client import Client, Connection
from riakjson.schema import Schema
							

Instantiate


client = Client(Connection(host='127.0.0.1', port=10018))

# Create or use an existing collection named 'customers'
customers = client.customers
							

Define Schema


customer_schema = Schema()
	.text('name')
	.text('Address')
	.text('City')
	.number('Age')
	.geo('Location').build()
							

Associate Schema


customers.set_schema(customer_schema)
							

Getting Data In


# No key - key generated by RiakJson
customer = {'name': 'Json Argo',
	'Address': '123 Fake Street',
	'City': 'Ancient',
	'State': 'Greece',
	'Age': 23}

customer_key = customers.insert(customer)
# '/TgFKK4uUZKSpyXkMGGQUMhcZ8TQ'
							

# With a defined key
customer2 = {'name': 'Dan Gerr',
	'Address': '1000 Hoover Drive',
	'City': 'Ferrytown',
	'State': 'WV',
	'Age': 33}
customer2_key = 'cust_key_12345'
customers.insert(customer2, customer2_key)
# 'cust_key_12345'
							

Getting Data Out


# GET by key
customer_record = customers.get(customer_key)
## u'{"Age":23,"City":"Ancient","State":"Greece","name":"Json Argo","Address":"123 Fake Street"}'
							

# SEARCH - Exact field match, single record
query = {'name': 'Json Argo'}

customers.find_one(query)
## {u'City': u'Ancient', u'name': u'Json Argo', u'Age': 23, u'State': u'Greece', u'Address': u'123 Fake Street', u'_id': u'TgFKK4uUZKSpyXkMGGQUMhcZ8TQ'}
							

## SEARCH - All users whose name begins with Json, field names are case sensitive
query = {'name': 'Json*'}

result = customers.find(query)
# <riakjson.result.Result object at 0x10e884910>

# iterate results
for item in result.objects():
	print item

## {u'City': u'Ancient', u'name': u'Json Argo', u'Age': 23, u'State': u'Greece', u'Address': u'123 Fake Street', u'_id': u'TgFKK4uUZKSpyXkMGGQUMhcZ8TQ'}
							

Riak JSON Operators

  • AND
  • OR
  • Not Equal ($ne)
  • Greater Than ($gt)
  • Greater Than Equal($gte)
  • Less Than ($lt)
  • Less Than Equal($lte)
  • Between
  • Regex
  • Exists

THE END

basho.com
randy.secrist.io