pull/2/head
chenbingxuan 2023-12-12 17:07:42 +08:00
parent c28ce8fa92
commit 5febc67e89
6 changed files with 88 additions and 5 deletions

View File

@ -17,6 +17,11 @@
<version>4.4.0</version> <!-- 使用最新版本 --> <version>4.4.0</version> <!-- 使用最新版本 -->
</dependency> </dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version> <!-- 请根据实际情况使用最新版本 -->
</dependency>
<!-- Elasticsearch Low Level REST Client --> <!-- Elasticsearch Low Level REST Client -->
<dependency> <dependency>

View File

@ -4,7 +4,6 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.ser.std.StringSerializer;
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
@ -39,6 +38,8 @@ import net.srt.vo.DataDatabaseVO;
import net.srt.vo.SchemaTableDataVo; import net.srt.vo.SchemaTableDataVo;
import net.srt.vo.SqlGenerationVo; import net.srt.vo.SqlGenerationVo;
import net.srt.vo.TableVo; import net.srt.vo.TableVo;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.clients.producer.ProducerRecord;
@ -53,6 +54,9 @@ import srt.cloud.framework.dbswitch.core.model.TableDescription;
import srt.cloud.framework.dbswitch.core.service.IMetaDataByJdbcService; import srt.cloud.framework.dbswitch.core.service.IMetaDataByJdbcService;
import srt.cloud.framework.dbswitch.core.service.impl.MetaDataByJdbcServiceImpl; import srt.cloud.framework.dbswitch.core.service.impl.MetaDataByJdbcServiceImpl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -147,7 +151,7 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
ProductTypeEnum productTypeEnum = ProductTypeEnum.getByIndex(vo.getDatabaseType()); ProductTypeEnum productTypeEnum = ProductTypeEnum.getByIndex(vo.getDatabaseType());
IMetaDataByJdbcService metaDataService = new MetaDataByJdbcServiceImpl(productTypeEnum); IMetaDataByJdbcService metaDataService = new MetaDataByJdbcServiceImpl(productTypeEnum);
if (StringUtil.isBlank(vo.getJdbcUrl())) { if (StringUtil.isNotBlank(vo.getJdbcUrl())) {
if(productTypeEnum.getUrl()!=null){ if(productTypeEnum.getUrl()!=null){
vo.setJdbcUrl(productTypeEnum.getUrl() vo.setJdbcUrl(productTypeEnum.getUrl()
.replace("{host}", vo.getDatabaseIp()) .replace("{host}", vo.getDatabaseIp())
@ -155,7 +159,7 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
.replace("{database}", vo.getDatabaseName())); .replace("{database}", vo.getDatabaseName()));
} }
} }
if (vo.getIsJdbc()== IsJdbc.NO.getValue()){ if (vo.getIsJdbc().equals(IsJdbc.YES.getValue())){
metaDataService.testQuerySQL( metaDataService.testQuerySQL(
vo.getJdbcUrl(), vo.getJdbcUrl(),
vo.getUserName(), vo.getUserName(),
@ -181,6 +185,7 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
} catch (Exception e) { } catch (Exception e) {
// 捕获连接异常 // 捕获连接异常
System.err.println("redis异常" + e.getMessage()); System.err.println("redis异常" + e.getMessage());
throw new ServerException("连接失败");
} finally { } finally {
// 关闭连接 // 关闭连接
jedis.close(); jedis.close();
@ -208,6 +213,7 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
System.out.println("消息发送成功"); System.out.println("消息发送成功");
} catch (Exception e) { } catch (Exception e) {
System.err.println("连接失败: " + e.getMessage()); System.err.println("连接失败: " + e.getMessage());
throw new ServerException("连接失败");
} finally { } finally {
// 关闭生产者 // 关闭生产者
if (producer != null) { if (producer != null) {
@ -230,8 +236,46 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
System.out.println("连接数据库成功"); System.out.println("连接数据库成功");
} catch (Exception e) { } catch (Exception e) {
System.err.println("连接MongoDB报错: " + e.getMessage()); System.err.println("连接MongoDB报错: " + e.getMessage());
throw new ServerException("连接失败");
} }
} }
//
if(vo.getDatabaseType().equals(Judgment.FTP.getIndex())){
// String server = "ftp.example.com";
String user = vo.getUserName();
String password = vo.getPassword();
FTPClient ftpClient = new FTPClient();
try {
// 连接到 FTP 服务器
ftpClient.connect(vo.getDatabaseIp(),Integer.valueOf(vo.getDatabasePort()));
ftpClient.login(user, password);
ftpClient.enterLocalPassiveMode();
// 打印服务器返回的欢迎信息
System.out.println("Connected to " + vo.getDatabaseIp() + ".");
System.out.println(ftpClient.getReplyString());
// 设置文件传输类型为二进制
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
// 上传文件
uploadFile(ftpClient, vo.getDatabaseIp(), "D:/desktop/ftp.txt");
// 下载文件
// downloadFile(ftpClient, "C:/Users/栗永斌/Desktop/新建 文本文档.txt", "C:/Users/栗永斌/Desktop/新建 文本文档.txt");
// 登出并断开连接
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
//
} }
@ -241,6 +285,39 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
} }
} }
private static void uploadFile(FTPClient ftpClient, String localFilePath, String remoteFilePath) throws IOException {
FileInputStream inputStream = new FileInputStream(new File(remoteFilePath));
try {
// 上传文件
boolean uploaded = ftpClient.storeFile(localFilePath, inputStream);
if (uploaded) {
System.out.println("File uploaded successfully.");
} else {
System.out.println("Failed to upload file.");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
inputStream.close();
}
}
// private static void downloadFile(FTPClient ftpClient, String remoteFilePath, String localFilePath) throws IOException {
// FileOutputStream outputStream = new FileOutputStream(new File(localFilePath));
//
// // 下载文件
// boolean downloaded = ftpClient.retrieveFile(remoteFilePath, outputStream);
// outputStream.close();
//
// if (downloaded) {
// System.out.println("File downloaded successfully.");
// } else {
// System.out.println("Failed to download file.");
// }
// }
@Override @Override
public List<TableVo> getTablesById(Long id) { public List<TableVo> getTablesById(Long id) {
DataDatabaseEntity dataDatabaseEntity = baseMapper.selectById(id); DataDatabaseEntity dataDatabaseEntity = baseMapper.selectById(id);

View File

@ -104,8 +104,9 @@ public enum ProductTypeEnum {
REDIS(17, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), REDIS(17, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""),
KAFKA(18, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), KAFKA(18, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""),
ES(19, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), ES(19, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""),
FTP(20, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), FTP(20, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", "ftp://example.com"),
MONGODB(21, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), MONGODB(21, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", "mongodb://{username}:{password}@{host}:{port}/{database}"),
; ;
private Integer index; private Integer index;