当前位置:   article > 正文

Spring Boot集成tablesaw插件快速入门Demo_springboot 继承 tablesaw

springboot 继承 tablesaw

1.什么是tablesaw?

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

  • 数据解析库,主要用于加载数据,对数据进行操作(转化,过滤,汇总等),类比Python中的Pandas库;
  • 数据可视化库,将目标数据转化为可视化的图表,类比Python中的Matplotlib库。

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

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.环境准备

mysql setup

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

init data

  1. create database demo;
  2. create table user_info
  3. (
  4. user_id varchar(64) not null primary key,
  5. username varchar(100) null ,
  6. age int(3) null ,
  7. gender tinyint(1) null ,
  8. remark varchar(255) null ,
  9. create_time datetime null ,
  10. create_id varchar(64) null ,
  11. update_time datetime null ,
  12. update_id varchar(64) null ,
  13. enabled tinyint(1) default 1 null
  14. );
  15. INSERT INTO demo.user_info
  16. (user_id, username, age, gender, remark, create_time, create_id, update_time, update_id, enabled)
  17. VALUES('1', '1', 1, 1, '1', NULL, '1', NULL, NULL, 1);

remark

  1. msyql username:root
  2. mysql password:123456

3.代码工程

实验目的:

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

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springboot-demo</artifactId>
  7. <groupId>com.et</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>tablesaw</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-autoconfigure</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>tech.tablesaw</groupId>
  32. <artifactId>tablesaw-core</artifactId>
  33. <version>0.43.1</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>tech.tablesaw</groupId>
  37. <artifactId>tablesaw-jsplot</artifactId>
  38. <version>0.43.1</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-jdbc</artifactId>
  43. </dependency>
  44. <!--mysql-->
  45. <dependency>
  46. <groupId>mysql</groupId>
  47. <artifactId>mysql-connector-java</artifactId>
  48. <version>8.0.29</version>
  49. </dependency>
  50. </dependencies>
  51. </project>

application.yaml

  1. server:
  2. port: 8088
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. type: com.zaxxer.hikari.HikariDataSource
  7. url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
  8. username: root
  9. password: 123456

读取csv数据

  1. @Before
  2. public void before() throws IOException {
  3. log.info("init some data");
  4. tornadoes = Table.read().csv("/Users/liuhaihua/IdeaProjects/springboot-demo/tablesaw/src/main/resources/data/tornadoes_1950-2014.csv");
  5. }

打印列名

  1. @Test
  2. public void columnNames() throws IOException {
  3. System.out.println(tornadoes.columnNames());
  4. }

结果

[Date, Time, State, State No, Scale, Injuries, Fatalities, Start Lat, Start Lon, Length, Width]

查看shape

  1. @Test
  2. public void shape() throws IOException {
  3. System.out.println(tornadoes.shape());
  4. }

结果

tornadoes_1950-2014.csv: 59945 rows X 11 cols

查看表结构

  1. @Test
  2. public void structure() throws IOException {
  3. System.out.println(tornadoes.structure().printAll());
  4. }

结果

  1. 2024-06-05 21:13:01.297 INFO 41685 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. Structure of tornadoes_1950-2014.csv
  3. Index | Column Name | Column Type |
  4. -----------------------------------------
  5. 0 | Date | LOCAL_DATE |
  6. 1 | Time | LOCAL_TIME |
  7. 2 | State | STRING |
  8. 3 | State No | INTEGER |
  9. 4 | Scale | INTEGER |
  10. 5 | Injuries | INTEGER |
  11. 6 | Fatalities | INTEGER |
  12. 7 | Start Lat | DOUBLE |
  13. 8 | Start Lon | DOUBLE |
  14. 9 | Length | DOUBLE |
  15. 10 | Width | INTEGER |
  16. 2024-06-05 21:13:02.907 INFO 41685 --- [ main] com.et.tablesaw.DemoTests : clean some data

查看数据

  1. @Test
  2. public void show() throws IOException {
  3. System.out.println(tornadoes);
  4. }

