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

相关推荐
小码哥_常30 分钟前
别再被误导!try...catch性能大揭秘
后端
苍何3 小时前
30分钟用 Agent 搓出一家跨境网店,疯了
后端
ssshooter3 小时前
Tauri 2 iOS 开发避坑指南:文件保存、Dialog 和 Documents 目录的那些坑
前端·后端·ios
追逐时光者3 小时前
一个基于 .NET Core + Vue3 构建的开源全栈平台 Admin 系统
后端·.net
程序员飞哥3 小时前
90后大龄程序员失业4个月终于上岸了
后端·面试·程序员
彭于晏Yan5 小时前
Redisson分布式锁
spring boot·redis·分布式
GetcharZp5 小时前
Git 命令行太痛苦?这款 75k Star 的神级工具,让你告别“合并冲突”恐惧症!
后端
Victor3566 小时前
MongoDB(69)如何进行增量备份?
后端
Victor3566 小时前
MongoDB(70)如何使用副本集进行备份?
后端
千寻girling6 小时前
面试官 : “ 说一下 Python 中的常用的 字符串和数组 的 方法有哪些 ? ”
人工智能·后端·python