Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
jan_teichmann
Explorer
Today, SAP HANA is one of the most compelling and innovative development platforms worldwide. It offers opportunities for various types of new applications which work on huge amounts of data and need very fast access to them. In this blog I focus on
  • data that originate in the Cloud or are best stored in the Cloud: therefore, HANA One (http://www.saphana.com/community/learn/cloud-info) in the Amazon Cloud, is the platform of choice
    • there are also other Cloud Service Providers running HANA One like Portugal Telecom or Korea Telecom and others, please search the Web for them
  • small smartphone apps that show the new opportunities in a very concise way and at the same time leverage all important aspects of the technology stack - I start with a tutorial on Android.
I try to use the leanest setup possible. Therefore, in the base scenario, I don't use HTML5, I just select data out of a simple table in HANA via the simplest possible SQL statement, but I do create a small HANA XS app because this is the way to connect to HANA directly via http/ https. There are some extension scenarios to show more technology to enrich your application, see at the bottom.
I did it on a Windows machine. But it should also run on other OS.

App idea

The example app I build further down is comparing some stock exchange data and their development over time. Therefore, I propose some fictive names for all the artifacts you will create.

Prerequisites and Installs

Before starting the app creation in 20 minutes we need to make sure to install the following components (this would exceed the 20 minutes...):

Create an Android App

Create an Adnroid Application:
  • Start your Eclipse version that you installed for Android development.
  • In the Package Explorer of the Java perspective right-click "New --> "Android Application Project".
  • Application Name "Android App on HANA base scenario"
  • keep all the other settings on New Adnroid Application Name screen as defaulted
  • Select any start icon you want on Configure Launcher Icon screen
  • Select a Blank Activity on the Create Activity screen
  • Use Activity name "ShowMarketPriceData" on the New Blank Activity screen
Give the application an Id:
  • In the the Package Explorer double click --> res --> layout --> activity_show_market_price_data.xml
  • Use the XML layout instead of the Graphical Layout tab (bottom) in the middle part and exchange the id-line in the <TextView ... /> part with this one:
    • android:id="@+id/MyTextResponse"
Allow access to the internet for the new app
  • In the Package Explorer double-click on AndroidManifest.xml
  • In the middle part of the screen, use the Permissions tab (bottom)
  • Add Uses Permission and assign name android.permission.INTERNET
Download additional library:
  • Download json-simple-1.1.1.jar for instance from http://code.google.com/p/json-simple/downloads/list
  • Put it into your lib directory, for instance C:\blabla\workspace_android\AndroidAppOnHANABaseScenario\libs\json-simple-1.1.1.jar
  • Right-click on Package Explorer double click --> libs and Refresh: it should show up
Add code to show some data (see also http://www.mkyong.com/android/android-gridview-example/😞
  • In the the Package Explorer double click --> src --> com.example.androidapponhanabasescenario --> ShowMarketPriceData.java
  • Add imports at the top:
    • import android.widget.TextView;
    • import android.os.AsyncTask;
    • import android.util.Base64;
    • import java.net.URL;
    • import java.net.URLConnection;
    • import java.io.BufferedReader;
    • import java.io.InputStreamReader;
  • In public class ShowMarketPriceData add the following code:
  • In method onCreate, just below setContentView(R.layout.activity_show_market_price_data);
ShowDialogAsyncTask aTask = new ShowDialogAsyncTask();
aTask.execute();
  • Also add this method and private class:
public String getOdata() {
     String JASONrs = "You will get there!";
                              // some more code is to come here
                              return JASONrs;
                           }
                          private class ShowDialogAsyncTask extends        AsyncTask<Void , Void , String>{
                              @Override
                              protected String doInBackground(Void... arg0) {
                                   return getOdata();
                                }
                              @Override
                              protected void onPostExecute(String result) {
                                    TextView tv = (TextView) findViewById(R.id.MyTextResponse);
                                   tv.setText(result);
                                }
   }
Use overall Save, again select activity_show_market_price_data.xml and click Run as Android Application
Your Android or Android Emulator should bring up a screen that shows "You will get there!".
Congrats! For now, your done on the Android side!

Create a HANA XS app

Start your HANA Studio. Connect to your HANA (as described in the SCN link mentioned further up).
 
Create a new HANA workspace:
  • In the SAP HANA Development perspective in the SAP HANA Repositories window on the left, right-click and select "New repository workspace"
  • Select your HANA SYSTEM, put the name "CompareStockMarketData" and define the workspace root directory or leave it as defaulted
Create and share a project for SAP HANA XS:
  • In the SAP HANA Development perspective, in the Project Explorer on the left, right-click New --> Project and select SAP HANA Development --> XS Project, put name "androidapponhana",  click Finish
  • In Project Explorer, right-click androidapponhana --> Team --> Share Project... 
  • Select your SAP HANA System, select Workspace Name CompareStockMarketData, (leave Repository package empty for now) and click Finish
Add necessary minimum artifacts
  • Within project androidapponhana, Right-click --> JavaScript Resources --> androidapponhana and select New --> Other
    • In the upcoming Wizard select General --> File and click Next, put ".xsapp" as name and click Finish (it shall remain empty for our purposes)
  • Do the same thing again - let's call this proceeding XS artifact addition - and create file ".xsaccess"
    • In the Project Explorer double click on file .xsaccess and put the following content into it
{
"exposed"
: true,
"authentication"
: [ { "method" : "Basic" } ]
}
  • Perform XS artifact addition and create file "COMPARE_STOCK.hdbschema" and put the following content into it
schema_name="COMPARE_STOCK";
  • Perform XS artifact addition and create file "PRICE_HISTORY.hdbtable" and put the following content into it
table.schemaName = "COMPARE_STOCK";
table.tableType = COLUMNSTORE;
table.columns = [
{name = "NSIN"; sqlType = SHORTTEXT; nullable = false; length = 12;  },
{name = "DATE"; sqlType = DATE; nullable = false; },
{name = "TIME"; sqlType = TIME; nullable = false; },
{name = "DAY_OPEN"; sqlType = DECIMAL; nullable = false; precision = 8; scale = 3;},
{name = "DAY_HIGH"; sqlType = DECIMAL; nullable = false; precision = 8; scale = 3;},
{name = "DAY_LOW"; sqlType = DECIMAL; nullable = false; precision = 8; scale = 3;},
{name = "DAY_CLOSE"; sqlType = DECIMAL; nullable = false; precision = 8; scale = 3;},
{name = "VOLUME"; sqlType = INTEGER; nullable = false; }
];
table.primaryKey.pkcolumns = ["NSIN", "DATE", "TIME"];
  • Perform XS artifact addition and create file "StockDataIF.xsodata" and put the following content into it
               service {
                    "androidapponhana::PRICE_HISTORY" as "History" ;
               }
Click the overall Save button.
In Project Explorer, right-click androidapponhana --> Team --> Commit.
In Project Explorer, right-click androidapponhana --> Team --> Activate.
Now, grant privileges to your user and put some test data in:
  • Go to SAP HANA Studio, for instance to the Quicklaunch perspective, in the Navigator view on the left open an SQL COnsole
  • Type and execute (make sure to put your user instead of the placeholder):
call _SYS_REPO.GRANT_SCHEMA_PRIVILEGE_ON_ACTIVATED_CONTENT('select', 'COMPARE_STOCK', '<your user>');
call _SYS_REPO.GRANT_SCHEMA_PRIVILEGE_ON_ACTIVATED_CONTENT('insert', 'COMPARE_STOCK', '<your user>');
  • Type and execute
    insert into "COMPARE_STOCK"."androidapponhana::PRICE_HISTORY" ( NSIN, DATE, TIME, DAY_OPEN, DAY_HIGH, DAY_LOW, DAY_CLOSE, VOLUME ) values ('000716460', '20130205', '130000', 60.19, 60.55, 59.91, 60.2, 2313291);
Now open a browser and test (put IP address and port/HANA instance number of your HANA server instead of the placeholders):
Congrats! This was the HANA XS stuff. Let's go back to the Adnroid side.

Calling the XS app from the Android App

Enhance Android Java code to show your HANA XS data:
  • In the the Package Explorer double click --> src --> com.example.androidapponhanabasescenario --> ShowMarketPriceData.java
  • In public class ShowMarketPriceData replace the line "   // some more code is to come here" by:
          try {
                URL myhana = new URL(

                URLConnection hanacon;
               hanacon = myhana.openConnection();
               hanacon.setReadTimeout(1000);
               hanacon.setConnectTimeout(1000);

               String userpass = "SYSTEM" + ":" + "<your pw>";
               String basicAuth = "Basic " + new String(Base64.encode(userpass.getBytes(), 0));
               hanacon.setRequestProperty ("Authorization", basicAuth);

               BufferedReader in = new BufferedReader(new InputStreamReader(hanacon.getInputStream()));
               String inputLine;

               while ((inputLine = in.readLine()) != null) JASONrs += inputLine;
               in.close();
           } catch (Exception e) {
               e.printStackTrace();
               return "Error";
          }
  • Make sure to replace user (for instance SYSTEM) and <your pw> (for instance manager).
  • Save and test your Android app. It shall return a lot of JSON text on your screen starting with the string "You will get there!".
    Congrats! That's it.
    Don't be shocked - it should look like this,
    but in my next Blog I will make it look a little bit nicer :wink: !

    Closing remarks and extension Scenarios

    Now, of course this is just a "Hellow World!" type of application. But it shows nicely what technology can be used.
    So far, I will leave it to the user to imagine a more appropriate way of putting user credentials. To reach a good security level for an App you should think of: SSL, do save user credentials on local device not at all or only encrypted (use OAUTH if you can) and save local data in a secure way.
    In the subsequent blogs on this topic I will make it look nicer and put more business sense to it. I decided to also create tutorials for some extension scenarios. These are very much built on top of the base scenario, but they make the app look more appealing and they show a lot more interesting technology.
    1. Android: Show HANA data in a gridview
    2. Android: Show HANA data as a graph
    3. Android: Use HTML5 to show data
    4. XS: Show data from a CalcView
    5. XS: Show data tweaked by SQLScript
    8 Comments