结果

  1. 2024-06-05 21:14:52.181 INFO 41732 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. tornadoes_1950-2014.csv
  3. Date | Time | State | State No | Scale | Injuries | Fatalities | Start Lat | Start Lon | Length | Width |
  4. -----------------------------------------------------------------------------------------------------------------------------------------
  5. 1950-01-03 | 11:00:00 | MO | 1 | 3 | 3 | 0 | 38.77 | -90.22 | 9.5 | 150 |
  6. 1950-01-03 | 11:00:00 | MO | 1 | 3 | 3 | 0 | 38.77 | -90.22 | 6.2 | 150 |
  7. 1950-01-03 | 11:10:00 | IL | 1 | 3 | 0 | 0 | 38.82 | -90.12 | 3.3 | 100 |
  8. 1950-01-03 | 11:55:00 | IL | 2 | 3 | 3 | 0 | 39.1 | -89.3 | 3.6 | 130 |
  9. 1950-01-03 | 16:00:00 | OH | 1 | 1 | 1 | 0 | 40.88 | -84.58 | 0.1 | 10 |
  10. 1950-01-13 | 05:25:00 | AR | 1 | 3 | 1 | 1 | 34.4 | -94.37 | 0.6 | 17 |
  11. 1950-01-25 | 19:30:00 | MO | 2 | 2 | 5 | 0 | 37.6 | -90.68 | 2.3 | 300 |
  12. 1950-01-25 | 21:00:00 | IL | 3 | 2 | 0 | 0 | 41.17 | -87.33 | 0.1 | 100 |
  13. 1950-01-26 | 18:00:00 | TX | 1 | 2 | 2 | 0 | 26.88 | -98.12 | 4.7 | 133 |
  14. 1950-02-11 | 13:10:00 | TX | 2 | 2 | 0 | 0 | 29.42 | -95.25 | 9.9 | 400 |
  15. ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
  16. 2014-12-23 | 14:46:00 | MS | 0 | 0 | 0 | 0 | 31.4247 | -89.5358 | 0.24 | 50 |
  17. 2014-12-23 | 15:22:00 | MS | 0 | 2 | 0 | 2 | 31.6986 | -89.2239 | 5.89 | 350 |
  18. 2014-12-23 | 16:27:00 | MS | 0 | 0 | 0 | 0 | 32.0005 | -88.4854 | 0.49 | 100 |
  19. 2014-12-24 | 09:30:00 | NC | 0 | 1 | 1 | 0 | 34.3124 | -77.917 | 0.2 | 25 |
  20. 2014-12-24 | 11:50:00 | GA | 0 | 1 | 0 | 0 | 31.406 | -82.251 | 5.69 | 100 |
  21. 2014-12-24 | 16:16:00 | OH | 0 | 0 | 0 | 0 | 39.728 | -82.634 | 0.63 | 30 |
  22. 2014-12-27 | 13:12:00 | TX | 0 | 0 | 0 | 0 | 30.3853 | -94.7823 | 0.23 | 75 |
  23. 2014-12-27 | 14:00:00 | TX | 0 | 1 | 0 | 0 | 30.6829 | -94.2553 | 4.25 | 50 |
  24. 2014-12-27 | 14:35:00 | TX | 0 | 1 | 0 | 0 | 30.864 | -93.979 | 1.98 | 150 |
  25. 2014-12-29 | 10:26:00 | GA | 0 | 2 | 9 | 0 | 30.8157 | -83.2833 | 0.74 | 180 |
  26. 2024-06-05 21:14:54.430 INFO 41732 --- [ main] com.et.tablesaw.DemoTests : clean some data

表结构过滤

  1. @Test
  2. public void structurefilter() throws IOException {
  3. System.out.println( tornadoes
  4. .structure()
  5. .where(tornadoes.structure().stringColumn("Column Type").isEqualTo("DOUBLE")));
  6. }

