对js中的文本文件进行采样

时间:2021-05-21 13:16:09

标签: javascript fetch sampling

我想对从 api 获取的文本数据输出进行采样,因为数据很大,而且每次都是动态的。

let response = await fetch(url, options);

let testdata = await response.text()

let str = testdata.split('\n');

这里的测试数据量很大,所以我想进行抽样,然后进一步使用样本数据。 请帮忙!! 现在 str 存储了 10000000 条线,我无法绘制超过 10000000 个数据点的图形,所以我需要在这里进行采样

我发现这种方法很好

client.connect(broker, port, 3) topic = "machine/gui/0/heartbeat" client.subscribe(topic, 0) client.on_connect = on_connect client.on_message=on_message client.on_disconnect = on_disconnect client.loop_stop()

1 个答案:

答案 0 :(得分:0)

here读取,可以使用awk命令选择'from line' -> 'to line'

awk 'NR >= 57890000 && NR <= 57890010' /path/to/file

 awk 'NR < 57890000 { next } { print } NR == 57890010 { exit }' /path/to/file

通过这种方式,您可以向 fetch url 添加参数以检索您想要的内容,然后您必须将文本拆分为临时数组,以添加具有偏移索引的大文本,即请求“来自行”的编号.

bigArray = [];
const from = 200000;
const to = 250000;
let response = await fetch('urll?from='+from+'&to='+to, options);
let testdata = await response.text();  
let lines = testdata.split('\n');
let idx = 0;
lines.forEach(line => {
  bigArray[from + idx] = line;
  idx++;
}
......

但无论您的行大小如何,这都可能占用大量内存,因此也许您可以删除一些索引,例如在“从行”之前 - 100000 和在“到行”+ 100000 之后,并在您移动时重新加载数据图呈现的值。