# data_collection **Repository Path**: suncc800/data_collection ## Basic Information - **Project Name**: data_collection - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-08 - **Last Updated**: 2026-08-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # data_collection —— 多源API数据采集框架 ## 技术栈 | 技术 | 说明 | |---|---| | SpringBoot 2.7 | 基础框架 | | MyBatis-Plus 3.5 | ORM,物理删除 | | Druid | 阿里连接池 | | Hutool | HTTP调用/JSON解析 | | Knife4j | 接口文档(Swagger增强) | | Lombok + Validation | 代码简化 + 参数校验 | --- ## 项目结构 ``` src/main/java/com/example/datacollection/ │ ├── DataCollectionApplication.java # 启动类 │ ├── collector/ # ★ 核心采集框架层 │ ├── api/ │ │ ├── ApiCollector.java # 采集器顶层接口(定义三步流程) │ │ ├── AbstractHttpCollector.java # HTTP采集器抽象基类(封装HTTP细节) │ │ └── CollectorRegistry.java # 采集器注册中心(自动扫描注册) │ └── impl/ │ └── WeatherCollector.java # 天气采集器(只写业务逻辑) │ ├── common/ # 公共基础设施 │ ├── result/R.java # 统一返回封装 │ ├── result/ResultCode.java # 状态码枚举 │ ├── exception/BusinessException.java # 自定义业务异常 │ └── handler/GlobalExceptionHandler.java # 全局异常处理 │ ├── config/ # 配置 │ ├── MybatisPlusConfig.java # 分页插件 │ └── SwaggerConfig.java # 接口文档 │ ├── controller/ │ ├── WeatherController.java # 天气专属接口 │ └── CollectController.java # 采集器管理(查看已注册列表) │ ├── service/ │ ├── WeatherRecordService.java │ └── impl/WeatherRecordServiceImpl.java │ ├── mapper/WeatherRecordMapper.java ├── entity/WeatherRecord.java # 实体(物理删除,无deleted字段) └── dto/WeatherCollectDTO.java ``` --- ## 采集框架核心设计 ``` 外部 API ↓ AbstractHttpCollector.fetchRaw() ← 统一处理HTTP/超时/异常/日志 ↓ XxxCollector.parse() ← 子类实现:解析业务字段 ↓ XxxCollector.save() ← 子类实现:调 Service 存库 ↓ 数据库 ``` --- ## 启动步骤 ### 1. 建表 在 `aisearchdb` 库执行 `src/main/resources/init.sql` ### 2. 启动 IDEA 打开项目 → Maven 下载依赖 → 运行 `DataCollectionApplication` ### 3. 访问接口文档 ``` http://localhost:8080/doc.html ``` --- ## 接口速查 ### 触发天气采集并入库 ```http POST http://localhost:8080/api/weather/collect Content-Type: application/json { "city": "北京", "latitude": 39.9042, "longitude": 116.4074 } ``` ### 分页查询 ``` GET http://localhost:8080/api/weather/page?pageNum=1&pageSize=10&city=北京 ``` ### 物理删除 ``` DELETE http://localhost:8080/api/weather/{id} ``` ### 查看已注册采集器 ``` GET http://localhost:8080/api/collect/list ``` --- ## 新增一个数据源(5步接入) 以新增"新闻API"为例: **1. 建表** → `init.sql` 中取消注释 `news_record` 建表语句并执行 **2. 新建实体** `entity/NewsRecord.java`(参考 WeatherRecord) **3. 新建 Mapper** `mapper/NewsRecordMapper.java`(两行代码) **4. 新建 Service + Impl** `service/NewsRecordService.java`(参考 Weather) **5. 新建采集器**: ```java @Component public class NewsCollector extends AbstractHttpCollector { @Override public String collectorName() { return "news"; } @Override protected String buildUrl(NewsCollectDTO p) { return "https://api.example.com/news?q=" + p.getKeyword(); } // 需要 API Key 时重写此方法: @Override protected Map buildHeaders(NewsCollectDTO p) { return Map.of("Authorization", "Bearer " + apiKey); } @Override public NewsRecord parse(String raw, NewsCollectDTO p) { /* 解析 JSON */ } @Override public void save(NewsRecord entity) { newsRecordService.save(entity); } } ``` **完成!** 框架自动注册,`GET /api/collect/list` 可见新采集器名称。 --- ## 常用城市经纬度 | 城市 | latitude | longitude | |---|---|---| | 北京 | 39.9042 | 116.4074 | | 上海 | 31.2304 | 121.4737 | | 广州 | 23.1291 | 113.2644 | | 深圳 | 22.5431 | 114.0579 | | 成都 | 30.5728 | 104.0668 |