结果

  1. 2024-06-05 21:16:12.489 INFO 41766 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. Structure of tornadoes_1950-2014.csv
  3. Index | Column Name | Column Type |
  4. -----------------------------------------
  5. 7 | Start Lat | DOUBLE |
  6. 8 | Start Lon | DOUBLE |
  7. 9 | Length | DOUBLE |
  8. 2024-06-05 21:16:14.104 INFO 41766 --- [ main] com.et.tablesaw.DemoTests : clean some data

预览数据

  1. @Test
  2. public void previewdata() throws IOException {
  3. System.out.println(tornadoes.first(3));
  4. }

结果

  1. 2024-06-05 21:17:21.268 INFO 41798 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. tornadoes_1950-2014.csv
  3. Date | Time | State | State No | Scale | Injuries | Fatalities | Start Lat | Start Lon | Length | Width |
  4. -----------------------------------------------------------------------------------------------------------------------------------------
  5. 1950-01-03 | 11:00:00 | MO | 1 | 3 | 3 | 0 | 38.77 | -90.22 | 9.5 | 150 |
  6. 1950-01-03 | 11:00:00 | MO | 1 | 3 | 3 | 0 | 38.77 | -90.22 | 6.2 | 150 |
  7. 1950-01-03 | 11:10:00 | IL | 1 | 3 | 0 | 0 | 38.82 | -90.12 | 3.3 | 100 |
  8. 2024-06-05 21:17:23.206 INFO 41798 --- [ main] com.et.tablesaw.DemoTests : clean some data

列操作

  1. @Test
  2. public void ColumnOperate() throws IOException {
  3. StringColumn month = tornadoes.dateColumn("Date").month();
  4. tornadoes.addColumns(month);
  5. System.out.println(tornadoes.first(3));
  6. tornadoes.removeColumns("State No");
  7. System.out.println(tornadoes.first(3));
  8. }

结果

  1. 2024-06-05 21:20:33.554 INFO 41879 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. tornadoes_1950-2014.csv
  3. Date | Time | State | State No | Scale | Injuries | Fatalities | Start Lat | Start Lon | Length | Width | Date month |
  4. --------------------------------------------------------------------------------------------------------------------------------------------------------
  5. 1950-01-03 | 11:00:00 | MO | 1 | 3 | 3 | 0 | 38.77 | -90.22 | 9.5 | 150 | JANUARY |
  6. 1950-01-03 | 11:00:00 | MO | 1 | 3 | 3 | 0 | 38.77 | -90.22 | 6.2 | 150 | JANUARY |
  7. 1950-01-03 | 11:10:00 | IL | 1 | 3 | 0 | 0 | 38.82 | -90.12 | 3.3 | 100 | JANUARY |
  8. tornadoes_1950-2014.csv
  9. Date | Time | State | Scale | Injuries | Fatalities | Start Lat | Start Lon | Length | Width | Date month |
  10. -------------------------------------------------------------------------------------------------------------------------------------------
  11. 1950-01-03 | 11:00:00 | MO | 3 | 3 | 0 | 38.77 | -90.22 | 9.5 | 150 | JANUARY |
  12. 1950-01-03 | 11:00:00 | MO | 3 | 3 | 0 | 38.77 | -90.22 | 6.2 | 150 | JANUARY |
  13. 1950-01-03 | 11:10:00 | IL | 3 | 0 | 0 | 38.82 | -90.12 | 3.3 | 100 | JANUARY |
  14. 2024-06-05 21:20:37.052 INFO 41879 --- [ main] com.et.tablesaw.DemoTests : clean some data

排序

  1. @Test
  2. public void sort() throws IOException {
  3. tornadoes.sortOn("-Fatalities");
  4. System.out.println(tornadoes.first(20));
  5. }

