Spring Boot集成tablesaw插件快速入门Demo

1 什么是tablesaw?

Tablesaw是一款Java的数据可视化库,主要包括两部分:

  • 数据解析库,主要用于加载数据,对数据进行操作(转化,过滤,汇总等),类比Python中的Pandas库;

  • 数据可视化库,将目标数据转化为可视化的图表,类比Python中的Matplotlib库。

与Pandas不同的是,Tablesaw中的表格以列(Column)为基本单位,因此大部分操作都是基于列进行的。当然也包括部分对行操作的函数,但是功能比较有限

1.1 tablesaw目录说明

  1. aggregate:maven 的项目父级项目,主要定义项目打包的配置。

  2. beakerx:tablesaw 库的注册中心,主要注册表和列。

  3. core:tablesaw 库的核心代码,主要是数据的加工处理操作:数据的追加,排序,分组,查询等。

  4. data:项目测试数据目录。

  5. docs:项目 MarkDown 文档目录。

  6. docs-src:项目文档源码目录,主要作用是生成 MarkDown 文档。

  7. excel:解析 excel 文件数据的子项目。

  8. html:解析 html 文件数据的子项目。

  9. json:解析 json 文件数据的子项目。

  10. jsplot:数据可视化的子项目,主要作用加载数据生成可视化图表。

  11. saw:tablesaw 读写图表数据的子项目。

2 环境准备

2.1 数据库安装

数据库安装这里不做详细阐述,小伙伴们可自行安装,在docker环境下可执行:

bash 复制代码
docker run --name docker-mysql-5.7 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7

2.2 数据库表初始化

sql 复制代码
create database springboot_demo;
create table user_info
(
user_id     varchar(64)          not null primary key,
username    varchar(100)         null ,
age         int(3)               null ,
gender      tinyint(1)           null ,
remark      varchar(255)         null ,
create_time datetime             null ,
create_id   varchar(64)          null ,
update_time datetime             null ,
update_id   varchar(64)          null ,
enabled     tinyint(1) default 1 null
);
INSERT INTO springboot_demo.user_info
(user_id, username, age, gender, remark, create_time, create_id, update_time, update_id, enabled)
VALUES('1', '1', 1, 1, '1', NULL, '1', NULL, NULL, 1);

3 代码demo

3.1 完成目标

利用tablesaw加工和处理二维数据,并且可视化

3.2 pom.xml

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.wkf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>tablesaw</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>
        <dependency>
            <groupId>tech.tablesaw</groupId>
            <artifactId>tablesaw-core</artifactId>
            <version>0.43.1</version>
        </dependency>
        <dependency>
            <groupId>tech.tablesaw</groupId>
            <artifactId>tablesaw-jsplot</artifactId>
            <version>0.43.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
    </dependencies>
</project>

3.3 application.yaml

yaml 复制代码
server:
  port: 8088
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://127.0.0.1:3305/springboot_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

3.4 读取csv数据

java 复制代码
    @Before
    public void before() {
        log.info("init some data");
        tornadoes = Table.read().csv("D:/gitProject/springboot-demo/tablesaw/src/main/resources/data/tornadoes_1950-2014.csv");
    }

3.5 打印列名

java 复制代码
    @Test
    public void columnNames() {
        System.out.println(tornadoes.columnNames());
    }

运行效果:

3.6 查看shape

java 复制代码
    @Test
    public void shape() {
        System.out.println(tornadoes.shape());
    }

运行效果:

3.7 查看表结构

java 复制代码
    @Test
    public void structure() {
        System.out.println(tornadoes.structure().printAll());
    }

运行效果:

3.8 查看数据

java 复制代码
    @Test
    public void show() {
        System.out.println(tornadoes);
    }

运行效果:

3.9 表结构过滤

java 复制代码
    @Test
    public void structurefilter() {
        System.out.println( tornadoes
                .structure()
                .where(tornadoes.structure().stringColumn("Column Type").isEqualTo("DOUBLE")));

    }

运行效果:

3.10 预览数据

java 复制代码
	@Test
    public void previewdata() {
        System.out.println(tornadoes.first(3));
    }

运行效果:

3.11 列操作

