Tôi có nhiệm vụ phân tích một tập dữ liệu khổng lồ gồm các tệp nhật ký. Khi bạn mở tệp trong Excel, nó sẽ đóng băng máy tính xách tay của bạn. Do giới hạn của các công cụ có sẵn, tôi cố gắng phân tích cú pháp tệp bằng nút. tập lệnh js
Vấn đề. Để đọc một tệp nhỏ, bạn có thể sử dụng tập lệnh bên dưới
var fs = require['fs'];
fs.readFile['path/mySmallFile.txt', 'utf-8', [err, data] => {
if [err] {
throw err;
}
console.log[data];
}]
Sau đó, bạn sẽ có thể đọc nội dung tệp nhỏ này. Tuy nhiên, khi kích thước tệp lớn, bạn sẽ gặp lỗi với bộ đệm. Chẳng hạn như RangeError. Cố gắng phân bổ Bộ đệm lớn hơn kích thước tối đa. Quá trình thực thi sẽ dừng lại khi có lỗi;
Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
Dung dịch. Để đọc tệp lớn, bạn có thể nhập thư viện gốc cho readline
var fs = require['fs'];
var readline = require['readline'];
const rl = readline.createInterface[{
input: fs.createReadStream['path/largeFile.csv'],
output: process.stdout,
terminal:false
}]
rl.on['line', [line] => {
console.log[line];
}]
rl.on['pause', [] => {
console.log['Done!'];
}]
Thay thế đường dẫn tệp bằng đường dẫn của bạn đến tệp lớn để xử lý. Bạn có thể xử lý từng dòng tệp bên trong hàm on[‘line’], chẳng hạn như phân tích cú pháp thành json và tăng bộ đếm. Tổng cuối cùng có thể được hiển thị tại chức năng bật ['tạm dừng'] sau khi đọc xong tệp
Bây giờ bạn sẽ có thể xử lý tập dữ liệu lớn với nodejs. Để biết thêm thông tin, vui lòng đọc tài liệu chính thức. https. //nodejs. org/api/readline. html
Có kinh nghiệm phát triển phần mềm, kiến trúc ứng dụng và triển khai các giải pháp đám mây cho khách hàng doanh nghiệp. Kỹ năng thực hành vững chắc với bằng Thạc sĩ Khoa học Máy tính và sự nhạy bén trong kinh doanh với bằng thạc sĩ quản trị kinh doanh [MBA] về Tài chính. Được chứng nhận về Amazon Web Services [AWS], Google Cloud Platform [GCP], Microsoft Azure, Kubernetes [CKA, CKAD, CKS, KCNA] và Scrum [PSM, PSPO] với kinh nghiệm xây dựng các sản phẩm ngân hàng từ đầu. Kết nối trên Linkedin
Nút. js là môi trường thời gian chạy JavaScript mã nguồn mở và đa nền tảng, cũng có thể được sử dụng để đọc từ tệp và ghi vào tệp có thể ở định dạng txt, ods, xlsx, docx, v.v.
Ví dụ sau đây trình bày cách một tệp excel [. xlsx] được đọc từ tệp excel, sau đó được chuyển đổi thành JSON và cũng để ghi vào tệp đó. Nó có thể đạt được bằng cách sử dụng gói có tên xlsx để đạt được mục tiêu của chúng tôi
Cài đặt mô-đun. Bạn có thể cài đặt mô-đun xlsx bằng lệnh sau
npm install xlsx
Ghi chú. Ví dụ sau, văn bản. xlsx là một tệp dữ liệu giả đã được sử dụng
tên tệp. kiểm tra. xlsx
tờ 1
tờ 2
Vậy file excel test. xlsx có 2 sheet, một sheet có thông tin Sinh viên và một sheet có thông tin giảng viên
Đọc tên tệp hoạt động. đọc. js
Javascript
npm install xlsx1
npm install xlsx2______33
npm install xlsx4
npm install xlsx5
npm install xlsx6
const sheets = file.SheetNames // Here the value of the sheets will be 20
const sheets = file.SheetNames // Here the value of the sheets will be 21
npm install xlsx4
npm install xlsx5
const sheets = file.SheetNames // Here the value of the sheets will be 24
npm install xlsx5
const sheets = file.SheetNames // Here the value of the sheets will be 26
npm install xlsx5
const sheets = file.SheetNames // Here the value of the sheets will be 28
const sheets = file.SheetNames // Here the value of the sheets will be 29
Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
00 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
01 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
02 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
03 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
04 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
01 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
06 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
07 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
08 Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
01 var fs = require['fs'];
var readline = require['readline'];
const rl = readline.createInterface[{
input: fs.createReadStream['path/largeFile.csv'],
output: process.stdout,
terminal:false
}]
rl.on['line', [line] => {
console.log[line];
}]
rl.on['pause', [] => {
console.log['Done!'];
}]
00 var fs = require['fs'];
var readline = require['readline'];
const rl = readline.createInterface[{
input: fs.createReadStream['path/largeFile.csv'],
output: process.stdout,
terminal:false
}]
rl.on['line', [line] => {
console.log[line];
}]
rl.on['pause', [] => {
console.log['Done!'];
}]
01npm install xlsx5
var fs = require['fs'];
var readline = require['readline'];
const rl = readline.createInterface[{
input: fs.createReadStream['path/largeFile.csv'],
output: process.stdout,
terminal:false
}]
rl.on['line', [line] => {
console.log[line];
}]
rl.on['pause', [] => {
console.log['Done!'];
}]
03 var fs = require['fs'];
var readline = require['readline'];
const rl = readline.createInterface[{
input: fs.createReadStream['path/largeFile.csv'],
output: process.stdout,
terminal:false
}]
rl.on['line', [line] => {
console.log[line];
}]
rl.on['pause', [] => {
console.log['Done!'];
}]
04Giải trình. Đầu tiên, mô-đun npm được bao gồm trong phần đọc. js và sau đó tệp excel được đọc vào sổ làm việc tôi. tệp hằng số e trong chương trình trên
Số lượng tệp trong tệp excel cụ thể đó có sẵn trong thuộc tính SheetNames của sổ làm việc. Nó có thể được truy cập như sau
const sheets = file.SheetNames // Here the value of the sheets will be 2
Một vòng lặp for được chạy cho đến khi kết thúc tệp excel bắt đầu từ trang đầu tiên. Một trong những hàm quan trọng nhất được sử dụng trong đoạn mã trên là hàm sheet_to_json[] có trong mô-đun utils của gói xlsx. Nó chấp nhận một đối tượng trang tính làm tham số và trả về một mảng các đối tượng JSON
Có một vòng lặp forEach lặp qua mọi đối tượng JSON có trong mảng tạm thời và đẩy nó vào một biến data chứa tất cả dữ liệu ở định dạng JSON
Cuối cùng, dữ liệu được in hoặc bất kỳ sửa đổi nào khác có thể được thực hiện trên mảng các đối tượng JSON
Bước chạy ứng dụng
Chạy đọc. js bằng lệnh sau
Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
0đầu ra
Thao tác ghi Trong ví dụ sau, chúng ta sẽ chuyển đổi một mảng các đối tượng JSON thành một trang tính excel và nối nó vào tệp
tên tệp. viết. js
Javascript
var fs = require['fs'];
var readline = require['readline'];
const rl = readline.createInterface[{
input: fs.createReadStream['path/largeFile.csv'],
output: process.stdout,
terminal:false
}]
rl.on['line', [line] => {
console.log[line];
}]
rl.on['pause', [] => {
console.log['Done!'];
}]
05npm install xlsx2______33
npm install xlsx4
npm install xlsx5
npm install xlsx6
const sheets = file.SheetNames // Here the value of the sheets will be 20
const sheets = file.SheetNames // Here the value of the sheets will be 21
npm install xlsx4
npm install xlsx5
npm install xlsx15
npm install xlsx16
npm install xlsx17
npm install xlsx18
npm install xlsx19
npm install xlsx20
npm install xlsx17
npm install xlsx22
npm install xlsx17
npm install xlsx24
npm install xlsx25
npm install xlsx20
npm install xlsx17
npm install xlsx28
npm install xlsx29
Error: "toString" failed
at stringSlice [buffer.js]
at Buffer.toString[buffer.js]
at FSReqWrap.readFileAfterClose [as oncomplete]
00npm install xlsx17
npm install xlsx18
npm install xlsx33
npm install xlsx20
npm install xlsx17
npm install xlsx36
npm install xlsx17
npm install xlsx24
npm install xlsx39
npm install xlsx20
npm install xlsx17
npm install xlsx42
npm install xlsx43
npm install xlsx5
npm install xlsx45
npm install xlsx5
npm install xlsx47
npm install xlsx48
npm install xlsx4
npm install xlsx5
npm install xlsx51
_______352____41____34
Giải trình. Ở đây chúng ta có một mảng các đối tượng JSON được gọi là student_data. Chúng tôi sử dụng hai chức năng chính trong chương trình này i. e json_to_sheet[] chấp nhận một mảng đối tượng và chuyển đổi chúng thành một trang tính và một hàm khác là book_append_sheet[] để nối thêm trang tính vào sổ làm việc
Cuối cùng, tất cả các thay đổi được ghi vào test. xlsx sử dụng hàm writeFile[] lấy sổ làm việc và tệp excel làm tham số đầu vào