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 } = HdfsConfig

import { getCurrentDateFormatted } from '../utils.js';

class WebHDFSClient {
  constructor() {
    this.host = host; // host e.g http://192.168.1.1
    this.port = port; // 9870
    this.user = hdfsUser;
    this.baseUrl = `http://${host}:${port}${hdfsPath}`; // http://192.168.1.1:9870/webhdfs/v1

    this.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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值