SAP Cloud Platform & Cloud Foundry – test, feedback, tips and tricks

After you followed all the steps to setting up Cloud Foundry in your SAP Cloud Platform Cockpit, it’s time to dig into and like all new environment you are testing, you’ll face some problems.

In this article, I’ll quickly overview the activation/installation and talk about a simple NodeJS with a MongoDB scenario. I hope it will help you to be more familiar with the tools, to implement your own needs on the platform.

Quick CF activation/installation

Install CF on your machine and follow instructions detailed here. One it’s installed, you need to test the CF CLI by typing cf in your favorite terminal.

A quota was not automatically assigned for my account because I had several sub accounts. Then, when I deployed an application, I get an error message:

Activate MongoDB / PostgreSQL and other services

Follow the path: Cloud Foundry > Services > Service Marketplace and activate the services you want to use. You can directly map the service with your app if you have already deployed it.

Create and deploy a NodeJS

First, you need to create a skeleton NodeJS project using the Nodeclipse available for free on the Eclipse Market or create your own project from scratch using the framework your like. You can simply download this project from my Github account : js-app-for-sap-cloud-foundry.

If you are not using mongodb in the scenario, you can comment the line require(‘./server/db/mongo-connect.js’)(oAppEnv);. It simplifies the workflow, you can uncomment it later. When your project is ready, be sure it compiles by running the two following commands:

npm install
node app.js

If all is OK, your project is ready to be hosted on CF.

// Logon to Could Foundry
cf login
// Enter your credentials and navigate to the space you want to host the app
// Change directory and push it!
cd js-app-for-sap-cloud-foundry
cf push

The file manifest.yml is very important for your app deployment, to specify the app information: commands, name, buildpack, etc. Below, an example of manifest.yml file

---
applications:
- name: js-app
  memory: 512M
  host: js-app
  command: node app.js

Fields you need to know for the manifest.yml file:

Key Value
name The name of the application
buildpack The name of the Node.js buildpack determined before with command cf buildpacks. (by default an auto determination of the buildpack is done if the buildpack information is missing)
command For NodeJS apps, a start command is needed to start the application. Here for example, it’s “node app.js”.
memory RAM available for the application.
disk_quota Disk space available for the application.
host Host information used in the URL which makes the application accessible.

Connect with a MongoDB

The first step is to create a new MongoDB instance. You have to go to your SAP CP CF sub account > Services > Service Marketplace and select MongoDB. Now in your left panel you can select Instances and create a new one (see below)

Now you are able to map your instance with the app you deployed before. The manifest.yml will change a little bit and the service will be mapped like this:

---
applications:
- name: js-app
  memory: 512M
  host: js-app
  command: node app.js
  services:
  - mongodb-service 

/!\ “mongodb-service” is the name of you instance, you need to adapt /!\

Useful links

Github | SAP/hcp-cloud-foundry-tutorials
SAP CP | Cloud Foundry Tutorials
Github | Cloud Foundry CLI
Cloud Foundry CLI Reference Guide

Useful CLI commands

cf
cf apps
cf login
cf logout
cf push
cf buildpacks
cf marketplace
cf api api.cf.<host information>
cf create-service mongodb v3.0-dev mongo-service

L’équivalent du SELECT (SQL) sur une base de données MongoDB (NoSQL)

La particularité des bases de données “non structurées” ou “orientée documents” comme MongoDB, c’est quelles sont très performantes pour de gros volumes de données (en particulier depuis l’arrivée de tous ces objets connectés).

Pour faire un petit tour d’horizon des bases de données NoSQL actuellement sur le marché (> 225 fin décembre 2015), rendez-vous sur le site http://nosql-database.org/ qui dispose d’une liste à peu près complète des produits du marché.

Je vais montrer l’équivalent (ou plutôt les deux équivalents) de mongoDB par rapport au SELECT qui est une des requêtes de base du SQL.

SQL

Structure d’un SGBD SQL

SGBD > Database > Table >Entrées (lignes)

Capture d’écran 2015-12-20 à 13.08.24

Exemple de Select

Pour rappel, la requête basique pour sélectionner le nom, prénom et l’age des utilisateurs ayant comme ville Paris en SQL :

[sql]
SELECT nom, prenom, age
FROM users
WHERE ville="Paris"
[/sql]

MONGODB

Structure d’un SGBD NoSQL

SGBD > Database > Collections > Documents

Capture d’écran 2015-12-20 à 13.09.46

db.collection.find(query, projection)

Le premier paramètre de la fonction find() est un objet au format JSON qui va permettre de filtrer les résultats (un peu comme le where en SQL). Le second paramètre permet de retourner seulement certains champs, il est donc similaire aux valeurs qui suivent le mot clé SELECT en SQL. On peut également ajouter des conditions comme supérieur à, inférieur ou égal à, etc…

  • $gt = supérieur à
  • $lt = inférieur à
  • $gte = supérieur ou égal à
  • $lte = inférieur ou égal à

Quelques exemples :

[javascript]
// All
var res = db.users.find( );

// Paris as city
var res = db.users.find( { ville: "Paris" } );

// Older than…
var res = db.users.find( { ville: "Paris", age: { $gte:20 } } );

// Don’t return all dataset, only the name
var res = db.users.find( { ville: "Paris" }, { name: true } );

// Same as findOne()
var res = db.users.find( { nom: "Bronner", prenom: "Joey" }).limit(1);
[/javascript]

db.collection.findOne(query, projection)

La requête findOne() est différente puisqu’une fois exécutée, elle va retourner les données et fermer le curseur. Au contraire de la requête find() (même en utilisant la syntaxe find().limit(1)) qui retourne un curseur. Il faut également savoir que sans argument, findOne() retourne un document quelconque de la collection. Ce type de requête sans argument peut donc être utilisé pour constater la structure des documents.

[javascript]
// All
var res = db.users.findOne( { nom: "Bronner", prenom: "Joey" });
[/javascript]

Il n’y a pas grand chose à ajouter, c’est simple comme bonjour. Ah si j’oubliais, si vous êtes en train de faire ou refaire votre CV et que vous n’avez pas de compétences avancées en SQL, mettez que vous êtes expert en NoSQL 🙂

nosql-expert