doris java jdbc 连接  

和使用mysql一样使用。   springdata  jpa  mybaatis 也一样。

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.16.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sea</groupId>
    <artifactId>prestotest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>prestotest</name>
    <description>prestoTest</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
     <!--   <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

application.properties:

#datasource.doris.type=org.apache.tomcat.jdbc.pool.DataSource
datasource.doris.sql-script-encoding=UTF-8
datasource.doris.jdbc-url=jdbc:mysql://192.168.18.129:9030/sea
datasource.doris.username=sea
datasource.doris.password=123456
datasource.doris.driver-class-name=com.mysql.jdbc.Driver
#datasource.doris.driver-class-name=com.mysql.cj.jdbc.Driver
#jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

 

config:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
 * @PACKAGE : com.sea.doristest.config
 * @Author :  Sea
 * @Date : 9/21/20 11:44 AM
 * @Desc :
 **/
@Configuration
public class DorisDataSourceConfig {
        private static Logger LOG = LoggerFactory.getLogger(DorisDataSourceConfig.class);
        @Bean(name = "dorisDataSource")
        @ConfigurationProperties(prefix ="datasource.doris")
        public DataSource prestoDataSource() {
            return DataSourceBuilder.create().build();
        }
        @Bean(name = "dorisTemplate")
        public JdbcTemplate dorisJdbcTemplate(@Qualifier("dorisDataSource") DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }
}

 

test case:

    @Autowired
    @Qualifier("dorisTemplate")
    private JdbcTemplate dorisTemplate;

    @Test
    public void testDoris() {
        String sql="select * from sea.user ";
        sql="SELECT * FROM example_db.table1";
        List<Map<String, Object>> maps = dorisTemplate.queryForList(sql);
        System.err.println(maps);
        System.err.println(maps.size());
    }

 

 

result:

[{siteid=4, citycode=3, username=bush, pv=3}, {siteid=1, citycode=1, username=jim, pv=2}, {siteid=5, citycode=3, username=helen, pv=3}, {siteid=3, citycode=2, username=tom, pv=2}, {siteid=2, citycode=1, username=grace, pv=2}]
5

 

上一篇:Doris消费Kafka数据


下一篇:Doris开发手记1:解决蛋疼的MySQL 8.0连接问题