| | const fs = require('fs'); |
| | const path = require('path'); |
| | const { XMLParser, XMLBuilder } = require('fast-xml-parser'); |
| | const StreamArray = require('stream-json/streamers/StreamArray'); |
| | const { chain } = require('stream-chain'); |
| | const { parser } = require('stream-json'); |
| | const { pick } = require('stream-json/filters/Pick'); |
| | const { ignore } = require('stream-json/filters/Ignore'); |
| | const performanceMonitor = require('./performance'); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | class XmlJsonHandler { |
| | constructor() { |
| | this.xmlParser = new XMLParser(); |
| | this.xmlBuilder = new XMLBuilder(); |
| | } |
| |
|
| | |
| | |
| | |
| | generateXml(itemCount = 1000) { |
| | performanceMonitor.start('生成XML'); |
| | const items = []; |
| | for (let i = 0; i < itemCount; i++) { |
| | items.push({ |
| | id: i, |
| | name: `Item_${i}`, |
| | price: Math.random() * 100, |
| | description: `Description for item ${i}` |
| | }); |
| | } |
| | |
| | const xmlObj = { |
| | root: { |
| | metadata: { timestamp: Date.now() }, |
| | items: { item: items } |
| | } |
| | }; |
| |
|
| | const xmlContent = this.xmlBuilder.build(xmlObj); |
| | performanceMonitor.end('生成XML'); |
| | return xmlContent; |
| | } |
| |
|
| | |
| | |
| | |
| | parseXml(xmlContent) { |
| | performanceMonitor.start('解析XML'); |
| | const result = this.xmlParser.parse(xmlContent); |
| | |
| | const count = result.root.items.item.length; |
| | console.log(`[XML处理] 解析完成,包含 ${count} 个条目`); |
| | performanceMonitor.end('解析XML'); |
| | return result; |
| | } |
| |
|
| | |
| | |
| | |
| | async generateLargeJson(filePath, itemCount = 50000) { |
| | performanceMonitor.start('生成大JSON文件'); |
| | return new Promise((resolve, reject) => { |
| | const stream = fs.createWriteStream(filePath); |
| | stream.write('[\n'); |
| | |
| | let i = 0; |
| | const writeNext = () => { |
| | let ok = true; |
| | do { |
| | i++; |
| | const item = { |
| | id: i, |
| | data: `Data_${i}`, |
| | timestamp: Date.now(), |
| | nested: { info: `Nested info ${i}` } |
| | }; |
| | const isLast = i === itemCount; |
| | const content = JSON.stringify(item) + (isLast ? '' : ',\n'); |
| | |
| | if (isLast) { |
| | stream.write(content); |
| | stream.write('\n]'); |
| | stream.end(); |
| | } else { |
| | ok = stream.write(content); |
| | } |
| | } while (i < itemCount && ok); |
| |
|
| | if (i < itemCount) { |
| | stream.once('drain', writeNext); |
| | } |
| | }; |
| | |
| | writeNext(); |
| |
|
| | stream.on('finish', () => { |
| | performanceMonitor.end('生成大JSON文件'); |
| | console.log(`[JSON处理] 已生成大JSON文件: ${filePath}`); |
| | resolve(); |
| | }); |
| | stream.on('error', reject); |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | async processLargeJsonStream(filePath) { |
| | performanceMonitor.start('流式处理大JSON'); |
| | |
| | return new Promise((resolve, reject) => { |
| | let counter = 0; |
| | |
| | |
| | const pipeline = chain([ |
| | fs.createReadStream(filePath), |
| | parser(), |
| | StreamArray.streamArray(), |
| | ]); |
| |
|
| | pipeline.on('data', (data) => { |
| | |
| | counter++; |
| | |
| | if (counter % 10000 === 0) { |
| | |
| | } |
| | }); |
| |
|
| | pipeline.on('end', () => { |
| | console.log(`[JSON流] 处理完成,共计 ${counter} 条记录`); |
| | performanceMonitor.end('流式处理大JSON'); |
| | resolve(); |
| | }); |
| |
|
| | pipeline.on('error', (err) => { |
| | console.error('[JSON流] 处理出错:', err); |
| | reject(err); |
| | }); |
| | }); |
| | } |
| | } |
| |
|
| | module.exports = new XmlJsonHandler(); |
| |
|