50
Dave Johnson Apigee Introduction to Apache Usergrid: The BaaS built on Cassandra

Introduction to Usergrid - ApacheCon EU 2014

Embed Size (px)

DESCRIPTION

Whether you are building a mobile app or a web app, Apache Usergrid (incubating) can provide you with a complete backend that supports authentication, persistence and social features like activities and followers all via a comprehensive REST API — and backed by Cassandra, giving you linear scalability. All that, and Usergrid is open source too. This session will explain how you can use Usergrid to provide a back-end for your application. We’ll start with an overview of Usergrid features, then explore in depth how to authenticate users, store data and query data with the REST API provided by a Usergrid server. We’ll develop a simple HTML5 app and package it as a native mobile app via Apache Cordova. We'll also cover how to run Usergrid locally for development and testing.

Citation preview

Page 1: Introduction to Usergrid - ApacheCon EU 2014

Dave Johnson Apigee

Introduction to Apache Usergrid: The BaaS built on Cassandra

Page 2: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 2

Agenda

» Overview: why Usergrid » Exploring the Usergrid API » Creating an app with Usergrid » Running & Deploying Usergrid » Q & A

Page 3: Introduction to Usergrid - ApacheCon EU 2014

Usergrid Overview

Page 4: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 4

Why Usergrid?

» Don’t build a server

PHPRuby Java

NodeMySQL PaaS

Services in the “cloud”

App Server Cloud

Page 5: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 5

Why Usergrid?

» Focus on building your app

Services in the “cloud”

&

App Cloud

Page 6: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 6

What is Usergrid?

» Complete Back-end as a Service (BaaS) » REST API, backed by Cassandra

» Three parts: » Web application, provides REST API » Comprehensive Management Portal » Client SDKs

» iOS, Android, JavaScript, Node, Ruby, Java, Perl, .Net, Command-line, …

Page 7: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 7

What is Usergrid?

» Core services you need to build apps: » JSON object indexing & query » Geo-location & geo-queries » Connections & Collections » Users, Groups, Social Graph » Authentication, Roles & Permissions » Asset & File Management

» Soon: Push Notifications

Page 8: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 8

Main limitation

» You cannot add server-side logic » If you need server-side logic:

Your server-side logic

Services in the “cloud”

&

App Cloud

Page 9: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 9

An Apache Incubator Project

Page 10: Introduction to Usergrid - ApacheCon EU 2014

Exploring the Usergrid API

8

Page 11: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 11

Usergrid REST API

» JSON over HTTP » POST, GET, PUT and DELETE JSON Entities

» Entities have UUID, Name, Type » Entities have name/value properties

» All Entity properties are indexed » SQL-like syntax for Entity queries

Page 12: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 12

Usergrid REST API

»Usergrid is multi-tenant »Multiple organizations »Each organization has multiple apps

»Each application has root URL like this »http:// host/${org}/${app}

» Entities exist in Collections »http:// host/${org}/${app}/${collection}

Page 13: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 13

Usergrid REST API

» You can define Collections of custom Entities with any properties you want

» Plus special “built-in” Entity Collections: » http:// host/${org}/${app}/users » http:// host/${org}/${app}/groups » http:// host/${org}/${app}/roles » http:// host/${org}/${app}/activities » http:// host/${org}/${app}/assets

Page 14: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 14

Connections

» No joins, but Entities can have Connections to other Entities

» For example, to get all Entities that Dave is connected to via a connection of type “pets”

» http:// host/${org}/${app}/users/dave/pets

Page 15: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 15

Collecting Cats

» Demo the Portal

» Create User Dave with Default Role » Create Cats Collection » Setup “Dave has Cats” Connections

Page 16: Introduction to Usergrid - ApacheCon EU 2014

Creating an app with Usergrid

18

Page 17: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 17

Let’s create a checkin app

» A simple checkin app to demonstrate Usergrid features:

» Login & signup » Roles & Permissions » Following, Followers and Checkins » Usergrid JavaScript SDK

» Built with jQueryMobile & Cordova

Page 18: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 18

Define the data model

» Usergrid provides the basic REST API

» You must decide: » Entity Types & Properties » Entity Connections » Users Roles & Permissions

Page 19: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 19

Entities & Connections

» Entity Collections » Users » Checkins

» Entity Connections » Users Follow Users » Users Have Checkins

Page 20: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 20

User Role: Guest

» User who has not signed up yet » Permissions User with Guest Role:

» Guest user POSTs to register or login » Usergrid takes care of account activation

