diff --git a/.gitignore b/.gitignore
index ffd1490..37385a1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,3 +21,5 @@
*.iws
*.iml
*.ipr
+
+*.json
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..f488b42
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,69 @@
+
+
+ 4.0.0
+
+ com.htfp
+ weather
+ 0.0.1
+ pom
+
+ weather app
+ weather service prepublish
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.6.13
+
+
+
+
+ UTF-8
+ UTF-8
+ 1.8
+
+
+
+
+ tablestore-grid-master
+ weather-service
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+ 1.8
+ 1.8
+ UTF-8
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.19.1
+
+ false
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tablestore-grid-master/pom.xml b/tablestore-grid-master/pom.xml
index 8963410..5517961 100644
--- a/tablestore-grid-master/pom.xml
+++ b/tablestore-grid-master/pom.xml
@@ -7,6 +7,20 @@
com.aliyun.tablestore
tablestore-grid
1.1-SNAPSHOT
+ jar
+
+
+ com.htfp
+ weather
+ 0.0.1
+ ../pom.xml
+
+
+
+ UTF-8
+ UTF-8
+ 1.8
+
@@ -113,10 +127,15 @@
org.apache.maven.plugins
maven-compiler-plugin
- 8
- 8
+ 1.8
+ 1.8
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
\ No newline at end of file
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/ExampleConfig.java b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/ExampleConfig.java
deleted file mode 100644
index 6b5eb52..0000000
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/ExampleConfig.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package com.aliyun.tablestore.example.grid.common;
-
-import ucar.ma2.DataType;
-
-
-public class ExampleConfig {
-
- /**
- * 表名和index名
- */
- public static String GRID_DATA_TABLE_NAME = "GRID_DATA_TABLE_EXAMPLE1";
- public static String GRID_META_TABLE_NAME = "GRID_META_TABLE_EXAMPLE1";
- public static String GRID_META_INDEX_NAME = "GRID_META_INDEX";
-
-
- /**
- * 数据属性:ID、本地数据文件名、目标变量名、变量维度大小、变量类型
- */
- public static String EXAMPLE_GRID_DATA_SET_ID = "test_echam_spectral_example";
- public static String EXAMPLE_GRID_DATA_SET_NC_FILE_PATH = "test_echam_spectral.nc";
- public static String EXAMPLE_GRID_DATA_VARIABLE = "tpot";
- public static int[] EXAMPLE_GRID_DATA_SHAPE = new int[]{8, 47, 96, 192};
- public static DataType EXAMPLE_GRID_DATA_TYPE = DataType.FLOAT;
-}
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/TableStoreConf.java b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/TableStoreConf.java
deleted file mode 100644
index b98964c..0000000
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/TableStoreConf.java
+++ /dev/null
@@ -1,62 +0,0 @@
-package com.aliyun.tablestore.example.grid.common;
-
-import com.google.gson.Gson;
-import org.apache.commons.io.IOUtils;
-
-import java.io.FileInputStream;
-import java.io.InputStream;
-
-
-
-public class TableStoreConf {
-
- private String endpoint;
- private String accessId;
- private String accessKey;
- private String instanceName;
-
- /**
- * 从目标配置文件中读取 数据库instance相关信息
- */
- public static TableStoreConf newInstance(String path) {
- try {
- InputStream f = new FileInputStream(path);
- Gson gson = new Gson();
- return gson.fromJson(IOUtils.toString(f), TableStoreConf.class);
- } catch (Exception ex) {
- throw new RuntimeException(ex);
- }
- }
-
- public String getEndpoint() {
- return endpoint;
- }
-
- public void setEndpoint(String endpoint) {
- this.endpoint = endpoint;
- }
-
- public String getAccessId() {
- return accessId;
- }
-
- public void setAccessId(String accessId) {
- this.accessId = accessId;
- }
-
- public String getAccessKey() {
- return accessKey;
- }
-
- public void setAccessKey(String accessKey) {
- this.accessKey = accessKey;
- }
-
- public String getInstanceName() {
- return instanceName;
- }
-
- public void setInstanceName(String instanceName) {
- this.instanceName = instanceName;
- }
-}
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/TableStoreGridExample.java b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/TableStoreGridExample.java
deleted file mode 100644
index cc03f78..0000000
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/common/TableStoreGridExample.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package com.aliyun.tablestore.example.grid.common;
-
-import com.aliyun.tablestore.grid.TableStoreGrid;
-import com.aliyun.tablestore.grid.TableStoreGridConfig;
-
-public abstract class TableStoreGridExample {
-
- protected TableStoreGrid tableStoreGrid;
- // private String pathSeperator = "/";
-
- public TableStoreGridExample(String dataTableName, String metaTableName) {
- // String os = System.getProperty("os.name");
- // if (os.toLowerCase().startsWith("win")) {
- // pathSeparator = "\\";
- // }
- String pathSeparator = System.getProperty("file.separator");
- TableStoreConf conf = TableStoreConf.newInstance(System.getProperty("user.dir") + pathSeparator + "tablestoreConf.json");
- TableStoreGridConfig config = new TableStoreGridConfig();
- config.setTableStoreEndpoint(conf.getEndpoint());
- config.setAccessId(conf.getAccessId());
- config.setAccessKey(conf.getAccessKey());
- config.setTableStoreInstance(conf.getInstanceName());
- config.setDataTableName(dataTableName);
- config.setMetaTableName(metaTableName);
- tableStoreGrid = new TableStoreGrid(config);
- }
-
- public void close() {
- if (tableStoreGrid != null) {
- tableStoreGrid.close();
- }
- }
-}
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/consts/AttributionEnum.java b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/consts/AttributionEnum.java
index 2e049d7..5add231 100644
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/consts/AttributionEnum.java
+++ b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/consts/AttributionEnum.java
@@ -1,5 +1,7 @@
package com.aliyun.tablestore.grid.consts;
+import com.alicloud.openservices.tablestore.model.search.FieldType;
+
import java.util.function.Consumer;
/**
@@ -9,14 +11,14 @@ import java.util.function.Consumer;
*/
public enum AttributionEnum {
//
- STATUS("status", "导入状态"),
- CREATE_TIME("create_time", "创建时间"),
- REFERENCE_TIME("reference_time", "起报时间"),
+ STATUS("status", "导入状态", FieldType.KEYWORD),
+ CREATE_TIME("create_time", "创建时间", FieldType.LONG),
+ REFERENCE_TIME("reference_time", "起报时间", FieldType.LONG),
;
public String name;
public String info;
-
+ public FieldType type;
public String getName() {
return name;
}
@@ -24,8 +26,13 @@ public enum AttributionEnum {
public String getInfo() {
return info;
}
- AttributionEnum(String name, String info) {
+
+ public FieldType getType() {
+ return type;
+ }
+ AttributionEnum(String name, String info, FieldType type) {
this.name = name;
this.info = info;
+ this.type = type;
}
}
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/core/TableStoreDataWriter.java b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/core/TableStoreDataWriter.java
index b795a35..74bce73 100644
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/core/TableStoreDataWriter.java
+++ b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/core/TableStoreDataWriter.java
@@ -91,6 +91,7 @@ public class TableStoreDataWriter implements GridDataWriter {
if (meta.getStoreOptions().getStoreType().equals(StoreOptions.StoreType.SLICE)) {
List columns = splitDataToColumns(grid2D);
writeToTableStore(variable, t, z, columns);
+ columns = null;
} else {
throw new IllegalArgumentException("unsupported store type");
}
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/model/grid/Grid.java b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/model/grid/Grid.java
index c4c79dd..0aeccdb 100644
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/model/grid/Grid.java
+++ b/tablestore-grid-master/src/main/java/com/aliyun/tablestore/grid/model/grid/Grid.java
@@ -52,7 +52,6 @@ public abstract class Grid {
}
public Array toArray() {
- Array array = Array.factory(dataType, shape, buffer.duplicate());
- return array;
+ return Array.factory(dataType, shape, buffer.duplicate());
}
}
diff --git a/tablestore-grid-master/src/test/java/TestReadGrib2ByNC.java b/tablestore-grid-master/src/test/java/TestReadGrib2ByNC.java
index 259bfea..6ae2ceb 100644
--- a/tablestore-grid-master/src/test/java/TestReadGrib2ByNC.java
+++ b/tablestore-grid-master/src/test/java/TestReadGrib2ByNC.java
@@ -1,4 +1,7 @@
+import com.aliyun.tablestore.grid.model.grid.Grid;
+import com.aliyun.tablestore.grid.model.grid.Grid2D;
import ucar.ma2.Array;
+import ucar.ma2.DataType;
import ucar.ma2.Index;
import ucar.ma2.InvalidRangeException;
import ucar.nc2.NetcdfFile;
@@ -6,6 +9,7 @@ import ucar.nc2.NetcdfFiles;
import ucar.nc2.Variable;
import java.io.IOException;
+import java.nio.ByteBuffer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@@ -13,7 +17,15 @@ import java.time.format.DateTimeFormatter;
* Unit test for simple App.
*/
public class TestReadGrib2ByNC {
- public static void main(String[] args){
+ public static void main(String[] args) throws InterruptedException {
+ // while (true) {
+ // ByteBuffer allocate = ByteBuffer.allocate(500 * 500 * 4);
+ // Grid2D grid = new Grid2D(allocate, DataType.FLOAT, new int[]{0, 0}, new int[]{500,500});
+ // allocate = null; grid=null;
+ // Thread.sleep(300);
+ // }
+ }
+ public static void main1(String[] args){
// String ncFile = "surface.nc";
String file = "C:\\Users\\shi_y\\Desktop\\java_learn\\data_download\\GFSData\\UTC-20230910\\BJT-20230911-0200.grib2";
try (NetcdfFile ncFile = NetcdfFiles.open(file)) {
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/CreateStoreExample.java b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/CreateStoreExample.java
similarity index 96%
rename from tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/CreateStoreExample.java
rename to tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/CreateStoreExample.java
index 7adcb37..8711734 100644
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/CreateStoreExample.java
+++ b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/CreateStoreExample.java
@@ -42,7 +42,7 @@ public class CreateStoreExample extends TableStoreGridExample {
this.tableStoreGrid.createMetaIndex(ExampleConfig.GRID_META_INDEX_NAME, indexSchema);
}
- public static void main(String[] args) throws Exception {
+ public static void test(String[] args) throws Exception {
CreateStoreExample example = new CreateStoreExample();
try {
example.createStore();
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/DataFetchExample.java b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/DataFetchExample.java
similarity index 97%
rename from tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/DataFetchExample.java
rename to tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/DataFetchExample.java
index d897470..d09351f 100644
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/DataFetchExample.java
+++ b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/DataFetchExample.java
@@ -6,7 +6,6 @@ import com.aliyun.tablestore.grid.GridDataFetcher;
import com.aliyun.tablestore.grid.model.grid.Grid4D;
import ucar.ma2.Array;
-import java.util.Arrays;
import java.util.Collections;
public class DataFetchExample extends TableStoreGridExample {
@@ -71,7 +70,7 @@ public class DataFetchExample extends TableStoreGridExample {
fetch3();
}
- public static void main(String[] args) throws Exception {
+ public static void test(String[] args) throws Exception {
DataFetchExample example = new DataFetchExample();
try {
example.run();
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/DataImportExample.java b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/DataImportExample.java
similarity index 97%
rename from tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/DataImportExample.java
rename to tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/DataImportExample.java
index ab78057..2f42f68 100644
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/DataImportExample.java
+++ b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/DataImportExample.java
@@ -12,7 +12,6 @@ import ucar.ma2.DataType;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
-import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -97,7 +96,7 @@ public class DataImportExample extends TableStoreGridExample {
updateMeta(meta);
}
- public static void main(String[] args) throws Exception {
+ public static void test(String[] args) throws Exception {
DataImportExample example = new DataImportExample();
try {
log.info("导入数据");
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/MetaDeleteExample.java b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/MetaDeleteExample.java
similarity index 98%
rename from tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/MetaDeleteExample.java
rename to tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/MetaDeleteExample.java
index 2bd5540..5652590 100644
--- a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/MetaDeleteExample.java
+++ b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/MetaDeleteExample.java
@@ -11,7 +11,6 @@ import com.aliyun.tablestore.grid.model.QueryGridDataSetResult;
import com.aliyun.tablestore.grid.model.QueryParams;
import lombok.extern.slf4j.Slf4j;
-import java.util.Arrays;
import java.util.Collections;
import java.util.List;
diff --git a/tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/MetaQueryExample.java b/tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/MetaQueryExample.java
similarity index 100%
rename from tablestore-grid-master/src/main/java/com/aliyun/tablestore/example/grid/MetaQueryExample.java
rename to tablestore-grid-master/src/test/java/com/aliyun/tablestore/example/grid/MetaQueryExample.java
diff --git a/weather-service/pom.xml b/weather-service/pom.xml
index 33e9704..9fccfee 100644
--- a/weather-service/pom.xml
+++ b/weather-service/pom.xml
@@ -2,18 +2,27 @@
4.0.0
- com.htfp.weather
+ com.htfp
weather-service
- 0.0.1-SNAPSHOT
+ 0.0.1
weather-service
weather-service
+ jar
+
+
+
+ com.htfp
+ weather
+ 0.0.1
+ ../pom.xml
+
1.8
UTF-8
UTF-8
2.6.13
-
+
unidata-all
@@ -88,11 +97,10 @@
spring-boot-starter-test
test
-
- org.springframework.boot
- spring-boot-starter-thymeleaf
-
-
+
+
+
+
com.squareup.okhttp3
okhttp
@@ -128,48 +136,34 @@
-
-
-
- org.springframework.boot
- spring-boot-dependencies
- ${spring-boot.version}
- pom
- import
-
-
-
-
+
-
- org.apache.maven.plugins
- maven-compiler-plugin
- 3.8.1
-
- 1.8
- 1.8
- UTF-8
-
-
org.springframework.boot
spring-boot-maven-plugin
- ${spring-boot.version}
- true
- JAR
+
+ com.htfp.weather.WeatherServiceApplication
+ ZIP
- repackage
+
repackage
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ 1.8
+ 1.8
+
+
-
diff --git a/weather-service/src/main/java/com/htfp/weather/download/DownLoadFileInfo.java b/weather-service/src/main/java/com/htfp/weather/download/DownLoadFileInfo.java
index a5e5a31..6813782 100644
--- a/weather-service/src/main/java/com/htfp/weather/download/DownLoadFileInfo.java
+++ b/weather-service/src/main/java/com/htfp/weather/download/DownLoadFileInfo.java
@@ -26,6 +26,7 @@ public class DownLoadFileInfo {
private String savePath;
private boolean isDownloadSuccess;
+ private String errorMsg;
public DownLoadFileInfo() {
}
diff --git a/weather-service/src/main/java/com/htfp/weather/download/gfs/GfsDataConfig.java b/weather-service/src/main/java/com/htfp/weather/download/gfs/GfsDataConfig.java
index 2d21a20..d61a969 100644
--- a/weather-service/src/main/java/com/htfp/weather/download/gfs/GfsDataConfig.java
+++ b/weather-service/src/main/java/com/htfp/weather/download/gfs/GfsDataConfig.java
@@ -108,7 +108,7 @@ public class GfsDataConfig {
}
public void readConfig() {
try (InputStream f = new FileInputStream(configPath)){
- GfsDataConfig gfsDataConfig = JSONUtils.json2pojo(IOUtils.toString(f), GfsDataConfig.class);
+ GfsDataConfig gfsDataConfig = JSONUtils.json2obj(IOUtils.toString(f), GfsDataConfig.class);
this.duration = gfsDataConfig.getDuration();
this.minLon = gfsDataConfig.getMinLon();
this.maxLon = gfsDataConfig.getMaxLon();
diff --git a/weather-service/src/main/java/com/htfp/weather/download/gfs/GfsDownloader.java b/weather-service/src/main/java/com/htfp/weather/download/gfs/GfsDownloader.java
index 077616b..7aa3dfc 100644
--- a/weather-service/src/main/java/com/htfp/weather/download/gfs/GfsDownloader.java
+++ b/weather-service/src/main/java/com/htfp/weather/download/gfs/GfsDownloader.java
@@ -75,8 +75,13 @@ public class GfsDownloader extends BaseDataDownloader {
try {
// DONE: 改用okhttp FIXME 2024/6/8: 如果连接是https,jar包启动下载会报错 javax.net.ssl.SSLException: Received fatal alert: internal_error
HttpClientUtils.downloadFileByUrl(url, fileOut.getPath());
- log.info("[GFS Download] 文件下载成功: {}", fileOut);
- downLoadFileInfo.setDownloadSuccess(fileValid(fileOut.getAbsolutePath()));
+ boolean fileIsValid = fileValid(fileOut.getAbsolutePath());
+ downLoadFileInfo.setDownloadSuccess(fileIsValid);
+ if (fileIsValid) {
+ log.info("[GFS Download] 文件下载成功: {}", fileOut);
+ } else {
+ log.error("[GFS Download] 文件下载失败,文件打开错误: {}", fileOut);
+ }
} catch (Exception e) {
// DONE 2024/5/24: 文件服务器在外网不稳定,增加重试机制
downLoadFileInfo.setDownloadSuccess(false);
@@ -85,7 +90,8 @@ public class GfsDownloader extends BaseDataDownloader {
return download0(downLoadFileInfo, retryNum - 1);
} else {
e.printStackTrace();
- log.error("[GFS Download] 文件下载失败: {}", fileOut);
+ downLoadFileInfo.setErrorMsg(e.getMessage());
+ log.error("[GFS Download] 文件{}下载失败: {}", fileOut, e.getMessage());
}
}
return downLoadFileInfo;
@@ -97,6 +103,7 @@ public class GfsDownloader extends BaseDataDownloader {
ncFile.getLocation();
return true;
} catch (IOException e){
+ log.error("GFS文件{}校验失败: {}", file, e.getMessage());
return false;
}
}
diff --git a/weather-service/src/main/java/com/htfp/weather/griddata/common/TableConfig.java b/weather-service/src/main/java/com/htfp/weather/griddata/common/TableConfig.java
index ca1b226..3751c22 100644
--- a/weather-service/src/main/java/com/htfp/weather/griddata/common/TableConfig.java
+++ b/weather-service/src/main/java/com/htfp/weather/griddata/common/TableConfig.java
@@ -81,7 +81,7 @@ public class TableConfig {
// String pathSeparator = System.getProperty("file.separator");
try (InputStream f = new FileInputStream(configPath)){
String jsonStr = IOUtils.toString(f, StandardCharsets.UTF_8);
- TableConfig tableConfig = JSONUtils.json2pojo(jsonStr, TableConfig.class);
+ TableConfig tableConfig = JSONUtils.json2obj(jsonStr, TableConfig.class);
this.dataTableName = tableConfig.getDataTableName();
this.metaTableName = tableConfig.getMetaTableName();
this.dataIndexName = tableConfig.getDataIndexName();
diff --git a/weather-service/src/main/java/com/htfp/weather/griddata/common/TableStoreConf.java b/weather-service/src/main/java/com/htfp/weather/griddata/common/TableStoreConf.java
index 49b4a4c..d6fe8f7 100644
--- a/weather-service/src/main/java/com/htfp/weather/griddata/common/TableStoreConf.java
+++ b/weather-service/src/main/java/com/htfp/weather/griddata/common/TableStoreConf.java
@@ -1,6 +1,5 @@
package com.htfp.weather.griddata.common;
-import com.google.gson.Gson;
import com.htfp.weather.utils.JSONUtils;
import lombok.Data;
import org.apache.commons.io.IOUtils;
@@ -25,7 +24,7 @@ public class TableStoreConf {
String path = System.getProperty("user.dir") + "./tablestoreConf.json";
InputStream f = new FileInputStream(path);
String jsonStr = IOUtils.toString(f, StandardCharsets.UTF_8);
- return JSONUtils.json2pojo(jsonStr, TableStoreConf.class);
+ return JSONUtils.json2obj(jsonStr, TableStoreConf.class);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
diff --git a/weather-service/src/main/java/com/htfp/weather/griddata/operation/BaseTableOperation.java b/weather-service/src/main/java/com/htfp/weather/griddata/operation/BaseTableOperation.java
index 21516b5..bb3a80a 100644
--- a/weather-service/src/main/java/com/htfp/weather/griddata/operation/BaseTableOperation.java
+++ b/weather-service/src/main/java/com/htfp/weather/griddata/operation/BaseTableOperation.java
@@ -19,10 +19,11 @@ public abstract class BaseTableOperation{
private TableConfig tableConfig;
@Autowired
private TableStoreConf tableStoreConf;
+ TableStoreGridConfig config;
@PostConstruct
public void init() {
- TableStoreGridConfig config = new TableStoreGridConfig();
+ config = new TableStoreGridConfig();
if (tableStoreConf == null){
tableStoreConf = TableStoreConf.init();
}
@@ -36,12 +37,25 @@ public abstract class BaseTableOperation{
config.setTableStoreInstance(tableStoreConf.getInstanceName());
config.setDataTableName(tableConfig.getDataTableName());
config.setMetaTableName(tableConfig.getMetaTableName());
- tableStoreGrid = new TableStoreGrid(config);
+ connect();
}
+ /**
+ * 连接数据库
+ */
+ public void connect() {
+ if (tableStoreGrid == null) {
+ tableStoreGrid = new TableStoreGrid(config);
+ }
+ }
+
+ /** 断开数据库
+ *
+ */
public void close() {
if (tableStoreGrid != null) {
tableStoreGrid.close();
+ tableStoreGrid = null;
}
}
}
diff --git a/weather-service/src/main/java/com/htfp/weather/griddata/operation/CreateTable.java b/weather-service/src/main/java/com/htfp/weather/griddata/operation/CreateTable.java
index 93a10a5..6fb38fa 100644
--- a/weather-service/src/main/java/com/htfp/weather/griddata/operation/CreateTable.java
+++ b/weather-service/src/main/java/com/htfp/weather/griddata/operation/CreateTable.java
@@ -39,7 +39,7 @@ public class CreateTable extends BaseTableOperation {
AttributionEnum[] values = AttributionEnum.values();
ArrayList fieldSchemas = new ArrayList<>();
for (AttributionEnum attributionEnum : values) {
- fieldSchemas.add(new FieldSchema(attributionEnum.getName(), FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true));
+ fieldSchemas.add(new FieldSchema(attributionEnum.getName(), attributionEnum.getType()).setIndex(true).setEnableSortAndAgg(true));
}
indexSchema.setFieldSchemas(fieldSchemas);
this.tableStoreGrid.createMetaIndex(TableConfigStatic.META_INDEX_NAME, indexSchema);
diff --git a/weather-service/src/main/java/com/htfp/weather/griddata/operation/GfsDataFetcher.java b/weather-service/src/main/java/com/htfp/weather/griddata/operation/GfsDataFetcher.java
index 7112dd1..785c30e 100644
--- a/weather-service/src/main/java/com/htfp/weather/griddata/operation/GfsDataFetcher.java
+++ b/weather-service/src/main/java/com/htfp/weather/griddata/operation/GfsDataFetcher.java
@@ -20,6 +20,7 @@ import org.springframework.util.CollectionUtils;
import ucar.ma2.Array;
import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
@@ -43,7 +44,10 @@ public class GfsDataFetcher extends BaseTableOperation {
@Resource
QueryMeta queryMeta;
GridDataSetMeta lastGridDataSetMeta;
-
+ @PreDestroy
+ public void destroy() {
+ close();
+ }
public GridDataSetMeta getLastGridDataSetMeta() {
if (lastGridDataSetMeta == null) {
updateLastGridDataSetMeta();
diff --git a/weather-service/src/main/java/com/htfp/weather/griddata/operation/GfsDataImport.java b/weather-service/src/main/java/com/htfp/weather/griddata/operation/GfsDataImport.java
index 1585511..e60827f 100644
--- a/weather-service/src/main/java/com/htfp/weather/griddata/operation/GfsDataImport.java
+++ b/weather-service/src/main/java/com/htfp/weather/griddata/operation/GfsDataImport.java
@@ -3,6 +3,7 @@ package com.htfp.weather.griddata.operation;
import com.aliyun.tablestore.grid.GridDataWriter;
import com.aliyun.tablestore.grid.model.GridDataSetMeta;
import com.aliyun.tablestore.grid.model.StoreOptions;
+import com.aliyun.tablestore.grid.model.grid.Grid;
import com.aliyun.tablestore.grid.model.grid.Grid2D;
import com.htfp.weather.download.gfs.GfsVariableHeightEnum;
@@ -17,9 +18,9 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.DependsOn;
-import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
+import sun.nio.ch.DirectBuffer;
import ucar.ma2.*;
import ucar.nc2.NetcdfFile;
import ucar.nc2.NetcdfFiles;
@@ -27,8 +28,8 @@ import ucar.nc2.Variable;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
-import javax.annotation.security.DenyAll;
import java.io.File;
+import java.nio.ByteBuffer;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@@ -50,7 +51,7 @@ public class GfsDataImport extends BaseTableOperation {
@Getter
boolean importing = false;
private final ExecutorService executorService = Executors.newFixedThreadPool(5);
-
+ GridDataWriter writer;
/**
* 表格元数据初始化
*
@@ -91,6 +92,7 @@ public class GfsDataImport extends BaseTableOperation {
/** 导入指定起报时间的所有文件*/
public synchronized List importData(OffsetDateTime refTime) throws Exception {
importing = true;
+ connect();
try {
long start = System.currentTimeMillis();
List fileList = getFiles(refTime);
@@ -103,8 +105,9 @@ public class GfsDataImport extends BaseTableOperation {
List fileVariables = getFileVariables(tableConfig.variableList);
int[] shape = new int[]{tableConfig.timeSizeMax, tableConfig.levSize, tableConfig.latSize, tableConfig.lonSize};
GridDataSetMeta meta = initMeta(dataSetId, tableConfig.dataType, fileVariables, shape);
- List forecastHours = new ArrayList<>(); // 所有有效预报时效坐标记录到数据库属性中
+ writer = tableStoreGrid.getDataWriter(meta);
+ List forecastHours = new ArrayList<>(); // 所有有效预报时效坐标记录到数据库属性中
// todo 2024/5/13: 待优化,用于数据库的索引,必须连续,因此必须保证前一个时刻成功导入后才能导入下一个时刻,数据量大的时候导入时间较长
List finishedList;
// List> futures = new ArrayList<>();
@@ -138,7 +141,7 @@ public class GfsDataImport extends BaseTableOperation {
meta.addAttribute("status", "DONE");
}
long end = System.currentTimeMillis();
- log.info("[tablestore] 数据导入完成, 耗时: {} s, forecastHours: {}", (end - start)/1000., forecastHours);
+ log.info("[tablestore] 数据导入完成, 耗时: {} s, forecastHours: {}", (end - start) / 1000., forecastHours);
meta.setForecastHours(forecastHours);
meta.addAttribute("reference_time", refTime.toInstant().toEpochMilli());
putMeta(meta);
@@ -147,6 +150,7 @@ public class GfsDataImport extends BaseTableOperation {
return finishedList;
} finally {
importing = false;
+ close();
}
}
@@ -159,8 +163,6 @@ public class GfsDataImport extends BaseTableOperation {
*/
public ImportFileInfo importFromNcFile(GridDataSetMeta meta, String file, int iTime, int forecastHour) {
try (NetcdfFile ncFile = NetcdfFiles.open(file)) {
- GridDataWriter writer = tableStoreGrid.getDataWriter(meta);
-
// 相对reftime的小时数,文件可能缺失,因此time可能是不连续的,但是iTime是连续的
if (forecastHour == -1) {
forecastHour = (int) ncFile.findVariable("time").read().getDouble(0);
@@ -182,36 +184,47 @@ public class GfsDataImport extends BaseTableOperation {
int zSize = variable.getShape(1);
for (int z = 0; z < zSize; z++) {
// 注意高度索引是递增排序
- Array array = variable.read(new int[]{0, z, 0, 0}, new int[]{1, 1, xsize, ysize});
- transferUnit(variableName, array);
- Grid2D grid2D = new Grid2D(array.getDataAsByteBuffer(), variable.getDataType(),
+ ByteBuffer dataBuffer = transferUnit(
+ variableName, variable.read(new int[]{0, z, 0, 0}, new int[]{1, 1, xsize, ysize})
+ ).getDataAsByteBuffer();
+ Grid2D grid2D = new Grid2D(dataBuffer, variable.getDataType(),
new int[]{0, 0}, new int[]{xsize, ysize});
writer.writeGrid2D(variableName, iTime, z, grid2D);
+ dataBuffer=null; grid2D = null;
}
} else if (shapeLength == 3) {
// DONE 2024/5/24: 导入不含高度坐标的数据,如降水量,阵风风速
- Array array = variable.read(new int[]{0, 0, 0}, new int[]{1,xsize, ysize});
- transferUnit(variableName, array);
- Grid2D grid2D = new Grid2D(array.getDataAsByteBuffer(), variable.getDataType(),
+ ByteBuffer dataBuffer = transferUnit(
+ variableName, variable.read(new int[]{0, 0, 0}, new int[]{1,xsize, ysize}))
+ .getDataAsByteBuffer();
+ Grid2D grid2D = new Grid2D(dataBuffer, variable.getDataType(),
new int[]{0, 0}, new int[]{xsize, ysize});
writer.writeGrid2D(variableName, iTime, 0, grid2D);
+ dataBuffer= null; grid2D = null;
}
} else {
log.warn("[tablestore] 数据文件 {} 中没有变量 {}", ncFile.getLocation(), variableName);
}
+
}
// 导入风速风向
- importWind(writer, meta, ncFile, iTime);
+ importWind(meta, ncFile, iTime);
} catch (Exception e) {
log.error("[tablestore] 导入文件数据失败,_t={}: {}", forecastHour, file, e);
return new ImportFileInfo(false, file, forecastHour, iTime);
+ } finally {
+ System.gc();
}
log.info("[tablestore] 导入文件数据成功,_t={}: {}", forecastHour, file);
return new ImportFileInfo(true, file, forecastHour, iTime);
}
- /**单位转换*/
- private void transferUnit(String variableName, Array array) {
+ /**
+ * 单位转换
+ *
+ * @return
+ */
+ private Array transferUnit(String variableName, Array array) {
if (GfsVariableIsobaricEnum.TEMP.getNameInFile().equals(variableName)) {
MeteoUtils.kelvin2Celsius(array);
}
@@ -221,10 +234,11 @@ public class GfsDataImport extends BaseTableOperation {
if (GfsVariableHeightEnum.PRECIP.getNameInFile().equals(variableName)) {
MeteoUtils.precipRate2mmPerHour(array);
}
+ return array;
}
/** 计算风速风向,包括地面坐标和气压坐标,并导入数据库*/
- private void importWind(GridDataWriter writer, GridDataSetMeta meta, NetcdfFile ncFile, int iTime) throws Exception {
+ private void importWind( GridDataSetMeta meta, NetcdfFile ncFile, int iTime) throws Exception {
// TODO 2024/5/8: 风速风向最好单独保存为一个文件,如果后续需要二维可视化,可以直接本地读取出图,减少网络请求
for (String suffix : new String[]{"_isobaric", "_height_above_ground"}) {
Variable uwnd = ncFile.findVariable("u-component_of_wind" + suffix);
@@ -248,6 +262,9 @@ public class GfsDataImport extends BaseTableOperation {
Grid2D dirGrid2D = new Grid2D(dirArray.getDataAsByteBuffer(), dirArray.getDataType(),
new int[]{0, 0}, new int[]{xsize, ysize});
writer.writeGrid2D("Wind_direction" + suffix, iTime, z, dirGrid2D);
+
+ uwndArray = null; vwndArray = null; speedArray = null; dirArray = null;
+ speedGrid2D = null; dirGrid2D = null;
}
}
}
diff --git a/weather-service/src/main/java/com/htfp/weather/griddata/operation/QueryMeta.java b/weather-service/src/main/java/com/htfp/weather/griddata/operation/QueryMeta.java
index 350354b..8bbbac1 100644
--- a/weather-service/src/main/java/com/htfp/weather/griddata/operation/QueryMeta.java
+++ b/weather-service/src/main/java/com/htfp/weather/griddata/operation/QueryMeta.java
@@ -3,6 +3,7 @@ package com.htfp.weather.griddata.operation;
import com.alicloud.openservices.tablestore.model.search.sort.FieldSort;
import com.alicloud.openservices.tablestore.model.search.sort.Sort;
import com.alicloud.openservices.tablestore.model.search.sort.SortOrder;
+import com.aliyun.tablestore.grid.consts.AttributionEnum;
import com.aliyun.tablestore.grid.core.QueryBuilder;
import com.aliyun.tablestore.grid.model.GridDataSetMeta;
import com.aliyun.tablestore.grid.model.QueryGridDataSetResult;
@@ -11,7 +12,10 @@ import com.htfp.weather.griddata.common.TableConfig;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.annotation.Resource;
+import javax.crypto.interfaces.PBEKey;
import java.util.Collections;
import java.util.List;
@@ -24,6 +28,11 @@ import java.util.List;
public class QueryMeta extends BaseTableOperation {
@Resource
TableConfig tableConfig;
+
+ @PreDestroy
+ public void destroy() {
+ close();
+ }
/**
* 查询最新的数据集信息
*
@@ -37,7 +46,8 @@ public class QueryMeta extends BaseTableOperation {
QueryBuilder.and()
.equal("status", "DONE")
.build(),
- new QueryParams(0, 10, new Sort(Collections.singletonList(new FieldSort("reference_time", SortOrder.DESC)))));
+ new QueryParams(0, 10, new Sort(Collections.singletonList(new FieldSort(AttributionEnum.REFERENCE_TIME.getName(), SortOrder.DESC))))
+ );
List gridDataSetMetas = result.getGridDataSetMetas();
if (CollectionUtils.isEmpty(gridDataSetMetas)) {
throw new RuntimeException("meta table为空");
diff --git a/weather-service/src/main/java/com/htfp/weather/schedule/GridDataProcessor.java b/weather-service/src/main/java/com/htfp/weather/schedule/GridDataProcessor.java
index 3dfea7e..08028cf 100644
--- a/weather-service/src/main/java/com/htfp/weather/schedule/GridDataProcessor.java
+++ b/weather-service/src/main/java/com/htfp/weather/schedule/GridDataProcessor.java
@@ -50,8 +50,8 @@ public class GridDataProcessor {
private final ExecutorService executorService = Executors.newFixedThreadPool(3);
boolean test = false;
- /**每日北京时0,8,12,18点执行*/
- @Scheduled(cron = "0 0 0,8,12,18 * * ?")
+ /**每日北京时0,8,12,18点 30分执行*/
+ @Scheduled(cron = "0 30 0,8,12,18 * * ?")
public void dailyDataProcess() {
if (download()) {
importToTableStore();
diff --git a/weather-service/src/main/java/com/htfp/weather/utils/HttpClientUtils.java b/weather-service/src/main/java/com/htfp/weather/utils/HttpClientUtils.java
index 060cf82..b54d497 100644
--- a/weather-service/src/main/java/com/htfp/weather/utils/HttpClientUtils.java
+++ b/weather-service/src/main/java/com/htfp/weather/utils/HttpClientUtils.java
@@ -53,7 +53,7 @@ public class HttpClientUtils {
// }
public static T sendGet(String url, Map params, Class responseClass) throws Exception {
String jsonStr = sendGet(url, params);
- return JSONUtils.json2pojo(jsonStr, responseClass);
+ return JSONUtils.json2obj(jsonStr, responseClass);
}
public static String sendGet(String url, Map params) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
@@ -92,7 +92,7 @@ public class HttpClientUtils {
}
- public static boolean downloadFileByUrl(String sourceUrl, String localPath) {
+ public static void downloadFileByUrl(String sourceUrl, String localPath) throws IOException {
File file = new File(localPath);
Request request = new Request.Builder()
.url(sourceUrl)
@@ -100,15 +100,15 @@ public class HttpClientUtils {
try (Response response = DONLOAD_OKHTTP_CLIENT.newCall(request).execute()) {
if (response.code() == 200) {
+ assert response.body() != null;
InputStream stream = response.body().byteStream();
- Files.copy(stream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
+ long dataBytes = Files.copy(stream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
+ log.debug("文件{}下载完成,文件大小: {} bytes", localPath, dataBytes);
} else {
- return false;
+ throw new RuntimeException("文件下载失败,Response:" + response);
}
- } catch (IOException e) {
- return false;
}
- return true;
+
}
}
diff --git a/weather-service/src/main/java/com/htfp/weather/utils/JSONUtils.java b/weather-service/src/main/java/com/htfp/weather/utils/JSONUtils.java
index da0fd75..c36db87 100644
--- a/weather-service/src/main/java/com/htfp/weather/utils/JSONUtils.java
+++ b/weather-service/src/main/java/com/htfp/weather/utils/JSONUtils.java
@@ -1,13 +1,13 @@
package com.htfp.weather.utils;
+import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -20,67 +20,58 @@ public class JSONUtils {
}
public static ObjectMapper getInstance() {
-
return objectMapper;
}
/**
* javaBean,list,array convert to json string
*/
- public static String obj2json(Object obj) throws Exception {
+ public static String obj2json(Object obj) throws JsonProcessingException {
return objectMapper.writeValueAsString(obj);
}
/**
* json string convert to javaBean
*/
- public static T json2pojo(String jsonStr, Class clazz)
- throws Exception {
+ public static T json2obj(String jsonStr, Class clazz) throws IOException {
return objectMapper.readValue(jsonStr, clazz);
}
-
+ /**
+ * 通用静态方法:将JSON字符串转换为带泛型的类, T嵌套了其他泛型
+ */
+ public static T json2obj(String json, TypeReference typeReference) throws IOException {
+ return objectMapper.readValue(json, typeReference);
+ }
/**
* json string convert to map
*/
- public static Map json2map(String jsonStr)
- throws Exception {
- return objectMapper.readValue(jsonStr, Map.class);
+ public static Map json2map(String jsonStr) throws IOException {
+ return objectMapper.readValue(jsonStr, new TypeReference