java 复制代码
    @Test
    public void ColumnOperate() {
        StringColumn month = tornadoes.dateColumn("Date").month();
        tornadoes.addColumns(month);
        System.out.println(tornadoes.first(3));
        tornadoes.removeColumns("State No");
        System.out.println(tornadoes.first(3));

    }

运行效果:

3.12 排序

java 复制代码
    @Test
    public void sort() {
        tornadoes.sortOn("-Fatalities");
        System.out.println(tornadoes.first(20));
    }

运行效果:

3.13 summary

java 复制代码
    @Test
    public void summary() {
        System.out.println( tornadoes.column("Fatalities").summary().print());
    }

运行效果:

3.14 数据过滤

java 复制代码
    @Test
    public void filter() {
        Table result = tornadoes.where(tornadoes.intColumn("Fatalities").isGreaterThan(0));
        result = tornadoes.where(result.dateColumn("Date").isInApril());
        result =
                tornadoes.where(
                        result
                                .intColumn("Width")
                                .isGreaterThan(300) // 300 yards
                                .or(result.doubleColumn("Length").isGreaterThan(10))); // 10 miles

        result = result.select("State", "Date");
        System.out.println(result);
    }

运行效果:

3.15 写入文件

java 复制代码
    @Test
    public void write() {
        tornadoes.write().csv("rev_tornadoes_1950-2014-test.csv");
    }

3.16 从mysql读取数据

java 复制代码
    @Resource
    private JdbcTemplate jdbcTemplate;
    
    @Test
    public void dataFromMySql() {
        Table table = jdbcTemplate.query("SELECT  user_id,username,age from user_info", resultSet -> {
            return Table.read().db(resultSet);
        });
        System.out.println(table);
    }

运行效果:

3.17 数据可视化

java 复制代码
package com.wkf.tablesaw;

import tech.tablesaw.api.Table;
import tech.tablesaw.plotly.Plot;
import tech.tablesaw.plotly.api.BubblePlot;
import tech.tablesaw.plotly.components.Figure;

import java.io.IOException;

/**
 * @author wuKeFan
 * @date 2024-06-13 09:57:07
 */
public class BubbleExample {

    public static void main(String[] args) throws IOException {

        Table wines = Table.read().csv("D:/gitProject/springboot-demo/tablesaw/src/main/resources/data/tornadoes_1950-2014.csv");

        Table champagne =
                wines.where(
                        wines
                                .stringColumn("wine type")
                                .isEqualTo("Champagne & Sparkling")
                                .and(wines.stringColumn("region").isEqualTo("California")));

        Figure figure =
                BubblePlot.create(
                        "Average retail price for champagnes by year and rating",
                        champagne, // table namex
                        "highest pro score", // x variable column name
                        "year", // y variable column name
                        "Mean Retail" // bubble size
                );

        Plot.show(figure);
    }

}

结果如下图:

4 代码仓库

https://github.com/363153421/springboot-demo/tree/master/tablesaw

相关推荐
每天一个秃顶小技巧9 分钟前
02.Golang 切片(slice)源码分析(一、定义与基础操作实现)
开发语言·后端·python·golang
帮帮志16 分钟前
vue实现与后台springboot传递数据【传值/取值 Axios 】
前端·vue.js·spring boot
gCode Teacher 格码致知1 小时前
《Asp.net Mvc 网站开发》复习试题
后端·asp.net·mvc
杨不易呀2 小时前
Java面试高阶篇:Spring Boot+Quarkus+Redis高并发架构设计与性能优化实战
spring boot·redis·高并发·分布式锁·java面试·quarkus
Moshow郑锴3 小时前
Spring Boot 3 + Undertow 服务器优化配置
服务器·spring boot·后端
码农飞哥3 小时前
互联网大厂Java面试实战:Spring Boot到微服务的技术问答解析
java·数据库·spring boot·缓存·微服务·消息队列·面试技巧
请你喝好果汁6414 小时前
TWASandGWAS中GBS filtering and GWAS(1)
信息可视化·数据挖掘·数据分析
张人玉4 小时前
数据可视化大屏——物流大数据服务平台(二)
大数据·信息可视化
Chandler244 小时前
Go语言即时通讯系统 开发日志day1
开发语言·后端·golang
有梦想的攻城狮4 小时前
spring中的@Lazy注解详解
java·后端·spring