Skip to content

Commit a486cf0

Browse files
committed
updated: tutorial prerequisites and steps for geospatial hotel search implementation
- Updated prerequisites to include specific Couchbase and AWS account requirements - Revised tutorial steps for creating AppSync API and configuring Data API - Updated screenshots and interactive map descriptions for better user guidance
1 parent f369a6a commit a486cf0

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed
90.3 KB
Loading
-23.7 KB
Loading

tutorial/markdown/nodejs/dataApi-appsync-tutorial/tutorial.md

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,26 @@ If you want to see the final code you can refer to it here: [Final Demo Code](ht
3232
This guide walks you through building a geospatial hotel search demo that finds hotels within a specified distance from airports. The application uses AWS AppSync (GraphQL) with environment variables for credential management, Couchbase Data API for executing SQL++ queries, and a Streamlit frontend for interactive map visualization — end to end, with inlined code.
3333

3434
### Prerequisites
35-
- Couchbase Capella account with the Travel Sample dataset loaded and credentials that can access it (and network access allowed).
36-
- Learn/setup here: [Couchbase Data API Prerequisites](https://docs.couchbase.com/cloud/data-api-guide/data-api-start.html#prerequisites)
37-
- Couchbase Data API docs (enable Data API, copy endpoint)
38-
- [Data API Docs](https://docs.couchbase.com/cloud/data-api-guide/data-api-start.html)
35+
- **[Couchbase Capella account](https://cloud.couchbase.com/sign-up)** with a [running cluster](https://docs.couchbase.com/cloud/get-started/create-account.html)
36+
- **[Travel-sample bucket](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html)** imported into your cluster
37+
- **[Data API enabled](https://docs.couchbase.com/cloud/data-api-guide/data-api-start.html)** in Capella (via the cluster's Connect page)
38+
- **Important:** Set Allowed IP address to **"Allow access from anywhere"** for demo purposes
39+
- This configuration is required for this demo to function with AWS AppSync
40+
- Not recommended for production—see [documentation](https://docs.couchbase.com/cloud/data-api-guide/data-api-start.html) for secure network configurations
41+
- **[AWS account](https://aws.amazon.com/)** with permissions to create AppSync APIs
3942

4043
---
4144

4245
### Enable Couchbase Data API
4346

4447
**What is Data API?**
4548
Couchbase Data API provides a RESTful HTTP interface to your cluster. Instead of embedding the Couchbase SDK in your app, you make standard HTTP requests to query, insert, or update documents. This is perfect for serverless architectures (like AppSync) because:
46-
- No heavy SDK initialization on cold starts
47-
- Works from any language with HTTP support
49+
- **Enables driverless mode:** No need to deploy SDKs in FaaS environments (AWS Lambda, Azure Functions)
50+
- **Is lightweight:** Avoids heavy SDK initialization overhead in stateless functions
51+
- **Simplifies integration:** Uses standard HTTP—no SDK version management or dependencies
52+
- **Is language agnostic:** Works with any platform that can make HTTP requests
53+
54+
Learn more: [Data API vs. SDKs](https://docs.couchbase.com/cloud/data-api-guide/data-api-sdks.html)
4855

4956
**Steps:**
5057
1. In Capella, enable Data API for your project/cluster (single click in the cluster settings).
@@ -68,7 +75,9 @@ Couchbase Data API provides a RESTful HTTP interface to your cluster. Instead of
6875
AppSync provides a managed GraphQL layer with built-in auth, and logging. It lets your frontend speak GraphQL while your backend (Data API) speaks SQL++. The resolver bridges the two.
6976

7077
**Steps:**
71-
1. Create an AppSync GraphQL API with Public API (we'll use API key auth for demo simplicity).
78+
1. Create an AppSync GraphQL API:
79+
- Choose **"Build from scratch"** option when prompted
80+
- Select **Public API** with **API key** authentication for demo simplicity
7281

7382
#### Define the schema (paste into the schema editor)
7483

@@ -162,7 +171,7 @@ AppSync can call external HTTP APIs. You configure a base URL (your Data API end
162171
**Steps:**
163172
- In AppSync, create a new HTTP data source.
164173
- Set the endpoint to your Couchbase Data API base URL (from step 1).
165-
- **Do not** configure auth here; we'll add Basic auth dynamically in the resolver using credentials from environment variables.
174+
- **Important:** Do NOT enable "Authorization configuration", since credentials will be passed dynamically via the resolver using AppSync environment variables.
166175

167176
#### Screenshot
168177
![AppSync Data Source](appsync-data-source.jpg)
@@ -186,7 +195,7 @@ These environment variables will be accessible in your resolvers via `ctx.env.cb
186195
#### Screenshot
187196
![AppSync Environment Variables](appsync-env-vars.jpg)
188197

189-
#### Add a JavaScript Unit Resolver for `Query.listHotelsInCity`
198+
#### Add a JavaScript Unit Resolver for `Query.listHotelsNearAirport`
190199

191200
**What does this resolver do?**
192201
AppSync resolvers have two functions:
@@ -195,13 +204,13 @@ AppSync resolvers have two functions:
195204

196205
Our resolver:
197206
- Reads `cb_username` and `cb_password` from AppSync environment variables (`ctx.env`).
198-
- Extracts `city` from the GraphQL arguments.
199-
- Constructs a parameterized SQL++ query: `SELECT c.* FROM hotel AS c WHERE city = $1`.
207+
- Extracts `airportName` and `withinKm` from the GraphQL arguments.
208+
- Constructs a geospatial SQL++ query to find hotels near the specified airport.
200209
- Builds a Data API Query Service request:
201210
- **Endpoint**: `/_p/query/query/service` (Data API's SQL++ query endpoint).
202211
- **Headers**: `Authorization: Basic <base64(username:password)>`, `Content-Type: application/json`.
203-
- **Body**: `{ query_context: "default:travel-sample.inventory", statement: "SELECT ...", args: [city], timeout: "30m" }`.
204-
- Returns the `results` array from Data API's JSON response, which AppSync then maps to the `[Hotel]` type.
212+
- **Body**: `{ query_context: "default:travel-sample.inventory", statement: "...", args: [airportName, withinKm], timeout: "30m" }`.
213+
- Returns both hotels and airport information in the `Output` schema format.
205214

206215
**Why `query_context`?**
207216
Setting `query_context` to `default:travel-sample.inventory` lets you write `FROM hotel` instead of the fully qualified `FROM travel-sample.inventory.hotel` in your SQL++. It's a namespace shortcut.
@@ -398,10 +407,12 @@ query ListHotelsNearAirport {
398407
```
399408

400409
3. Click **Run**. You should see a JSON response with:
401-
- An array of hotels within 50km of Heathrow airport
410+
- An array of hotels within 50km of Les Loges airport
402411
- Airport information including name and location coordinates
403412

404-
Try different airport names like "Charles de Gaulle", or "Changi" to see results from different locations.
413+
Try different airport names like **"San Francisco Intl"**, **"Charles de Gaulle"**, **"Luton"**, or **"London St Pancras"** to see results from different locations.
414+
415+
**Troubleshooting:** If you encounter a "connection timed out" error, verify that your Data API has IP address access set to **"Allow access from anywhere"** in the Capella Connect page (see Prerequisites section). This is required for AWS AppSync to reach your cluster.
405416

406417
If it works, you've successfully bridged AppSync → Data API → Couchbase with geospatial querying!
407418

@@ -415,17 +426,38 @@ Now that you've set up your AWS AppSync backend with the Couchbase Data API inte
415426

416427
Visit the live Streamlit app at **[https://couchbase-data-api-appsync-demo.streamlit.app/](https://couchbase-data-api-appsync-demo.streamlit.app/)**
417428

418-
The app provides an interactive map-based interface where you can search for hotels near airports. Simply enter your AppSync GraphQL endpoint and API key in the sidebar (note that Couchbase credentials are securely stored as environment variables in AppSync, so you don't need to enter them). Navigate to "Search Hotels", enter an airport name like **"Les Loges"** and a distance like **100 km**, then click "Search".
429+
The app provides an interactive map-based interface where you can search for hotels near airports. Simply enter your **AppSync GraphQL Endpoint** and **AppSync API Key** in the sidebar (note that Couchbase credentials are securely stored as environment variables in AppSync, so you don't need to enter them). Click the **"Search Hotels"** tab, enter an airport name like **"San Francisco Intl"**, **"Les Loges"**, **"Luton"**, or **"London St Pancras"** and a distance like **100 km**, then click "Search". A loading spinner will appear while fetching results.
419430

420431
Results display on an interactive map with the airport shown as an orange marker and hotels as color-coded dots (green for excellent ratings, red for poor). The map automatically centers on the airport location. Hover over any marker to see details like hotel names, addresses, prices, and phone numbers, or expand the "Raw response" section to see the full JSON data from AppSync.
421432

422433
![Streamlit search interface](streamlit-search.jpg)
423434

424435
![Streamlit map with hotels and airport](streamlit-map.jpg)
425436

426-
**Want to explore the source code or run it locally?** The complete Streamlit frontend code is available at [https://github.com/couchbase-examples/couchbase-data_api-appsync-demo/tree/main/src/frontend](https://github.com/couchbase-examples/couchbase-data_api-appsync-demo/tree/main/src/frontend). The frontend consists of `home.py` (navigation and connection settings) and `search_hotels.py` (hotel search with map visualization using `pydeck`). It uses the `requests` library to call AppSync GraphQL with standard HTTP POST requests. Ratings are computed from review data and mapped to colors for visual feedback.
437+
**Want to explore the source code or run it locally?** The complete Streamlit frontend code is available at [https://github.com/couchbase-examples/couchbase-data_api-appsync-demo/tree/main/src/frontend](https://github.com/couchbase-examples/couchbase-data_api-appsync-demo/tree/main/src/frontend). The frontend consists of `home.py` (tab-based navigation and connection settings) and `search_hotels.py` (hotel search with map visualization using `pydeck`). It uses the `requests` library to call AppSync GraphQL with standard HTTP POST requests. Ratings are computed from review data and mapped to colors for visual feedback.
438+
439+
To run locally:
440+
```bash
441+
# Clone the repository
442+
git clone https://github.com/couchbase-examples/couchbase-data_api-appsync-demo.git
443+
444+
# Navigate to the project directory
445+
cd couchbase-data_api-appsync-demo
446+
447+
# Create a virtual environment
448+
python3 -m venv .venv
449+
450+
# Activate the virtual environment
451+
source ./.venv/bin/activate
452+
453+
# Install required dependencies
454+
pip install -r requirements.txt
455+
456+
# Run the Streamlit app
457+
streamlit run src/frontend/home.py
458+
```
427459

428-
To run locally: clone the repo, install dependencies with `pip install -r requirements.txt`, navigate to `src/frontend`, and run `streamlit run home.py`. The app will start at `http://localhost:8501`.
460+
The app will start at `http://localhost:8501`.
429461

430462
---
431463

0 commit comments

Comments
 (0)