结果

  1. 2024-06-05 21:23:22.978 INFO 41945 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. tornadoes_1950-2014.csv
  3. Date | Time | State | State No | Scale | Injuries | Fatalities | Start Lat | Start Lon | Length | Width |
  4. -----------------------------------------------------------------------------------------------------------------------------------------
  5. 1950-01-03 | 11:00:00 | MO | 1 | 3 | 3 | 0 | 38.77 | -90.22 | 9.5 | 150 |
  6. 1950-01-03 | 11:00:00 | MO | 1 | 3 | 3 | 0 | 38.77 | -90.22 | 6.2 | 150 |
  7. 1950-01-03 | 11:10:00 | IL | 1 | 3 | 0 | 0 | 38.82 | -90.12 | 3.3 | 100 |
  8. 1950-01-03 | 11:55:00 | IL | 2 | 3 | 3 | 0 | 39.1 | -89.3 | 3.6 | 130 |
  9. 1950-01-03 | 16:00:00 | OH | 1 | 1 | 1 | 0 | 40.88 | -84.58 | 0.1 | 10 |
  10. 1950-01-13 | 05:25:00 | AR | 1 | 3 | 1 | 1 | 34.4 | -94.37 | 0.6 | 17 |
  11. 1950-01-25 | 19:30:00 | MO | 2 | 2 | 5 | 0 | 37.6 | -90.68 | 2.3 | 300 |
  12. 1950-01-25 | 21:00:00 | IL | 3 | 2 | 0 | 0 | 41.17 | -87.33 | 0.1 | 100 |
  13. 1950-01-26 | 18:00:00 | TX | 1 | 2 | 2 | 0 | 26.88 | -98.12 | 4.7 | 133 |
  14. 1950-02-11 | 13:10:00 | TX | 2 | 2 | 0 | 0 | 29.42 | -95.25 | 9.9 | 400 |
  15. 1950-02-11 | 13:50:00 | TX | 3 | 3 | 12 | 1 | 29.67 | -95.05 | 12 | 1000 |
  16. 1950-02-11 | 21:00:00 | TX | 4 | 2 | 5 | 0 | 32.35 | -95.2 | 4.6 | 100 |
  17. 1950-02-11 | 23:55:00 | TX | 5 | 2 | 6 | 0 | 32.98 | -94.63 | 4.5 | 67 |
  18. 1950-02-12 | 00:30:00 | TX | 6 | 2 | 8 | 1 | 33.33 | -94.42 | 8 | 833 |
  19. 1950-02-12 | 01:15:00 | TX | 7 | 1 | 0 | 0 | 32.08 | -98.35 | 2.3 | 233 |
  20. 1950-02-12 | 06:10:00 | TX | 8 | 2 | 0 | 0 | 31.52 | -96.55 | 3.4 | 100 |
  21. 1950-02-12 | 11:57:00 | TX | 9 | 1 | 32 | 0 | 31.8 | -94.2 | 7.7 | 100 |
  22. 1950-02-12 | 12:00:00 | TX | 10 | 3 | 15 | 3 | 31.8 | -94.2 | 1.9 | 50 |
  23. 1950-02-12 | 12:00:00 | MS | 2 | 1 | 0 | 0 | 34.6 | -89.12 | 2 | 10 |
  24. 1950-02-12 | 12:00:00 | MS | 1 | 2 | 2 | 3 | 34.6 | -89.12 | 0.1 | 10 |
  25. 2024-06-05 21:23:24.864 INFO 41945 --- [ main] com.et.tablesaw.DemoTests : clean some data

summary

  1. @Test
  2. public void summary() throws IOException {
  3. System.out.println( tornadoes.column("Fatalities").summary().print());
  4. }

结果

  1. 2024-06-05 21:24:17.166 INFO 41963 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. Column: Fatalities
  3. Measure | Value |
  4. ------------------------------------
  5. Count | 59945 |
  6. sum | 6802 |
  7. Mean | 0.11347068145800349 |
  8. Min | 0 |
  9. Max | 158 |
  10. Range | 158 |
  11. Variance | 2.901978053261765 |
  12. Std. Dev | 1.7035193140266314 |
  13. 2024-06-05 21:24:19.131 INFO 41963 --- [ main] com.et.tablesaw.DemoTests : clean some data

