Commit f3313744 authored by Xuxiaodong's avatar Xuxiaodong

对接入库类与发送di

parent 71409ae5
......@@ -64,6 +64,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
......
package com.jianju.di.Manager;
import visad.browser.Convert;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.*;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class PostGISMannager {
private Connection c ;
private static Statement stmt;
private String m_tablename;
private float fSrcDefault = -9999.0f; ///数据源中默认缺测值
private float fDesDefault = 999999f; ///NC文件中(结果)默认缺测值
public PostGISMannager(String host, String port, String dbname, String tablename, String user, String password) throws SQLException, ClassNotFoundException, SQLException {
Class.forName("org.postgresql.Driver");
c= DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+dbname,user,password);
stmt=c.createStatement();
m_tablename=tablename;
}
public void createOlrDay(String tableName) throws SQLException
{
String sql="create table if not exists public."+tableName+
"(byteData bytea,"+
"name char(100),"+
"time date,"+
"minlon numeric,"+
"maxlon numeric,"+
"maxlat numeric,"+
"minlat numeric,"+
"type char(50),"+
"dataCode char(50),"+
"geom geometry);";
stmt.executeUpdate(sql);
}
public void createTableGDAS(String tableName) throws SQLException {
String sql="create table if not exists public."+tableName+
......@@ -35,7 +50,7 @@ public class PostGISMannager {
stmt.executeUpdate(sql);
}
public void insertGodasToPG(String tableName,String gdasName) throws IOException, SQLException, ParseException {
public int insertGodasToPG(String tableName,String gdasName) throws IOException, SQLException, ParseException {
......@@ -54,7 +69,7 @@ public class PostGISMannager {
String time;
float minLat,maxLat,minLon,maxLon;
String mongoID = null;
int status=0;
maxLat= (float) 64.499;
minLat=-74;
minLon=1;
......@@ -149,29 +164,16 @@ public class PostGISMannager {
ps.setFloat(7, minLat);
ps.setString(8, "u_flx");
ps.setString(9, datacode);
ps.executeUpdate();
status=ps.executeUpdate();
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
} finally {
try {
if (ps != null) {
ps.close();
}
if (c != null) {
// conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace(System.out);
}
}
}
lines++;
}
return status;
}
......@@ -189,4 +191,169 @@ public class PostGISMannager {
}
return out.toByteArray();
}
public int insertOlrToPG(String tableName,String dataPath) throws IOException, SQLException, ParseException {
int lines=0;
String geom;
String type="orl";
String datacode="REAN_NOAA_OLR_ORIGINAL";
String time;
float minLat,maxLat,minLon,maxLon;
String mongoID = null;
String name="";
int status=0;
//String []s=str.split(",");
name=dataPath;
String []s=name.split("#");
time=s[1];
String dataName="#"+time;
SimpleDateFormat simFormat = new SimpleDateFormat("yyyyMMdd");
java.util.Date tiemDate = simFormat.parse(time);
Date sqlDate=new Date(tiemDate.getTime());
maxLat=90;
minLat=-90;
minLon=0;
maxLon=(float) 357.5;
type="olr";
//datacode=s[7];
geom="POLYGON(("+minLon+" "+maxLat+","+maxLon+" "+maxLat+","+maxLon+" "+minLat+","+minLon+" "+minLat+","+minLon+" "+maxLat+"))";
PreparedStatement ps = null;
ResultSet st = null;
try
{
System.out.println("success connect");
String sql ="insert into "+tableName+"(bytedata,name,time,minlon,maxlon,maxlat,minlat,type,dataCode,geom)" +
" values(?,?,?,?,?,?,?,?,?,"+"'"+geom+"'"+")";
try {
ps = c.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
String fpath = name;
float[] fData=readTxtData(fpath);
byte[] byteData=floatArrayToByteArray(fData);
ps.setBytes(1, byteData);
ps.setString(2,dataName);
ps.setDate(3, sqlDate);
ps.setFloat(4,minLon);
ps.setFloat(5,maxLon);
ps.setFloat(6,maxLat);
ps.setFloat(7,maxLat);
ps.setString(8,type);
ps.setString(9,datacode);
status=ps.executeUpdate();
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
return status;
}
public float[] readTxtData(String strFileName)
{
int iLat = 73; ///维度
int iLon = 144; ///经度
int iDataLen = iLat * iLon; ///数据长度
float[] fdataArray = null; ///存放结果数据
int [] iCntArray = null; ///存放计数
BufferedReader br = null;
try
{
if ((new File(strFileName).exists()))
{
br = new BufferedReader(new FileReader(strFileName));
}
else {
// log.error("数据文件" + strFileName + "不存在");
// throw new ParserException(EI.STR_EVENTTYPE_OPENFILE + "+数据文件" + strFileName + "不存在");
}
fdataArray = new float[iDataLen];
iCntArray = new int[iDataLen];
String strLine = null;
int dataIndex = 0;
int iLineNum = 0; ///行号
float fdata = 0; ///每个数据
while ((strLine = br.readLine()) != null)
{
iLineNum++;
if((iLineNum==1) || (iLineNum==73))
{
dataIndex=iLon;
continue;
}
if(strLine.length()!=864) ///行长度不等于144*6+1个字节,则说明此行数据存在问题
{
//throw new ParserException(EI.STR_EVENTTYPE_RDWRDATA + "+读取数据文件" + strFileName + "失败,第" + iLineNum + "行数据长度为"+strLine.length());
}
int iPos = 0;
if(iLineNum<73) ///直接赋值
{
do {
fdata = Float.valueOf(strLine.substring(iPos, iPos + 6).trim());
if(fdata!=fSrcDefault)
{
fdataArray[dataIndex] = fdata;
iCntArray[dataIndex]++;
}
iPos += 6;
dataIndex++;
} while (iPos < strLine.length());
}
else ///进行累加
{
do {
fdata = Float.valueOf(strLine.substring(iPos, iPos + 6).trim());
if(fdata!=fSrcDefault)
{
fdataArray[dataIndex] += fdata;
iCntArray[dataIndex]++;
}
iPos += 6;
dataIndex++;
} while (iPos < strLine.length());
}
}
///进行算术平均
for(int i = 0; i < iDataLen; i++)
{
fdataArray[i]=(iCntArray[i]>0)?(fdataArray[i]*0.1f/iCntArray[i]):fDesDefault;
}
}
catch (IOException | NullPointerException e)
{
// throw new ParserException(EI.STR_EVENTTYPE_RDWRDATA + "+读取数据文件" + strFileName + "失败", e);
}
finally
{
try {
if (null != br) {
br.close();
}
} catch (Exception e) {
}
}
return fdataArray;
}
private static byte[] floatArrayToByteArray(float[] data) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
for (int i = 0; i < data.length; i++) {
try {
dataOutputStream.writeFloat(data[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
return out.toByteArray();
}
}
package com.jianju.di.Manager;
import org.xml.sax.SAXException;
import ucar.ma2.InvalidRangeException;
......@@ -15,9 +16,14 @@ public class Test {
public static void main(String[] arg) throws ClassNotFoundException, SQLException, IOException, SQLException, IOException, InvalidRangeException, ParserConfigurationException, SAXException, TransformerException, ParseException {
//数据入库Test
// MongodbManager m_MyMongoDB = new MongodbManager("10.40.16.39", 27088, "Data", "day");
// PostGISMannager postGIS = new PostGISMannager("localhost", "5432",
// "postgis_25_sample", "GODAS", "postgres", "123456");
// postGIS.createTableGDAS("GODAS");
// postGIS.insertGodasToPG("GODAS", "E:\\NewWeather\\godas.P.20200105.grb");
PostGISMannager postGIS = new PostGISMannager("localhost", "5432",
"postgis_25_sample", "GODAS", "postgres", "123456");
postGIS.createTableGDAS("GODAS");
postGIS.insertGodasToPG("GODAS", "E:\\NewWeather\\godas.P.20200105.grb");
postGIS.createOlrDay("Olr");
postGIS.insertOlrToPG("olr","E:\\NOAA\\#20050718");
}
}
package com.jianju.di.entity;
import lombok.Data;
import java.util.Date;
@Data
public class Di {
private String type;
private String name;
private String message;
private long occur_time;
private Fields fields;
@Data
public class Fields {
String DATA_TYPE;
String DATA_TYPE_1;
String RECEIVE;
String SEND;
Date TRAN_TIME;
Date DATA_TIME;
String DATA_FLOW;
String SYSTEM;
String PROCESS_LINK;
Date PROCESS_START_TIME;
Date PROCESS_END_TIME;
String FILE_NAME_O;
String FILE_NAME_N;
double FILE_SIZE;
int PROCESS_STATE;
int BUSINESS_STATE;
Date RECORD_TIME;
}
public static void main(String[] args) {
}
}
package com.jianju.di.task;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jianju.di.Manager.PostGISMannager;
import com.jianju.di.entity.Di;
import com.jianju.di.utils.FtpUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
......@@ -55,8 +60,9 @@ public class PullFileTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMdd");
//每日0点与中午12点执行;
//@Scheduled(cron ="0 0 12,18 * * ?" )
//@Scheduled(cron ="0 0 19 * * ?" )
@Scheduled(fixedRate = 10000)
public void PullFileAndSendDi() throws Exception {
FtpUtil.FtpConfig ftpConfig = new FtpUtil.FtpConfig().setHost(host).setPort(port).setUserName(userName).setPassword(password);
......@@ -65,15 +71,46 @@ public class PullFileTask {
c.add(Calendar.DATE, -2);
String fileName = "#"+dateFormat.format(c.getTime());
log.info("开始下载"+"#"+fileName);
log.info("开始下载----"+"#"+fileName);
boolean download = FtpUtil.download(ftpConfig, remotePath, fileName, localPath, fileName);
if(download)
{
log.info("下载成功");
// PostGISMannager postGIS = new PostGISMannager(dbHost, dbPort,
// dbName, tbName, dbUserName, dbPassword);
// postGIS.createTableGDAS("GODAS");
// postGIS.insertGodasToPG("GODAS", );
PostGISMannager postGIS = new PostGISMannager(dbHost, dbPort,
dbName, tbName, dbUserName, dbPassword);
postGIS.createOlrDay(tbName);
if(postGIS.insertOlrToPG(tbName, localPath + fileName)>0){
//发送Di
Di di = new Di();
// di.setType();
// di.setName();
// di.setMessage();
// di.setOccur_time();
Di.Fields fields = di.new Fields();
di.setFields(fields);
OkHttpClient httpClient = new OkHttpClient();
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSONObject.toJSONString(di));
Request request = new Request.Builder()
.url("127.0.0.1:29999/transfer")
.post(requestBody)
.build();
//发送请求获取响应
try {
Response response=httpClient.newCall(request).execute();
//判断请求是否成功
if(response.isSuccessful()){
//打印服务端返回结果
log.info("请求代理返回结果+:"+response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
......@@ -83,4 +120,6 @@ public class PullFileTask {
}
}
......@@ -9,8 +9,8 @@ ftp:
remotepath: /NOAA/NCEP/noaa18_olr/
localpath: C:\Users\D\Desktop\证书\
postgis:
host: 10.40.16.39
port: 27088
host: 10.40.16.38
port: 5432
username:
password:
dbname: Data
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment