您的位置:首页 > 游戏 > 手游 > Spring Boot集成tablesaw插件快速入门

Spring Boot集成tablesaw插件快速入门

2024/10/6 16:23:26 来源:https://blog.csdn.net/qq_37284798/article/details/139644035  浏览:    关键词:Spring Boot集成tablesaw插件快速入门

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环境下可执行:

docker run --name docker-mysql-5.7 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7

2.2 数据库表初始化


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 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

server:port: 8088
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Drivertype: com.zaxxer.hikari.HikariDataSourceurl: jdbc:mysql://127.0.0.1:3305/springboot_demo?useUnicode=true&characterEncoding=utf-8&useSSL=falseusername: rootpassword: 123456

3.4 读取csv数据

    @Beforepublic 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 打印列名

    @Testpublic void columnNames() {System.out.println(tornadoes.columnNames());}

运行效果:

在这里插入图片描述

3.6 查看shape

    @Testpublic void shape() {System.out.println(tornadoes.shape());}

运行效果:

在这里插入图片描述

3.7 查看表结构

    @Testpublic void structure() {System.out.println(tornadoes.structure().printAll());}

运行效果:

在这里插入图片描述

3.8 查看数据

    @Testpublic void show() {System.out.println(tornadoes);}

运行效果:

在这里插入图片描述

3.9 表结构过滤

    @Testpublic void structurefilter() {System.out.println( tornadoes.structure().where(tornadoes.structure().stringColumn("Column Type").isEqualTo("DOUBLE")));}

运行效果:

在这里插入图片描述

3.10 预览数据

	@Testpublic void previewdata() {System.out.println(tornadoes.first(3));}

运行效果:

在这里插入图片描述

3.11 列操作

    @Testpublic 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 排序

    @Testpublic void sort() {tornadoes.sortOn("-Fatalities");System.out.println(tornadoes.first(20));}

运行效果:

在这里插入图片描述

3.13 summary

    @Testpublic void summary() {System.out.println( tornadoes.column("Fatalities").summary().print());}

运行效果:

在这里插入图片描述

3.14 数据过滤

    @Testpublic 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 milesresult = result.select("State", "Date");System.out.println(result);}

运行效果:

在这里插入图片描述

3.15 写入文件

    @Testpublic void write() {tornadoes.write().csv("rev_tornadoes_1950-2014-test.csv");}

3.16 从mysql读取数据

    @Resourceprivate JdbcTemplate jdbcTemplate;@Testpublic 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 数据可视化

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com