当前位置: 首页 > news >正文

node 与 webhdfs 交互

注意 hadoop v3 之后,webhdfs 的端口为 9870 不是 50070

请求地址举例,例如创建一个文件:host:9870/webhdfs/v1${hdfsFilePath}?op=CREATE&overwrite=true&user.name=username

node 中使用。简单封装示例

import axios from 'axios'import HdfsConfig from './config.js';
import appConfig from '../config.js';const { hdfsAttachPath, hdfsPagePdfPath, localPagePdfPath, localAttachPath } = appConfig
const { host, port, path: hdfsPath, user: hdfsUser } = HdfsConfigimport { getCurrentDateFormatted } from '../utils.js';class WebHDFSClient {constructor() {this.host = host; // host e.g http://192.168.1.1this.port = port; // 9870this.user = hdfsUser;this.baseUrl = `http://${host}:${port}${hdfsPath}`; // http://192.168.1.1:9870/webhdfs/v1this.originPagePdfPath = '' // 当天的页面快照存储路径this.originAttachPath = '' // 当天的附件文件存储路径this.initDir()}// 列出目录async listDirectory (path) {const url = `${this.baseUrl}${path}?op=LISTSTATUS&user.name=${this.user}`;try {const response = await axios.get(url);return response.data.FileStatuses.FileStatus;} catch (error) {throw new Error(`Error listing directory: ${error.message}`);}}// 创建目录async createDirectory (path) {const url = `${this.baseUrl}${path}?op=MKDIRS&user.name=${this.user}`;try {const response = await axios.put(url);return response.data;} catch (error) {throw new Error(`Error creating directory: ${error.message}`);}}// 写入文件async writeFile (path, data) {const createUrl = `${this.baseUrl}${path}?op=CREATE&user.name=${this.user}&overwrite=true`;try {await axios({method: 'PUT',url: createUrl,headers: {'Content-Type': 'application/octet-stream', // 根据你的文件类型设置// 如果需要认证,添加相应的认证 headers,如 Authorization: Basic ...},data, // 创建文件流并作为请求体发送})console.log('File written successfully');return { message: 'File written successfully' };} catch (error) {console.log(error)throw new Error(`Error writing file: ${error.message}`);}}// 读取文件async readFile (path) {const url = `${this.baseUrl}${path}?op=OPEN&user.name=${this.user}`;try {const response = await axios.get(url, { responseType: 'stream' });return response.data;} catch (error) {throw new Error(`Error reading file: ${error.message}`);}}// 初始化当天目录async initDir () {const nowDayDir = getCurrentDateFormatted()this.originPagePdfPath = `${hdfsPagePdfPath}/${nowDayDir}`this.originAttachPath = `${hdfsAttachPath}/${nowDayDir}`// 创建页面快照目录await this.createDirectory(this.originPagePdfPath)// 创建存储附件目录await this.createDirectory(this.originAttachPath)}
}export default WebHDFSClient;

使用示例:

const hdfsClient = new WebHDFSClient()
// 读取文件内容
const fileData = fs.createReadStream(filePath);
// 写入文件到 hadoop
await hdfsClient.writeFile(saveAttachPath, fileData)

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • IOC容器初始化流程
  • [大模型实战] DAMODEL云算力平台部署LLama3.1大语言模型
  • jmeter使用问题记录
  • 数据库篇--八股文学习第十九天| Redis的数据类型有哪些?;Redis是单线程的还是多线程的,为什么?;说一说Redis持久化机制有哪些
  • 2-61 基于matlab的光学干涉仿真系统
  • Nebula图数据库常用 nGQL命令
  • 《向量数据库指南》——选择、评估并优化索引
  • 谷粒商城实战笔记-126-全文检索-ElasticSearch-整合-测试保存
  • CentOS 安装Redis
  • QMI8658 - 运动唤醒(WOM)功能 - Ⅳ
  • 汽车电子中间件概述
  • LISA: Reasoning Segmentation via Large Language Model
  • 模板初阶(详解)
  • Linux驱动.之I2C,iic驱动层(二)
  • 字体识别验证码的介绍!
  • [NodeJS] 关于Buffer
  • [数据结构]链表的实现在PHP中
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • 〔开发系列〕一次关于小程序开发的深度总结
  • Android 控件背景颜色处理
  • css的样式优先级
  • gulp 教程
  • Java程序员幽默爆笑锦集
  • Linux中的硬链接与软链接
  • mongo索引构建
  • mysql innodb 索引使用指南
  • npx命令介绍
  • sessionStorage和localStorage
  • webpack入门学习手记(二)
  • 测试如何在敏捷团队中工作?
  • 目录与文件属性:编写ls
  • 使用iElevator.js模拟segmentfault的文章标题导航
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 原生 js 实现移动端 Touch 滑动反弹
  • No resource identifier found for attribute,RxJava之zip操作符
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • ## 1.3.Git命令
  • #NOIP 2014#Day.2 T3 解方程
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • #数学建模# 线性规划问题的Matlab求解
  • (+4)2.2UML建模图
  • (2)nginx 安装、启停
  • (BFS)hdoj2377-Bus Pass
  • (zhuan) 一些RL的文献(及笔记)
  • (不用互三)AI绘画工具应该如何选择
  • (南京观海微电子)——示波器使用介绍
  • (原創) 系統分析和系統設計有什麼差別? (OO)
  • (转)http协议
  • (转)人的集合论——移山之道