[英]Use Read Stream from AWS S3 getObject to read and upload to a different bucket
[英]Javascript - Read parquet data (with snappy compression) from AWS s3 bucket
在nodeJS 中,我试图读取镶木地板文件(compression='snappy')但没有成功。
我使用https://github.com/ironSource/parquetjs npm 模块打开本地文件并读取它,但 reader.cursor() 抛出神秘错误“尚未实现”。 无论使用哪种压缩(plain、rle 或 snappy)来创建输入文件,它都会引发相同的错误。
这是我的代码:
const readParquet = async (fileKey) => {
const filePath = 'parquet-test-file.plain'; // 'snappy';
console.log('----- reading file : ', filePath);
let reader = await parquet.ParquetReader.openFile(filePath);
console.log('---- ParquetReader initialized....');
// create a new cursor
let cursor = reader.getCursor();
// read all records from the file and print them
if (cursor) {
console.log('---- cursor initialized....');
let record = await cursor.next() ; // this line throws exception
while (record) {
console.log(record);
record = await cursor.next();
}
}
await reader.close();
console.log('----- done with reading parquet file....');
return;
};
致电阅读:
let dt = readParquet(fileKeys.dataFileKey);
dt
.then((value) => console.log('--------SUCCESS', value))
.catch((error) => {
console.log('-------FAILURE ', error); // Random error
console.log(error.stack);
})
更多信息: 1. 我使用 pyarrow.parquet 在 python 中生成了我的镶木地板文件 2. 我在写入文件时使用了 'SNAPPY' 压缩 3. 我可以在 python 中读取这些文件而没有任何问题 4. 我的架构不固定(未知)每次我写镶木地板文件。 我在写作时不创建模式。 5. error.stack 在控制台打印undefined 6. console.log('-------FAILURE ', error); 打印“尚未实施”
我想知道是否有人遇到过类似的问题并有想法/解决方案可以分享。 顺便说一句,我的镶木地板文件存储在 AWS S3 位置(与此测试代码不同)。 我仍然需要找到从 S3 存储桶读取镶木地板文件的解决方案。
任何帮助、建议、代码示例将不胜感激。
使用var AWS = require('aws-sdk');
从 S3 获取数据。
然后使用node-parquet
将 parquet 文件读入变量。
import np = require('node-parquet');
// Read from a file:
var reader = new np.ParquetReader(`file.parquet`);
var parquet_info = reader.info();
var parquet_rows = reader.rows();
reader.close();
parquet_rows = parquet_rows + "\n";
这里有一个https://github.com/ironSource/parquetjs的分支: https : //github.com/ZJONSSON/parquetjs ,它是 IronSource 项目的“精简版”版本。 您可以使用npm install parquetjs-lite
安装它。
ZJONSSON 项目带有一个函数ParquetReader.openS3
,它接受一个 s3 客户端(来自 AWS SDK 的第 2 版)和参数( {Bucket: 'x', Key: 'y'}
)。 您可能想尝试看看这是否适合您。
如果您使用的是 AWS SDK / S3 客户端的第 3 版,我在这里有一个兼容的分支: https : //github.com/entitycs/parquetjs (请参阅标签功能/openS3v3)。
项目 README.md 中的示例用法:
const parquet = require("parquetjs-lite");
const params = {
Bucket: 'xxxxxxxxxxx',
Key: 'xxxxxxxxxxx'
};
// v2 example
const AWS = require('aws-sdk');
const client = new AWS.S3({
accessKeyId: 'xxxxxxxxxxx',
secretAccessKey: 'xxxxxxxxxxx'
});
let reader = await parquet.ParquetReader.openS3(client,params);
//v3 example
const {S3Client, HeadObjectCommand, GetObjectCommand} = require('@aws-sdk/client-s3');
const client = new S3Client({region:"us-east-1"});
let reader = await parquet.ParquetReader.openS3(
{S3Client:client, HeadObjectCommand, GetObjectCommand},
params
);
// 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.