Skip to content

Commit

Permalink
[feat]add jdbc date type support, include DATE、TIME、 DATETIME、 TIMEST…
Browse files Browse the repository at this point in the history
…AMP and YEAR
  • Loading branch information
MuLeiSY2021 committed Jan 3, 2025
1 parent 4b89c5d commit 8a6d4c7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@

package org.apache.hugegraph.loader.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import org.apache.hugegraph.loader.constant.Constants;
import org.apache.hugegraph.loader.source.AbstractSource;
import org.apache.hugegraph.loader.source.InputSource;
Expand All @@ -38,8 +32,7 @@
import org.apache.hugegraph.util.InsertionOrderUtil;
import org.apache.hugegraph.util.ReflectionUtil;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import java.util.*;

public final class DataTypeUtil {

Expand Down Expand Up @@ -138,8 +131,8 @@ private static Object parseSingleValue(String key, Object rawValue,
return parseBoolean(key, value);
} else if (dataType.isDate()) {
E.checkState(source instanceof FileSource,
"Only accept FileSource when convert String value " +
"to Date, but got '%s'", source.getClass().getName());
"Only accept FileSource when convert String value " +
"to Date, but got '%s'", source.getClass().getName());
String dateFormat = ((FileSource) source).dateFormat();
String timeZone = ((FileSource) source).timeZone();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

package org.apache.hugegraph.loader.test.functional;

import org.apache.hugegraph.loader.exception.LoadException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.hugegraph.loader.exception.LoadException;

public class DBUtil {

private final String driver;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void connect() {
public void connect(String database) {
this.close();
String url = String.format("%s/%s?%s", this.url, database,
"useSSL=false");
"useSSL=false");
try {
Class.forName(this.driver);
this.conn = DriverManager.getConnection(url, this.user, this.pass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@

package org.apache.hugegraph.loader.test.functional;

import java.util.List;

import org.apache.hugegraph.loader.HugeGraphLoader;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import org.apache.hugegraph.structure.graph.Edge;
import org.apache.hugegraph.structure.graph.Vertex;

import org.apache.hugegraph.testutil.Assert;
import org.junit.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
* TODO: add more test cases
Expand Down Expand Up @@ -72,6 +71,17 @@ public static void setUp() {
"`price` double(10,2) NOT NULL," +
"PRIMARY KEY (`id`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
// vertex date
dbUtil.execute("CREATE TABLE IF NOT EXISTS `date_test` (" +
"`id` int(10) unsigned NOT NULL," +
"`calendar_date` DATE NOT NULL," +
"`calendar_datetime` DATETIME NOT NULL," +
"`calendar_timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
"`calendar_time` TIME NOT NULL," +
"`calendar_year` YEAR NOT NULL," +
"PRIMARY KEY (`id`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;");

// edge knows
dbUtil.execute("CREATE TABLE IF NOT EXISTS `knows` (" +
"`id` int(10) unsigned NOT NULL," +
Expand All @@ -90,6 +100,8 @@ public static void setUp() {
"`weight` double(10,2) NOT NULL," +
"PRIMARY KEY (`id`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;");


}

@AfterClass
Expand All @@ -99,6 +111,7 @@ public static void tearDown() {
dbUtil.execute("DROP TABLE IF EXISTS `software`");
dbUtil.execute("DROP TABLE IF EXISTS `knows`");
dbUtil.execute("DROP TABLE IF EXISTS `created`");
dbUtil.execute("DROP TABLE IF EXISTS `date_test`");
// drop database
dbUtil.execute(String.format("DROP DATABASE `%s`", DATABASE));

Expand All @@ -117,6 +130,7 @@ public void clear() {
dbUtil.execute("TRUNCATE TABLE `software`");
dbUtil.execute("TRUNCATE TABLE `knows`");
dbUtil.execute("TRUNCATE TABLE `created`");
dbUtil.execute("TRUNCATE TABLE `date_test`");
}

@Test
Expand Down Expand Up @@ -243,4 +257,55 @@ public void testNumberToStringInJDBCSource() {
assertContains(vertices, "person", "age", "29");
assertContains(vertices, "software", "price", "199.67");
}

@Test
public void testJdbcSqlDateConvert() {
dbUtil.execute("INSERT INTO `date_test` VALUES " +
"(1, '2017-12-10', '2017-12-10 15:30:45', '2017-12-10 15:30:45', '15:30:45', '2017')," +
"(2, '2009-11-11', '2009-11-11 08:15:30', '2009-11-11 08:15:30', '08:15:30', '2009')," +
"(3, '2017-03-24', '2017-03-24 12:00:00', '2017-03-24 12:00:00', '12:00:00', '2017');");

String[] args = new String[]{
"-f", configPath("jdbc_sql_date_convert/struct.json"),
"-s", configPath("jdbc_sql_date_convert/schema.groovy"),
"-g", GRAPH,
"-h", SERVER,
"-p", String.valueOf(PORT),
"--batch-insert-threads", "2",
"--test-mode", "true"
};
HugeGraphLoader.main(args);

List<Vertex> vertices = CLIENT.graph().listVertices();

Assert.assertEquals(3, vertices.size());
// Define formatters
DateTimeFormatter serverDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");


// DATE check
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime date = LocalDate.parse("2017-12-10", dateFormatter).atStartOfDay();

DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime datetime = LocalDateTime.parse("2017-12-10 15:30:45", datetimeFormatter);

DateTimeFormatter timestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime timestamp = LocalDateTime.parse("2017-12-10 15:30:45", timestampFormatter);

DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime time = LocalTime.parse("15:30:45", timeFormatter);
LocalDateTime timeWithDate = time.atDate(LocalDate.of(1970, 1, 1)); // 补充日期为 Epoch 起点

DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("yyyy");
Year year = Year.parse("2017", yearFormatter);
LocalDateTime yearStart = year.atDay(1).atStartOfDay(); // 补充日期为该年的第一天

assertContains(vertices, "date_test",
"calendar_date", date.format(serverDateFormatter),
"calendar_datetime", datetime.format(serverDateFormatter),
"calendar_timestamp", timestamp.format(serverDateFormatter),
"calendar_time", timeWithDate.format(serverDateFormatter),
"calendar_year", yearStart.format(serverDateFormatter));
}
}

0 comments on commit 8a6d4c7

Please sign in to comment.