SapHanaTutorial.Com HOME     Learning-Materials Interview-Q&A Certifications Quiz Online-Courses Forum Jobs Trendz FAQs  
     Explore The World of Hana With Us     
About Us
Contact Us
 Apps
X
HANA App
>>>
Hadoop App
>>>
Tutorial App on SAP HANA
This app is an All-In-One package to provide everything to HANA Lovers.

It contains
1. Courses on SAP HANA - Basics, Modeling and Administration
2. Multiple Quizzes on Overview, Modelling, Architeture, and Administration
3. Most popular articles on SAP HANA
4. Series of Interview questions to brushup your HANA skills
Tutorial App on Hadoop
This app is an All-In-One package to provide everything to Hadoop Lovers.

It contains
1. Courses on Hadoop - Basics and Advanced
2. Multiple Quizzes on Basics, MapReduce and HDFS
3. Most popular articles on Hadoop
4. Series of Interview questions to brushup your skills
Apps
HANA App
Hadoop App
';
Search
Stay Connected
Search Topics
Topic Index
+
-
XSA Overview
+
-
XS Overview
+
-
Hello World Example
+
-
XS OData Service
+
-
HANA XSJS Service
+
-
Core Data Service
+
-
Others

XS Job Schedule

Introduction:

The XSJob file enables you to run a service (for example, an XS JavaScript or a SQLScript) at a scheduled interval.
In this article, we will explain how to schedule a job that triggers an XS JavaScript application.

Prerequisites:


Use-case:

In this example we will create a job that triggers an XS JavaScript application that
  • Reads the latest value of a share price from a public financial service available on the Internet.
  • Update the stock value for SAP DE in HANA table with current timestamps.

The job should run every 10th second.

Create Table:

Copy paste the below SQL script to create table.
--REPLACE <YOUR_SCHEMA> WITH YOUR SCHEMA NAME
CREATE COLUMN TABLE <YOUR_SCHEMA>.STOCK (
CREATED_AT TIMESTAMP PRIMARY KEY,
STOCK_VALUE FLOAT
)


Create XSJob to get SAP stock value and update in HANA table:

  1. Create HANA XS Project as mentioned in Create Your First HANA XS Application using HANA Studio

Note: We highly recommend you that you create a package with the name “xsjob-tutorial” in Content and create the project inside it. Otherwise you will have to change the package reference in XSJS and XSJob files.
  1. Create a package inside the project called “jobs” and create 3 files
    1. yahoo.xsjob // job schedule definition
    2. yahoo.xshttpdest // HTTP destination details
    3. yahoo.xsjs // Script to run on schedule
SAP HANA XS Job

  1. Open yahoo.xsjs file and paste below code.

function readStock(input) {
    var stock = input.stock;
    var dest = $.net.http.readDestination("xsjob-tutorial.jobs", "yahoo");
    var client = new $.net.http.Client();
    var req = new $.web.WebRequest($.net.http.GET, "/d/quotes.csv?f=a&s=" + stock);
    client.request(req, dest);
    var response = client.getResponse();
    var stockValue;
    if (response.body){
       stockValue = parseInt(response.body.asString(), 10);
    }
    var sql = "INSERT INTO SAP_HANA_TUTORIAL.STOCK VALUES (NOW(), ?)";
    var conn = $.db.getConnection();
    var pstmt = conn.prepareStatement(sql);
    pstmt.setDouble(1, stockValue);
    pstmt.execute();
    conn.commit();
    conn.close();
}


Note: You may have to change the package name in the line
$.net.http.readDestination("xsjob-tutorial.jobs", "yahoo");


  1. Open the HTTP destination file yahoo.xshttpdest and paste below content.
host = "download.finance.yahoo.com";
port = 80; 
description = "my stock-price checker";
useSSL = false;
pathPrefix = "/d/quotes.csv?f=a";
authType = none;
useProxy = false;
proxyHost = "";
proxyPort = 0;timeout = 0;

Note: In case you are behind a proxy, then change the proxy settings. For example:
useProxy = true;
proxyHost = "proxy";
proxyPort = 8080;

  1. Open XS job file and paste below content

{
   "description": "Read stock value",
   "action": "xsjob-tutorial.jobs:yahoo.xsjs::readStock",
   "schedules": [{
       "description": "Read current stock value every 10 second",
       "xscron": "* * * * * * 0:59/10",
       "parameter": {
           "stock": "SAP.DE"
       }
   }]
}

Note: You may have to change the package name in the line
"action": "xsjob-tutorial.jobs:yahoo.xsjs::readStock",

  1. Save and activate all the files.
  2. Open HANA Admin tool. The URL of Admin tool is:
    http://<WebServerHost>:80<SAPHANAinstance>/sap/hana/xs/admin/

  1. Activate the job. Specify User name and other details and save the job.

SAP HANA XS Job

  1. Check the job logs to ensure the XS job is active and running according to the defined schedule.

You can open the table STOCK and see the data inserted into it.
SAP HANA XS Job

Download Full Source Code:

Click here to download the full source code.

Continue reading:



Support us by sharing this article.



Explore More
Close X
Close X

2 thoughts on “SAP HANA XS Job Schedule – XSJob

  1. Sumeet Durgia says:

    Hi,

    In my case job scheduler is disabled and I am not able to change the same.

    Will it affect my scheduling? currently everything is activated and I have created a procedure to insert the records but records are not getting inserted. And job is showing in scheduled state.

    Br
    Sumeet

    • Admin says:

      Hi Summet,
      Please note that you need to start the job from HANA Admin tool.
      Check the step 8.
      You may have to enable the Scheduler.

Leave a Reply

Your email address will not be published. Required fields are marked *

Current day month ye@r *

 © 2017 : saphanatutorial.com, All rights reserved.  Privacy Policy