如何使用 Typescript 读取多个镶木地板文件?

How to read several parquet files with Type Script?

我有一个包含镶木地板文件的文件夹。

如何将它们全部读取并转换成 1 个大的 txt 文件?

我正在使用 parquetjs 库读取 1 个文件:

(
    async () => {
        // create new ParquetReader that reads from 'fruits.parquet`
        let reader = await parquet.ParquetReader.openFile('fruits.parquet');

        // create a new cursor
        let cursor = reader.getCursor();

        // read all records from the file and print them
        let record = null;
        while (record = await cursor.next()) {
            console.log(record);
        }

    }

) ();

需要帮助同时读取多个文件并合并它们..

  1. aynsc 函数转换为采用 filename 参数。使函数 return 成为 record
  2. 创建一个数组 filename
  3. 使用Array.map to transform the filename array into a Promise数组
  4. 使用Promise.all等待所有文件读取完毕
  5. 使用String.join将所有record组合成一个字符串

async 函数转换为 filename

转换 async 文件以获取 filename 参数

const readFile = async(filename) => {
  let reader = await parquet.ParquetReader.openFile(filename);
  let cursor = reader.getCursor();

  let record = '';
  let currentContent = '';
  while (currentContent = await cursor.next()) {
    record += currentContent;
  }

  return record;
};

读取并合并所有文件

const filenames = ['f1.parquet', 'f2.parquet', 'f3.parquet'];
const readPromises = filenames.map(f => readFile(f));
const allPromises = Promise.all(readPromises);

// Read and combine
allPromises.then(contentsArray => contentsArray.join('\n'))
  .then(joinedContent => console.log(joinedContent))
  .catch(console.error);