GET POST PUT DELETE/users/* 🚫 ✅ 🚫 🚫

/token 🚫 ✅ 🚫 🚫

Page 21: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 21

User Role: Default

» Permissions for User with Default Role:

GET POST PUT DELETE/users/${user}/** ✅ ✅ ✅ ✅

/activities/* ✅ 🚫 🚫 🚫

/users/* ✅ 🚫 🚫 🚫

/users/${user} ✅ 🚫 ✅ 🚫

Page 22: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 22

App setup in the Portal

» Quick demo

» Take a look at Roles & Permissions » Ensure they are setup right

Page 23: Introduction to Usergrid - ApacheCon EU 2014

Adding Login & Signup

32

Page 24: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 24

Getting started

» Pre-requisites: » Usergrid JavaScript SDK » jQuery Mobile » Apache Cordova » Your favorite dev environment

Page 25: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 25

jQueryMobile

Page 26: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 26

Apache Cordova

Page 27: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 27

Get setup in an IDE

Page 28: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 28

Login with password

» With Javascript SDK

var client = new Usergrid.Client({ appName: 'checkin1', orgName: 'test-organization', URI: 'http://10.1.1.161:8080' }); client.login(username, password, function(err, res, entity) { if (err) { // inform user of error } else { // change to View Checkins page } });

Page 29: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 29

Login in with password

» Without Javascript SDK » POST a JSON object to /token » For example with curl:

curl -X POST -i -H "Content-Type: application/json" "http://host/test-org/test-app/token" -d '{"grant_type":"password","username":"dave","password":"XXX"}'

Page 30: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 30

Get setup in an IDE

» Demo time

» Create Cordova project in IDE » Add Login & Registration Page

Page 31: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 31

What we’ll add add in checkinv1

» index.html » $(document).on(“mobileinit”, …» login-page» signup-page» checkin-list-page (a placeholder)

» index.js » login()» logout()» signup()

Page 32: Introduction to Usergrid - ApacheCon EU 2014

Adding Checkins

4

Page 33: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 33

Checkins as Activities

» Usergrid supports notion of Activity » And supports a “Following” Connection

» Users post Activities to their own Collection » POST /users/${user}/activities

» Users can see Activities those they follow » GET /activities

Page 34: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 34

JavaScript SDK - create checkin

var data = { type: “checkin", content: content, verb: “post", actor: { username: user.get(“username") } }; client.createUserActivity(user.get("username"), data, function( err, response, activity ) { if (err) { // alert user of error } else { // send user back to View Checkins page } });

Page 35: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 35

What we’ll add in checkinv2

» index.html » checkin-list—page» add-page» view-checkin—page

» index.js » buildCheckinList()» checkin()» showCheckinPage()

Page 36: Introduction to Usergrid - ApacheCon EU 2014

Add Follower relationships

36

Page 37: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 37

Usergrid Social Graph

» Usergrid provides Social Graph features including Following and Groups

» Fred can follow Barney: » POST /users/fred/following/barney

» Fred can see his followers: » GET /users/fred/followers

Page 38: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 38

JavaScript SDK - follow user

var target = // user to be followed var options = { method: 'POST', endpoint: ‘/users/' + me + '/following/users/' + target };

client.request(options, function (err, data) { if (err) { alert("Unable to follow user " + target); } else { alert("Followed user " + target); } });

Page 39: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 39

What we’ll add in checkinv3

» index.html » user-list-page» view-user—page

» index.js » buildUserList()» showUserPage()» followUser()

Page 40: Introduction to Usergrid - ApacheCon EU 2014

Package & run your app

40

Page 41: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 41

Apache Cordova

» Demo time

» Use Cordova to launch the app in iOS

Page 42: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 42

What we’ll add in checkinv4

» index.js » deviceReady() » doWhenBothFrameworksLoaded()

Page 43: Introduction to Usergrid - ApacheCon EU 2014

Running & Deploying Usergrid

Page 44: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 44

Developing with Usergrid

» Three ways to run Usergrid locally: » All-in-one Usergrid Launcher » Install Tomcat, Cassandra and Usergrid » Start Usergrid instance via Vagrant

» Or find a BaaS provider that runs Usergrid: » e.g. Apigee’s API BaaS is free to use

Page 45: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 45

Usergrid deployment options

» Do it yourself with Tomcat, Cassandra and Puppet, Chef, etc.

» Do it yourself on AWS via Usergrid AWSCluster » AWS Cloud Formation Template » Plus supporting Bash and Groovy scripts

» Let somebody else run it for you… » Hint hint 😃

Page 46: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 46

Deployment Architecture UG1

Mobile

Cassandra Cluster

Load Balancer

Web Tomcats (shared nothing)

Page 47: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 47

Deployment Architecture UG2

Mobile

Cassandra Cluster

Load Balancer

ElasticSearch Cluster

Web Tomcats (shared nothing)

Distributed Queue

Page 48: Introduction to Usergrid - ApacheCon EU 2014

Wrapping up…

Page 49: Introduction to Usergrid - ApacheCon EU 2014

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 49

Questions?

» Apache Usergrid » http://usergrid.incubator.apache.org » http://twitter.com/usergrid

» Apigee API BaaS » http://goo.gl/zcjRxC

» Dave Johnson » https://github.com/snoopdave/

» usergrid-mobile (the example code for the presentation) » usergrid-vagrant (scripts to start Usergrid in a VM)

» http://twitter.com/snoopdave

Page 50: Introduction to Usergrid - ApacheCon EU 2014

Thank you