Upload
david-m-johnson
View
2.767
Download
1
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
Dave Johnson Apigee
Introduction to Apache Usergrid: The BaaS built on Cassandra
© 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
Usergrid Overview
© 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
© 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
© 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, …
© 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
© 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
© 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
Exploring the Usergrid API
8
© 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
© 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}
© 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
© 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
© 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
Creating an app with Usergrid
18
© 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
© 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
© 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
© 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 🚫 ✅ 🚫 🚫
© 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} ✅ 🚫 ✅ 🚫
© 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
Adding Login & Signup
32
© 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
© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 25
jQueryMobile
© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 26
Apache Cordova
© 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
© 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 } });
© 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"}'
© 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
© 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()
Adding Checkins
4
© 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
© 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 } });
© 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()
Add Follower relationships
36
© 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
© 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); } });
© 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()
Package & run your app
40
© 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
© 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()
Running & Deploying Usergrid
© 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
© 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 😃
© 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)
© 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
Wrapping up…
© 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
Thank you