[JAVA-5353]: working with tablesaw (#12866)
Co-authored-by: Harpal Singh <harpal.singh@kaleyra.com>
This commit is contained in:
parent
ee19bc0f34
commit
e14ea6632f
4
pom.xml
4
pom.xml
|
@ -456,7 +456,6 @@
|
|||
<module>logging-modules</module>
|
||||
<module>lombok-modules</module>
|
||||
<module>lucene</module>
|
||||
|
||||
<module>mapstruct</module>
|
||||
|
||||
<module>maven-modules</module>
|
||||
|
@ -861,7 +860,6 @@
|
|||
<module>logging-modules</module>
|
||||
<module>lombok-modules</module>
|
||||
<module>lucene</module>
|
||||
|
||||
<module>mapstruct</module>
|
||||
|
||||
<module>maven-modules</module>
|
||||
|
@ -1208,6 +1206,7 @@
|
|||
<module>testing-modules/testing-assertions</module>
|
||||
<module>persistence-modules/fauna</module>
|
||||
<module>lightrun</module>
|
||||
<module>tablesaw</module>
|
||||
</modules>
|
||||
</profile>
|
||||
|
||||
|
@ -1285,6 +1284,7 @@
|
|||
<module>persistence-modules/fauna</module>
|
||||
<module>lightrun</module>
|
||||
<module>spring-core-6</module>
|
||||
<module>tablesaw</module>
|
||||
</modules>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
This module contains tutorials related to the tablesaw java library.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>tablesaw</artifactId>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- TABLESAW -->
|
||||
<dependency>
|
||||
<groupId>tech.tablesaw</groupId>
|
||||
<artifactId>tablesaw-core</artifactId>
|
||||
<version>${tablesaw.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<tablesaw.version>0.43.1</tablesaw.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,146 @@
|
|||
package com.baeldung.tablesaw;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import tech.tablesaw.api.DateColumn;
|
||||
import tech.tablesaw.api.DoubleColumn;
|
||||
import tech.tablesaw.api.StringColumn;
|
||||
import tech.tablesaw.api.Table;
|
||||
import tech.tablesaw.api.TimeColumn;
|
||||
import tech.tablesaw.io.csv.CsvReadOptions;
|
||||
import tech.tablesaw.selection.Selection;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatNoException;
|
||||
import static tech.tablesaw.aggregate.AggregateFunctions.max;
|
||||
import static tech.tablesaw.aggregate.AggregateFunctions.mean;
|
||||
import static tech.tablesaw.aggregate.AggregateFunctions.min;
|
||||
import static tech.tablesaw.aggregate.AggregateFunctions.stdDev;
|
||||
|
||||
class CSVUnitTest {
|
||||
|
||||
private static Table table;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
URL resource = CSVUnitTest.class.getClassLoader().getResource("avocado.csv");
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
Path filePath = Paths.get(resource.getPath());
|
||||
File file = filePath.toFile();
|
||||
CsvReadOptions csvReadOptions =
|
||||
CsvReadOptions.builder(file)
|
||||
.separator(',')
|
||||
.header(true)
|
||||
.dateFormat(formatter)
|
||||
.build();
|
||||
table = Table.read().usingOptions(csvReadOptions);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnTheShapeStringOnAvocadoDataSet() {
|
||||
assertThat(table.shape()).isEqualTo("avocado.csv: 18249 rows X 14 cols");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldOrderTheTableByDateInAscendingOrder() {
|
||||
Table ascendingDateSortedTable = table.sortAscendingOn("Date");
|
||||
assertThat(ascendingDateSortedTable.dateColumn("Date").get(0)).isEqualTo(LocalDate.parse("2015-01-04"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldOrderTheTableByDateInDescendingOrder() {
|
||||
Table descendingDateSortedTable = table.sortDescendingOn("Date");
|
||||
assertThat(descendingDateSortedTable.dateColumn("Date").get(0)).isEqualTo(LocalDate.parse("2018-03-25"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldOrderTheTableByYearAndAveragePriceInAscendingOrder() {
|
||||
Table ascendingYearAndAveragePriceSortedTable = table.sortOn("year", "-AveragePrice");
|
||||
assertThat(ascendingYearAndAveragePriceSortedTable.intColumn("year").get(0)).isEqualTo(2015);
|
||||
assertThat(ascendingYearAndAveragePriceSortedTable.numberColumn("AveragePrice").get(0)).isEqualTo(2.79);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRemoveTheValueWhenSettingAsMissing() {
|
||||
DoubleColumn averagePrice = table.doubleColumn("AveragePrice").setMissing(0);
|
||||
assertThat(averagePrice.get(0)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAppendDataToTheColumn() {
|
||||
DoubleColumn averagePrice = table.doubleColumn("AveragePrice");
|
||||
averagePrice.append(1.123);
|
||||
assertThat(averagePrice.get(averagePrice.size() - 1)).isEqualTo(1.123);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAppendDataToAveragePriceColumn() {
|
||||
DoubleColumn averagePrice2 = table.doubleColumn("AveragePrice").copy();
|
||||
averagePrice2.setName("AveragePrice2");
|
||||
averagePrice2.append(1.123);
|
||||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> table.addColumns(averagePrice2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnTheTableContainingDataFrom2017andAveragePriceIsGreaterThan2Only() {
|
||||
DateColumn dateTable = table.dateColumn("Date");
|
||||
DoubleColumn averagePrice = table.doubleColumn("AveragePrice");
|
||||
Selection selection = dateTable.isInYear(2017).and(averagePrice.isGreaterThan(2D));
|
||||
Table table2017 = table.where(selection);
|
||||
assertThat(table2017.intColumn("year")).containsOnly(2017);
|
||||
assertThat(table2017.doubleColumn("AveragePrice")).allMatch(avrgPrice -> avrgPrice > 2D);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPrintToStandardOutputStatisticsOfAveragePriceByYearData() {
|
||||
Table summary = table.summarize("AveragePrice", max, min, mean, stdDev).by("year");
|
||||
System.out.println(summary.print());
|
||||
Assertions.assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowIllegalArgumentExceptionWhenCreatingTableWithDifferentSizeColumns() {
|
||||
StringColumn type = StringColumn.create("type");
|
||||
StringColumn region = StringColumn.create("region");
|
||||
type.addAll(List.of("Country", "City"));
|
||||
region.append("USA");
|
||||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> Table.create(type, region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotThrowIllegalArgumentExceptionWhenCreatingTableWithDifferentSameSizeColumns() {
|
||||
StringColumn type = StringColumn.create("type");
|
||||
StringColumn region = StringColumn.create("region");
|
||||
type.addAll(List.of("Country", "City"));
|
||||
region.append("USA");
|
||||
region.appendMissing();
|
||||
assertThatNoException().isThrownBy(() -> Table.create(type, region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddColumnToTable() {
|
||||
TimeColumn time = TimeColumn.create("time");
|
||||
Table table = Table.create("test");
|
||||
table.addColumns(time);
|
||||
assertThat(table.columnNames()).contains("time");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldBeEqualTwoValuesFromDifferentRowsOnTheTypeColumn() {
|
||||
StringColumn type = table.stringColumn("type");
|
||||
List<String> conventional = type.where(type.isEqualTo("conventional")).asList().stream()
|
||||
.limit(2)
|
||||
.toList();
|
||||
assertThat(conventional.get(0)).isSameAs(conventional.get(1));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue