当前位置: 首页 > news >正文

MapReduce清洗数据 -

通过爬虫爬取的数据保存为JSON格式,然后将JSON数据清洗,得到想要的数据

package com.example.jingd;import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;public class CommentCleanerSimple {public static void main(String[] args) {String inputFilePath = "src/main/resources/jd_comments_raw_20251026_135759.json";String outputFilePath = "target/cleaned_comments.csv";ObjectMapper objectMapper = new ObjectMapper();try {// 确保输出目录存在Files.createDirectories(Paths.get("target"));// 创建输入输出流BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath));// 写入CSV标题行writer.write("用户名,评论ID,评分,评论日期,评论内容,用户等级,购买型号,购买次数,购买信息,产品规格\n");String line;while ((line = reader.readLine()) != null) {try {// 解析JSON数据JsonNode jsonNode = objectMapper.readTree(line);// 提取需要的字段String userNickName = jsonNode.has("userNickName") ? jsonNode.get("userNickName").asText() : "";String commentId = jsonNode.has("commentId") ? jsonNode.get("commentId").asText() : "";String commentScore = jsonNode.has("commentScore") ? jsonNode.get("commentScore").asText() : "";String commentDate = jsonNode.has("commentDate") ? jsonNode.get("commentDate").asText() : "";// 处理评论内容:移除特殊字符、换行符,将英文逗号替换为中文逗号String commentData = "";if (jsonNode.has("commentData")) {// 简单直接的字符串处理方式commentData = jsonNode.get("commentData").asText().replace('\n', ' ')  // 替换换行符为空格.replace('\r', ' ')  // 替换回车符为空格.replace(',', ',')  // 将英文逗号替换为中文逗号.replaceAll("[\\\\/\\*\\(\\)\\[\\]\\{\\}\\^\\$\\|\\?\\+\\.\\<\\>\\!\\@\\#\\%\\^\\&\\*]", "")  // 移除常见特殊字符.trim();  // 去除首尾空白}// 提取用户等级String userLevel = jsonNode.has("officerLevel") ? jsonNode.get("officerLevel").asText() : "";// 提取购买型号(从wareAttribute数组中获取)String productModel = "";if (jsonNode.has("wareAttribute") && jsonNode.get("wareAttribute").isArray()) {for (JsonNode attr : jsonNode.get("wareAttribute")) {if (attr.has("型号")) {productModel = attr.get("型号").asText();break;}}}// 提取购买次数String buyCount = jsonNode.has("buyCount") ? jsonNode.get("buyCount").asText() : "";// 提取购买信息文本String buyCountText = jsonNode.has("buyCountText") ? jsonNode.get("buyCountText").asText().replaceAll(",", ",") : "";// 提取产品规格信息String productSpecifications = jsonNode.has("productSpecifications") ? jsonNode.get("productSpecifications").asText().replaceAll(",", ",") : "";// 构建CSV行String csvLine = String.join(",", userNickName, commentId, commentScore, commentDate, commentData,userLevel, productModel, buyCount, buyCountText, productSpecifications);writer.write(csvLine + "\n");} catch (Exception e) {System.err.println("解析JSON失败: " + e.getMessage());e.printStackTrace();}}reader.close();writer.close();System.out.println("数据清洗完成!结果保存在: " + outputFilePath);} catch (IOException e) {System.err.println("文件操作失败: " + e.getMessage());e.printStackTrace();}}
}

然后得到.csv数据文件
image

http://www.jsqmd.com/news/26170/

相关文章:

  • 2025年10月308nm准分子设备排名:专业评测与选择指南
  • 2025年10月上海疤痕医院推荐榜单:五家机构综合对比分析
  • 2025-10-28 aoao Round 赛后总结
  • 见过哪些醍醐灌顶的Java代码:从卧槽到原来如此的顿悟
  • 2025年10月308nm准分子推荐榜:五款设备权威对比与选择指南
  • 2025年靠谱的六氟化硫断路器厂家推荐及选购参考榜
  • 2025年10月上海胎记医院推荐榜单:权威机构综合对比分析
  • 2025年评价高的欧式分支箱厂家最新推荐权威榜
  • 2025年10月上海胎记医院评价指南:从资质到服务的全面对比分析
  • 2025年10月上海胎记医院选择排行:基于真实用户反馈的实用指南
  • 2025年比较好的防静电泡棉厂家推荐及选择参考
  • 2025年10月上海血管瘤医院评测指南:口碑排行与选择建议
  • 2025年10月上海血管瘤医院推荐榜单:五家机构综合对比分析
  • 2025年10月上海血管瘤医院评价推荐:专业对比与选择指南
  • 2025年质量好的生涯规划测评系统行业口碑排行榜
  • 炒股境界
  • P5434 有标号荒漠计数
  • 2025年热门的陕西法兰阀门厂家推荐及采购指南
  • 2025年热门的隧道炉厂家最新推荐权威榜
  • 使用 Word 模板占位符生成文档的技术方案实践
  • 35435
  • 2025年热门的指纹家用防盗智能门锁TOP实力厂家推荐榜
  • 454554
  • 2025年热门的护理床厂家厂家最新推荐排行榜
  • 2025年化工设备公司行业洞察与排名前十榜单
  • 2025汽车传动轴钢管品牌综合评测与排名前十
  • 2025年口碑好的压力容器公司排名前十
  • 2025年口碑好的压力容器品牌前十强排名
  • 2025年口碑好的压力容器品牌前十强排行榜
  • 2025年钢结构品牌综合评测:江苏华力钢结构领跑行业