From 3e2fbed5c25204e85ce2a795ccddbea261f19754 Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Sun, 8 Jul 2012 20:52:55 +0100 Subject: [PATCH] [#168,docs/backend][m]: major extension to backend tutorial to give examples of most backends. --- _includes/example-backends-csv-disk.js | 22 ++++ _includes/example-backends-dataproxy.js | 17 +++ _includes/example-backends-elasticsearch.js | 13 +++ _includes/example-backends-gdocs.js | 2 +- _includes/example-backends-online-csv.js | 20 ++++ docs/index.html | 11 +- docs/tutorial-backends.markdown | 112 +++++++++++++++++++- 7 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 _includes/example-backends-csv-disk.js create mode 100644 _includes/example-backends-dataproxy.js create mode 100644 _includes/example-backends-elasticsearch.js create mode 100644 _includes/example-backends-online-csv.js diff --git a/_includes/example-backends-csv-disk.js b/_includes/example-backends-csv-disk.js new file mode 100644 index 00000000..537340b3 --- /dev/null +++ b/_includes/example-backends-csv-disk.js @@ -0,0 +1,22 @@ +// the file input +var $file = $('.my-file-input')[0]; + +// listen for the file to be submitted +$($file).change(function(e) { + // create the dataset in the usual way but specifying file attribute + var dataset = new recline.Model.Dataset({ + file: $file.files[0], + backend: 'csv' + }); + + // now load - note that this is again async (HTML5 File API is async) + // dataset.fetch().done(function() { console.log('here'); }); + dataset.fetch(); + + // For demonstrations purposes display the data in a grid + var grid = new recline.View.Grid({ + model: dataset + }); + $('#my-csv-disk').append(grid.el); +}); + diff --git a/_includes/example-backends-dataproxy.js b/_includes/example-backends-dataproxy.js new file mode 100644 index 00000000..9d4aea3c --- /dev/null +++ b/_includes/example-backends-dataproxy.js @@ -0,0 +1,17 @@ +var dataset = new recline.Model.Dataset({ + url: 'http://data.london.gov.uk/datafiles/transport/tfl_passengers.csv', + // optional rows parameter specifies how many rows to retrieve - default is a 1000 + // rows: 5000 + backend: 'dataproxy' +}); + +// async again as we fetch via AJAX behind the scenes +// once data is fetched it will be stored in a local MemoryStore so further querying will not involve the DataProxy +dataset.fetch(); + +// For demonstrations purposes display the data in a grid +var grid = new recline.View.Grid({ + model: dataset +}); +$('#my-dataproxy').append(grid.el); + diff --git a/_includes/example-backends-elasticsearch.js b/_includes/example-backends-elasticsearch.js new file mode 100644 index 00000000..51d41c0c --- /dev/null +++ b/_includes/example-backends-elasticsearch.js @@ -0,0 +1,13 @@ +var dataset = new recline.Model.Dataset({ + url: 'http://datahub.io/dataset/rendition-on-record/ac5a28ea-eb52-4b0a-a399-5dcc1becf9d9/api', + backend: 'elasticsearch' +}); + +dataset.fetch(); + +// For demonstrations purposes display the data in a grid +var grid = new recline.View.SlickGrid({ + model: dataset +}); +$('#my-elasticsearch').append(grid.el); + diff --git a/_includes/example-backends-gdocs.js b/_includes/example-backends-gdocs.js index 555d6a2c..5ef56069 100644 --- a/_includes/example-backends-gdocs.js +++ b/_includes/example-backends-gdocs.js @@ -15,7 +15,7 @@ $('#my-gdocs').append(grid.el); // Now do the query to the backend to load data dataset.fetch().done(function(dataset) { if (console) { - console.log(dataset.currentDocuments); + console.log(dataset.records); } }); diff --git a/_includes/example-backends-online-csv.js b/_includes/example-backends-online-csv.js new file mode 100644 index 00000000..60a57d93 --- /dev/null +++ b/_includes/example-backends-online-csv.js @@ -0,0 +1,20 @@ +// Create the dataset in the usual way +// Note the additional options you can specify for parsing the CSV file +var dataset = new recline.Model.Dataset({ + url: '{{page.root}}/demos/data/sample.csv', + backend: 'csv', + // separator: ',', + // delimiter: '"', + // encoding: 'utf8' +}); + +// remember this is async so if you want to do something you need to call it in done method e.g. +// dataset.fetch.done(function(dataset) { console.log(dataset.recordCount)}); +dataset.fetch(); + +// show the data for illustrations sake +var grid = new recline.View.Grid({ + model: dataset +}); +$('#my-online-csv').append(grid.el); + diff --git a/docs/index.html b/docs/index.html index 2671d183..37f90f55 100644 --- a/docs/index.html +++ b/docs/index.html @@ -55,11 +55,12 @@ root: ../

Backends

diff --git a/docs/tutorial-backends.markdown b/docs/tutorial-backends.markdown index 8dab2ffb..48d5fb51 100644 --- a/docs/tutorial-backends.markdown +++ b/docs/tutorial-backends.markdown @@ -30,6 +30,13 @@ source. They provide methods for loading and saving Datasets and individuals Documents as well as for bulk loading via a query API and doing bulk transforms on the backend. +Backends come in 2 flavours: + +1. Loader backends - only implement fetch method. The data is then cached in a Memory.Store on the Dataset and interacted with there. This is best for sources which just allow you to load data or where you want to load the data once and work with it locally. +2. Store backends - these support fetch, query and, if write-enabled, save. These are suitable where the backend contains a lot of data (infeasible to load locally - for examples a million rows) or where the backend has capabilities you want to take advantage of. + +### Instantiation and Use + You can use a backend directly e.g. {% highlight javascript %} @@ -55,10 +62,11 @@ recline.Backend.ElasticSearch can be identified as 'ElasticSearch' or ### Included Backends -* [elasticsearch: ElasticSearch Backend](docs/backend/elasticsearch.html) -* [dataproxy: DataProxy Backend (CSV and XLS on the Web)](docs/backend/dataproxy.html) -* [gdocs: Google Docs (Spreadsheet) Backend](docs/backend/gdocs.html) -* [csv: Local CSV file backend](docs/backend/csv.html) +* [gdocs: Google Docs (Spreadsheet)](src/backend.gdocs.html) +* [csv: CSV files](src/backend.csv.html) +* [elasticsearch: ElasticSearch](src/backend.elasticsearch.html) - this also covers the DataHub as it has an ElasticSearch compatible API +* [dataproxy: DataProxy (CSV and XLS on the Web)](src/backend.dataproxy.html) +* [couchdb: CouchDB](src/backend.couchdb.html) Backend not on this list that you would like to see? It's very easy to write a new backend -- see below for more details. @@ -108,6 +116,102 @@ a bespoke chooser and a Kartograph (svg-only) map. +## Loading Data from ElasticSearch and the DataHub + +Recline supports ElasticSearch as a full read/write/query backend. It also means that Recline can load data from the [DataHub's](http://datahub.io/) data API as that is ElasticSearch compatible. Here's an example, using [this dataset about Rendition flights](http://datahub.io/dataset/rendition-on-record/ac5a28ea-eb52-4b0a-a399-5dcc1becf9d9') on the DataHub: + +{% highlight javascript %} +{% include example-backends-elasticsearch.js %} +{% endhighlight %} + +### Result + +
 
+ + + + +## Loading data from CSV files + +For loading data from CSV files there are 3 cases: + +1. CSV is online but on same domain -- we can then load using AJAX (as no problems with same origin policy) +2. CSV is on local disk -- if your browser supports HTML5 File API we can load the CSV file off disk +3. CSV is online but not on same domain -- use DataProxy (see below) + +### Local online CSV file + +Let's start with first case: loading a "local" online CSV file. We'll be using this [example file]({{page.root}}/demos/data/sample.csv). + +{% highlight javascript %} +{% include example-backends-online-csv.js %} +{% endhighlight %} + +#### Result + +
 
+ + + +### CSV file on disk + +This requires your browser to support the HTML5 file API. Suppose we have a file input like: + + + +Then we can load the file into a Recline Dataset as follows: + +{% highlight javascript %} +{% include example-backends-csv-disk.js %} +{% endhighlight %} + +#### Try it out! + +Try it out by clicking on the file input above, selecting a CSV file and seeing what happens. + +
 
+ + + + +## Loading data from CSV and Excel files online using DataProxy + +The [DataProxy](http://github.com/okfn/dataproxy) is a web-service run by the Open Knowledge Foundation that converts CSV and Excel files to JSON. It has a convenient JSON-p-able API which means we can use it to load data from online CSV and Excel into Recline Datasets. + +Recline ships with a simple DataProxy "backend" that takes care of fetching data from the DataProxy source. + +The main limitation of the DataProxy is that it can only handle Excel files up to a certain size (5mb) and that as we must use JSONP to access it error information can be limited. + +{% highlight javascript %} +{% include example-backends-dataproxy.js %} +{% endhighlight %} + +### Result + +
 
+ + + +### Customizing the timeout + +As we must use JSONP in this backend we have the problem that if DataProxy errors (e.g. 500) this won't be picked up. To deal with this and prevent the case where the request never finishes We have a timeout on the request after which the Backend sends back an error stating that request timed out. + +You can customize the length of this timeout by setting the following constant: + +{% highlight javascript %} +// Customize the timeout (in milliseconds) - default is 5000 +recline.Backend.DataProxy.timeout = 10000; +{% endhighlight %} + + ## Writing your own backend Writing your own backend is easy to do. Details of the required API are in the