This tutorial can only be completed with a live instructor.
This tutorial uses two databases obtained from two open sources:
FooDB is the world’s largest and most comprehensive resource on food constituents, chemistry and biology. It provides information on both macro-nutrients and micro-nutrients, including many of the constituents that give foods their flavor, color, taste, texture and aroma.
Open Food Facts gathers information and data on food products from around the world.
Step 1Connect to the database
โ
This tutorial can only be completed with a live instructor.
You will be provided with connection details. You should see a connection:
Database explorer
You are currently connected with the user FOODIE.
For a better experience, make sure you close all of the tabs if there are any open:
Database explorer
Step 2Explore the existing data
+
Find an entry for the food you like. To understand what data looks like, open the schema FOOD.
Click on tables. Right-click on COMM_FOODS and choose Generate SELECT statement.
Database explorer
You will see a SQL console and a SELECT statement. Press RunRunto see the results.
Database explorer
Think of a food you like.
Replace the word in the like clause to get a commercial food to analyze.
You are using a fuzzy search on text. The results cover anything with a similarity score of 0.8 to bourbon biscuits in this example.
Results could also contain typos, differences between upper and lower case and even similar words.
Step 3Create a view
+
The nutrition grade is an indicator of how much energy for growth is provided by food. You have selected a food that does not have a nutrition grade assigned.
You will train a model using the records in the database that have a nutrition grade.
The algorithm you will use is called Multi-Class Logistic Regression. You will take the normalized values of nutrients as explanatory attributes to model the relation with the nutrition grade (dependent variable).
Open a new SQL console
Open SQL
Use the following code to create a view for foods that need a nutrition grade assigned:
Aside from the values for training, the procedure included in the Predictive Analytics Library in SAP HANA requires a table with parameter as an input.
You can find more details about the parameters and their meaning in the official SAP Help.
Use the following statement to create the input table and populate it with parameters
The previous call populated the table PAL_FOOD_RESULT_SCORE_TBL with nutrition scores based on the results of the predictive algorithm. Get the ID of your selected food…
Get ID
…and use it in the where clause to replace the sample one:
You now know the most probable nutrition grade for the food you have chosen.
Step 7Get the most popular ingredients
+
The ingredients for the commercial foods are contained in a text field, separated by a comma.
You now want to find out which ingredients are the most common in foods with the lowest nutrition grades.
You will use the map reduce capabilities to split the ingredients into a separate table and get the most frequent ones.
Paste and execute the following code in a new SQL console. This will take some moments to execute but you can continue to read an explanation of the code in the meantime.
SQL
dropfunction"mapper";createfunction"mapper"(iniv_idint,iniv_textnvarchar(5000))returnstable(idint,valnvarchar(5000),freqint)languagesqlscriptreadssqldataasbeginusingsqlscript_stringaslib;declarelt_split_resulttable(resultnvarchar(5000));lt_split_result=lib:split_to_table(:iv_text,',',5000);returnselectiv_idasid,resultasval,count(result)asfreqfrom:lt_split_resultgroupbyresult;end;dropfunction"reducer";createfunction"reducer"(iniv_valnvarchar(5000),init_valtabtable(idint,freqint))returnstable(valnvarchar(5000),ingdt_freqint,total_freqint)languagesqlscriptreadssqldataasbeginreturnselect:iv_valasval,count(distinct(id))asingdt_freq,sum(freq)astotal_freqfrom:it_valtab;end;dobegindeclarelt_inputtable(idint,ingredients_textnvarchar(5000));declarelt_resulttable(valnvarchar(5000),ingdt_freqint,total_freqint);declarelv_idint;declarelv_code_namenvarchar(100);declarelv_new_friendnvarchar(100)='0';declarelv_main_ingredientnvarchar(100);declarelt_heretable(code_namenvarchar(100));declarelv_countint=0;----!!!!SET THE ID FOR YOUR FOOD AND YOUR CODE NAME HERE!!!!------
lv_id=<<UsetheIDofthefoodyouselected>>;lv_code_name='<<Use your First name, your day of birth and the first letter of the color of your socks>>';-------------------------------
lt_input=selectid,to_nvarchar(INGREDIENTS_TEXT)asingredients_textfrom"FOOD"."COMM_FOODS"whereid=lv_id;lt_result=map_reduce(:lt_input,"mapper"(:lt_input.id,:lt_input.ingredients_text)groupbyvalasmap_result,"reducer"(map_result.val,map_result));select*from:lt_resultorderbytotal_freqdesc;selectcount(code_name)intolv_countfrom"FOOD"."I_WAS_HERE"whereingredientin(selectvalfrom:lt_result);iflv_count>0thenselecttop1code_nameintolv_new_friendfrom"FOOD"."I_WAS_HERE"whereingredientin(selectvalfrom:lt_result)groupbycode_name;endif;iflv_new_friend='0'thenlv_new_friend=lv_code_name;endif;insertinto"FOOD"."I_WAS_HERE"(code_name,fav_food,ingredient)select:lv_code_name,:lv_id,valfrom:lt_result;insertinto"FOOD"."FRIENDS"(code_name,friends_with)values(lv_code_name,lv_new_friend);end;
What is going on?
MapReduce is a programming model to split large dataset into parts and process them in parallel, generally in different nodes in a cluster. The parts in the dataset are then reunited into a consistent output.
You have created two functions. The first one, mapper, takes each list of ingredients from each record, and splits the ingredients into separate records in a table. You are using a reusable library to split text into a table. The function also counts the occurrences of an ingredient within a list. The MAP_MERGE operator then merges the results into a single table with the individual ingredient and its original ID.
mapperThe second function, the reducer, will use each group of ingredient tables for each ID, process all groups and return the total count for the ingredients.
mapperYou can learn more about these examples in this blog post. Note the input is limited so you can finish on time for this exercise.
Step 8Find who you are connected to through the same food
+
The previous function associated participants through food and their ingredients. You can now represent that relationship in a graph
Share feedback on this tutorial or join the conversation in SAP Community.
Submit detailed feedbackDiscuss in Community
Steps
Step 1 of 8
1. Connect to the database2. Explore the existing data3. Create a view4. Create structures for training5. Train the model6. Use the model to predict the nutrition score7. Get the most popular ingredients8. Find who you are connected to through the same food
Joule
AI Notice
Joule is an AI assistant. Generative AI may produce inaccurate, incomplete, or biased information. Always verify important details before acting on them.
Conversations are sent to SAP-hosted large language models for processing. Do not include personal data, credentials, or confidential information in your messages.
Joule's responses are based on the SAP tutorial catalog and may not reflect the latest product changes. For authoritative guidance, consult the linked tutorials and official SAP documentation.