原创

Spring Boot集成hana快速入门demo

1.什么是hana?

SAP HANA高性能分析工具)是一款将数据存储在内存而不是硬盘的多模型数据库。这款列式内存数据库支持企业在同一系统中执行快速的事务处理和先进的分析处理。为什么 SAP HANA 如此重要?因为该产品支持企业近乎零延迟地处理海量数据,即时查询数据,并真正实现由数据驱动。SAP HANA 拥有独特的优势。该平台将数据存储在主内存的列存储表中,并且集联机分析处理 (OLAP) 和联机事务处理 (OLTP) 于一体,处理速度比目前市场上的其他数据库管理系统 (DBMS) 快很多

什么是内存数据库?

内存数据库 (IMDB) 是一种将数据存储在计算机主内存 (RAM),而不是传统磁盘或固态硬盘 (SSD) 的数据库。虽然如今大多数数据库都增加了许多内存功能,但从根本上来说还是基于磁盘的存储数据库。SAP HANA 则是从零开始构建,主要目的是利用内存处理数据,其次才是利用其他必要的存储机制来平衡性能和成本。从内存中检索数据要比从磁盘或固态硬盘中检索数据快得多,因此 SAP HANA 可以实现瞬时响应。

SAP HANA 有多快?

  •  比传统数据库快 3600 倍
  • 1 秒内给出查询结果
  • 每核每秒扫描 35 亿次
  • 每核每秒执行 1,500 万次聚合

SAP HANA 架构

SAP HANA 采用列式内存架构,支持快速执行查询和事务处理。除此之外,该架构还提供数据库管理、应用开发、高级分析处理和灵活的数据虚拟化功能。

67515

2.hana环境搭建

pull images

docker pull saplabs/hanaexpress:latest

run

docker run -p 39013:39013 -p 39017:39017 -p 39041-39045:39041-39045 -p 1128-1129:1128-1129 -p 59013-59014:59013-59014 -v D:/IdeaProjects/ETFramework/hana/docker/hana/data/sap:/hana/mounts --name hana1 saplabs/hanaexpress:latest --passwords-url file:///hana/mounts/passwd.json --agree-to-sap-license

Connect via JDBC driver

To log into your system database via JDBC, use the following command:
jdbc:sap://<ip_address>:39017/?databaseName=<database_name>
To log into your tenant database via JDBC, use the following command:
jdbc:sap://<ip_address>:39041/?databaseName=<tenant_name>

passwd.json

${path}/hana/data/sap/passwd.json
{
"system_user_password" : "Hyd20240531",
"default_tenant_system_user_password" : "Hyd20240531"
}
默认用户名
system_user: SYSTEM
default_tenant_system_user:SYSTEM

init data

-- 创建新数据库
CREATE DATABASE demo;

-- 授予权限给特定用户
GRANT ALL PRIVILEGES ON DATABASE demo TO my_user;

-- 创建表
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
Department NVARCHAR(50),
Salary DECIMAL(10, 2)
);

-- 插入测试数据
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary)
SELECT 1, 'John', 'Doe', 'Engineering', 60000.00 FROM DUMMY UNION ALL
SELECT 2, 'Jane', 'Smith', 'HR', 55000.00 FROM DUMMY UNION ALL
SELECT 3, 'Alice', 'Johnson', 'Finance', 65000.00 FROM DUMMY UNION ALL
SELECT 4, 'Bob', 'Brown', 'Marketing', 58000.00 FROM DUMMY UNION ALL
SELECT 5, 'Emma', 'Lee', 'Engineering', 62000.00 FROM DUMMY;
 

3.代码工程

实验目标:实现对hana数据库的读写

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hana</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Spring Boot Starter Data JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- SAP HANA JDBC Driver -->
        <dependency>
            <groupId>com.sap.cloud.db.jdbc</groupId>
            <artifactId>ngdbc</artifactId>
            <version>2.14.7</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

controller

package com.et.hana.controller;

import com.et.hana.entity.Employee;
import com.et.hana.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public List<Employee> getAllEmployees() {
        return employeeService.getAllEmployees();
    }

    @PostMapping
    public Employee createEmployee(@RequestBody Employee employee) {
        return employeeService.saveEmployee(employee);
    }

    // other CRUD endpoints
}

service

package com.et.hana.service;

import com.et.hana.entity.Employee;
import com.et.hana.reponsitory.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

    public List<Employee> getAllEmployees() {
        return employeeRepository.findAll();
    }

    public Employee saveEmployee(Employee employee) {
        return employeeRepository.save(employee);
    }

    // other CRUD methods
}

entity

package com.et.hana.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Data
public class Employee {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String position;

    // getters and setters
}

reponsitory

package com.et.hana.reponsitory;

import com.et.hana.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

application.yaml

spring.datasource.url=jdbc:sap://localhost:39041/?currentschema=DEMO
spring.datasource.username=System
spring.datasource.password=Hyd20240531
spring.datasource.driver-class-name=com.sap.db.jdbc.Driver

# JPA (optional)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.HANARowStoreDialect

4.测试

启动spring boot 应用

写入数据

post http://localhost:8080/employees
{
 "id":1,
 "name":"harries",
 "position":"manager"
}

读数据

get http://127.0.0.1:8080/employees
[{"id":1,"name":"harries","position":"manager"}]

5.引用

正文到此结束
Loading...