You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Node.js实现SQLite文件每日存储到Hadoop(含调度)

实现Node.js上传SQLite文件到HDFS并每日定时执行

一、前提准备

确保你的Hadoop集群开启了WebHDFS服务(默认端口50070,可通过Hadoop配置文件修改),且Node.js环境已安装。

二、上传SQLite文件到HDFS的Node.js实现

方案1:使用webhdfs npm包(推荐,封装好的API)

  1. 安装依赖:
npm install webhdfs
  1. 编写上传代码:
const WebHDFS = require('webhdfs');
const fs = require('fs');

// 配置WebHDFS连接信息
const hdfs = WebHDFS.createClient({
  user: 'hadoop', // HDFS操作的用户名,根据集群配置修改
  host: 'your-namenode-ip', // NameNode的IP地址
  port: 50070, // WebHDFS默认端口
  path: '/webhdfs/v1'
});

// 定义上传函数
function uploadSqliteToHdfs(localFilePath, hdfsFilePath) {
  return new Promise((resolve, reject) => {
    const readStream = fs.createReadStream(localFilePath);
    const writeStream = hdfs.createWriteStream(hdfsFilePath);

    readStream.pipe(writeStream);

    writeStream.on('finish', () => {
      console.log(`SQLite文件已成功上传到HDFS路径:${hdfsFilePath}`);
      resolve();
    });

    writeStream.on('error', (err) => {
      console.error('上传失败:', err);
      reject(err);
    });
  });
}

// 调用上传示例(SQLite文件路径按实际情况修改)
// 给HDFS文件加日期后缀避免覆盖
const today = new Date().toISOString().split('T')[0];
uploadSqliteToHdfs('./data/daily.db', `/user/hadoop/sqlite/${today}.db`)
  .catch(err => process.exit(1));

方案2:直接调用WebHDFS REST API(无需额外包)

如果不想用第三方包,可通过axios发送请求:

  1. 安装axios:
npm install axios
  1. 上传代码:
const axios = require('axios');
const fs = require('fs');

async function uploadViaWebHdfs(localFilePath, hdfsFilePath) {
  const hdfsApiUrl = `http://your-namenode-ip:50070/webhdfs/v1${hdfsFilePath}?op=CREATE&user.name=hadoop&overwrite=true`;

  try {
    // 获取上传重定向地址
    const redirectResp = await axios.get(hdfsApiUrl, { maxRedirects: 0 });
    const uploadUrl = redirectResp.headers.location;

    // 上传文件流
    const fileStream = fs.createReadStream(localFilePath);
    await axios.put(uploadUrl, fileStream, {
      headers: { 'Content-Type': 'application/octet-stream' }
    });

    console.log(`文件上传成功:${hdfsFilePath}`);
  } catch (err) {
    console.error('上传失败:', err.message);
    throw err;
  }
}

// 调用示例
const today = new Date().toISOString().split('T')[0];
uploadViaWebHdfs('./data/daily.db', `/user/hadoop/sqlite/${today}.db`)
  .catch(err => process.exit(1));

三、实现每日定时上传

使用node-schedule包实现定时任务:

  1. 安装依赖:
npm install node-schedule
  1. 编写定时任务代码:
const schedule = require('node-schedule');
const uploadSqliteToHdfs = require('./your-upload-code-file').uploadSqliteToHdfs; // 引入上传函数

// 配置每日凌晨2点执行上传任务
const job = schedule.scheduleJob('0 0 2 * * *', async () => {
  console.log('开始执行每日SQLite文件上传任务...');
  const today = new Date().toISOString().split('T')[0];
  try {
    await uploadSqliteToHdfs('./data/daily.db', `/user/hadoop/sqlite/${today}.db`);
    console.log('每日上传任务执行完成');
  } catch (err) {
    console.error('每日上传任务失败:', err);
    // 可添加告警逻辑,比如邮件、消息通知
  }
});

console.log('每日定时上传任务已启动,将在每日凌晨2点执行');

四、注意事项

  • 权限问题:确保HDFS目标路径对配置的user有写入权限,否则会上传失败。
  • 文件命名:给HDFS文件加日期后缀,避免每日文件相互覆盖。
  • 错误处理:生产环境需添加完善的日志记录(如用winston库),上传失败时触发告警。
  • 安全认证:若集群开启Kerberos认证,需额外配置认证信息,建议使用Hadoop官方Node.js客户端适配。

内容的提问来源于stack exchange,提问作者Ankush Rj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 10:40:16