#1
This guide shows how to build real-time analytics in SAP HANA Express and expose them with Spring Boot APIs.

1. Create Analytics Table in HANA

CREATE COLUMN TABLE SALES (
    ID INTEGER PRIMARY KEY,
    PRODUCT NVARCHAR(100),
    AMOUNT DECIMAL(10,2),
    REGION NVARCHAR(50),
    SALE_DATE TIMESTAMP
);
Insert data:
INSERT INTO SALES VALUES (1, 'Laptop', 1200, 'US', CURRENT_TIMESTAMP);
INSERT INTO SALES VALUES (2, 'Phone', 800, 'EU', CURRENT_TIMESTAMP);

2. Configure Spring Boot

application.properties:
spring.datasource.url=jdbc:sap://localhost:39015/?databaseName=HXE
spring.datasource.username=SYSTEM
spring.datasource.password=YourPassword
spring.datasource.driver-class-name=com.sap.db.jdbc.Driver

3. Repository for Analytics

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

@Repository
public class AnalyticsRepository {
    private final JdbcTemplate jdbc;

    public AnalyticsRepository(JdbcTemplate jdbc) {
        this.jdbc = jdbc;
    }

    public List<Map<String, Object>> salesByRegion() {
        String sql = "SELECT REGION, SUM(AMOUNT) AS TOTAL FROM SALES GROUP BY REGION";
        return jdbc.queryForList(sql);
    }

    public List<Map<String, Object>> recentSales() {
        String sql = "SELECT * FROM SALES ORDER BY SALE_DATE DESC LIMIT 5";
        return jdbc.queryForList(sql);
    }
}

4. Expose REST Endpoints

import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/analytics")
public class AnalyticsController {
    private final AnalyticsRepository repo;

    public AnalyticsController(AnalyticsRepository repo) {
        this.repo = repo;
    }

    @GetMapping("/region")
    public List<Map<String, Object>> salesByRegion() {
        return repo.salesByRegion();
    }

    @GetMapping("/recent")
    public List<Map<String, Object>> recentSales() {
        return repo.recentSales();
    }
}

5. Test in Real-Time

Run Spring Boot app:
mvn spring-boot:run
API calls:
GET /analytics/region   → { "US": 1200, "EU": 800 }
GET /analytics/recent   → shows latest sales records

image quote pre code