数据过滤

  1. @Test
  2. public void filter() throws IOException {
  3. Table result = tornadoes.where(tornadoes.intColumn("Fatalities").isGreaterThan(0));
  4. result = tornadoes.where(result.dateColumn("Date").isInApril());
  5. result =
  6. tornadoes.where(
  7. result
  8. .intColumn("Width")
  9. .isGreaterThan(300) // 300 yards
  10. .or(result.doubleColumn("Length").isGreaterThan(10))); // 10 miles
  11. result = result.select("State", "Date");
  12. System.out.println(result);
  13. }

结果

  1. 2024-06-05 21:25:03.671 INFO 41980 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. tornadoes_1950-2014.csv
  3. State | Date |
  4. ------------------------
  5. MO | 1950-01-03 |
  6. IL | 1950-01-03 |
  7. OH | 1950-01-03 |
  8. AR | 1950-01-13 |
  9. MO | 1950-01-25 |
  10. IL | 1950-01-25 |
  11. TX | 1950-02-12 |
  12. TX | 1950-02-12 |
  13. LA | 1950-02-12 |
  14. AR | 1950-02-12 |
  15. ... | ... |
  16. KS | 1951-06-21 |
  17. OK | 1951-06-21 |
  18. KS | 1951-06-23 |
  19. WV | 1951-06-26 |
  20. KS | 1951-06-27 |
  21. IL | 1951-06-27 |
  22. PA | 1951-06-27 |
  23. SD | 1951-07-20 |
  24. KS | 1951-08-06 |
  25. NC | 1951-08-09 |
  26. 2024-06-05 21:25:05.764 INFO 41980 --- [ main] com.et.tablesaw.DemoTests : clean some data

写入文件

  1. @Test
  2. public void write() throws IOException {
  3. tornadoes.write().csv("rev_tornadoes_1950-2014-test.csv");
  4. }

从mysql读取数据

  1. @Resource
  2. private JdbcTemplate jdbcTemplate;
  3. @Test
  4. public void datafrommysql() throws IOException {
  5. Table table = jdbcTemplate.query("SELECT user_id,username,age from user_info", new ResultSetExtractor<Table>() {
  6. @Override
  7. public Table extractData(ResultSet resultSet) throws SQLException, DataAccessException {
  8. return Table.read().db(resultSet);
  9. }
  10. });
  11. System.out.println(table);
  12. }

结果

  1. 2024-06-05 21:26:04.963 INFO 42016 --- [ main] com.et.tablesaw.DemoTests : init some data
  2. 2024-06-05 21:26:06.622 INFO 42016 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  3. 2024-06-05 21:26:06.949 INFO 42016 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
  4. user_id | username | age |
  5. --------------------------------
  6. 1 | 1 | 1 |
  7. 2024-06-05 21:26:07.037 INFO 42016 --- [ main] com.et.tablesaw.DemoTests : clean some data

数据可视化

  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package com.et.tablesaw;
  15. import java.io.IOException;
  16. import tech.tablesaw.api.Table;
  17. import tech.tablesaw.plotly.Plot;
  18. import tech.tablesaw.plotly.api.BubblePlot;
  19. import tech.tablesaw.plotly.components.Figure;
  20. /** */
  21. public class BubbleExample2 {
  22. public static void main(String[] args) throws IOException {
  23. Table wines = Table.read().csv("/Users/liuhaihua/IdeaProjects/springboot-demo/tablesaw/src/main/resources/data/test_wines.csv");
  24. Table champagne =
  25. wines.where(
  26. wines
  27. .stringColumn("wine type")
  28. .isEqualTo("Champagne & Sparkling")
  29. .and(wines.stringColumn("region").isEqualTo("California")));
  30. Figure figure =
  31. BubblePlot.create(
  32. "Average retail price for champagnes by year and rating",
  33. champagne, // table namex
  34. "highest pro score", // x variable column name
  35. "year", // y variable column name
  36. "Mean Retail" // bubble size
  37. );
  38. Plot.show(figure);
  39. }
  40. }

结果如下图

11

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

4.引用

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/737329
推荐阅读
相关标签
  

闽ICP备14008679号