美文网首页
Spring boot 操作doris

Spring boot 操作doris

作者: wudl | 来源:发表于2021-08-07 14:39 被阅读0次

    1. 启动doris

    1.1 一台fe 三台be

    [root@node01 bin]# ./start_fe.sh --daemon
    [root@node01 bin]# ./start_be.sh --daemon
    [root@node02 bin]# ./start_be.sh --daemon
    [root@node03 bin]# ./start_be.sh --daemon
    
    

    1.2 连接 doris

    [root@node01 bin]# mysql -h 192.168.1.161 -P9030 -uroot
    mysql> create database wudl_db;
    Query OK, 0 rows affected (0.01 sec)
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | test               |
    | test_db            |
    | wudl_db            |
    +--------------------+
    
    
    
    

    1.3 创建表

    mysql> CREATE TABLE `doris_test` (
        ->   `id` int NULL COMMENT "id",
        ->   `name` varchar(40) NULL COMMENT "名称"
        -> ) ENGINE=OLAP
        -> DUPLICATE KEY(`id`)
        -> COMMENT "OLAP"
        -> DISTRIBUTED BY HASH(`id`) BUCKETS 1
        -> PROPERTIES (
        -> "replication_num" = "1",
        -> "in_memory" = "false",
        -> "storage_format" = "V2"
        -> );
    Query OK, 0 rows affected (0.06 sec)
    
    

    1.4 插入数据

    mysql> insert into doris_test (id,name) values(1,'hdfs');
    Query OK, 1 row affected (0.07 sec)
    {'label':'insert_683fe77d679648be-b9440896f330d70c', 'status':'VISIBLE', 'txnId':'30'}
    
    

    2.通过接口(Spring boot mybatis)的方式访问 数据

    2.1 项目结构

    mybatis-dois.png
    package com.wudl.doris.controller;
    
    import com.wudl.doris.domain.DorisTest;
    import com.wudl.doris.service.DorisService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * @ClassName : DorisController
     * @Description :
     * @Author :wudl
     * @Date: 2021-08-08 13:58
     */
    @RestController
    public class DorisController {
    
        @Autowired
        DorisService dorisService;
    
        @GetMapping("/dorislist")
        public List<DorisTest>  getListDoris()
        {
            return  dorisService.listDoris();
        }
    }
    
    
    package com.wudl.doris.domain;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    /**
     * @ClassName : doris_test
     * @Description :
     * @Author :wudl
     * @Date: 2021-08-08 13:53
     */
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class DorisTest {
    
        private int id;
        private String name;
    
    }
    
    
    package com.wudl.doris.mapper;
    
    import com.wudl.doris.domain.DorisTest;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    /**
     * @ClassName : DorisMapper
     * @Description :
     * @Author :wudl
     * @Date: 2021-08-08 14:01
     */
    
    @Mapper
    public interface DorisMapper {
    
        /**
         * 查询所有的doris 数据
         * @return
         */
       public  List<DorisTest> listDoris();
    
    }
    
    
    package com.wudl.doris.service;
    
    import com.wudl.doris.domain.DorisTest;
    
    import java.util.List;
    
    /**
     * @ClassName : DorisService
     * @Description :
     * @Author :wudl
     * @Date: 2021-08-08 13:58
     */
    
    public interface DorisService {
    
        /**
         * 查询所有的表数据
         * @return
         */
       public List<DorisTest> listDoris();
    
    }
    
    
    package com.wudl.doris.service.impl;
    
    import com.wudl.doris.domain.DorisTest;
    import com.wudl.doris.mapper.DorisMapper;
    import com.wudl.doris.service.DorisService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * @ClassName : DorisServiceImpl
     * @Description :
     * @Author :wudl
     * @Date: 2021-08-08 14:01
     */
    
    @Service
    public class DorisServiceImpl implements DorisService {
    
        @Autowired
        DorisMapper dorisMapper;
        @Override
        public List<DorisTest> listDoris() {
            return dorisMapper.listDoris();
        }
    }
    
    
    package com.wudl.doris;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @ClassName : App
     * @Description :
     * @Author :wudl
     * @Date: 2021-08-08 12:5151
     */
    
    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    
    
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.wudl.doris.mapper.DorisMapper">
    
        <select id="listDoris" resultType="com.wudl.doris.domain.DorisTest">
            select id,name from doris_test
        </select>
    </mapper>
    

    application.yml

    server:
      port: 8080
    spring:
      #数据库连接配置
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.1.161:9030/wudl_db?characterEncoding=utf-8&useSSL=false
        username: root
        password:
    
    #mybatis的相关配置
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.wudl.doris.domain.DorisTest
    
    <?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>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>springboot-doris-db</artifactId>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <mybatis.spring.boot.starter.version>1.3.2</mybatis.spring.boot.starter.version>
            <fastjson.version>1.2.70</fastjson.version>
            <druid.version>1.1.14</druid.version>
            <commons.io.version>2.5</commons.io.version>
        </properties>
    
        <dependencies>
            <!-- alibaba druid -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.15</version>
            </dependency>
    
            <!-- SpringBoot Web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
            <!-- pool -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
            </dependency>
    
    
            <!-- SpringBoot mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.starter.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
            </dependency>
    
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons.io.version}</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.20</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    3. 请求结果

    spring-boot dois.png

    4. 总结

    总的来说doris  可以当做一个mysql  数据库用mybatis 来操作, 因为他支持mybatis  协议。
    

    相关文章

      网友评论

          本文标题:Spring boot 操作doris

          本文链接:https://www.haomeiwen.com/subject/rtlovltx.html