0

我需要阅读有关 node.js 和 HDFS 的信息。我与 Centos 一起工作。我在 HDFS 中有一个文件,我想在控制台中读取和打印它。我写了一个 node.js 程序来写一个文件,它可以工作。但是当我想打印 HDFS 文件时,它不起作用。

这是我的代码:

var WebHDFS = require('webhdfs');

var hdfs = WebHDFS.createClient({
    user: 'webuser',
    host: 'localhost',
    port: 80,
    path: '/user/cloudera/consultaBicing/numerobicis'
});

var fs = require('fs');

fs.readFile('/home/cloudera/proyecto/nodejs/node-v0.10.17/node_modules/express/prueba.txt',bar)

function bar(err,data) {
   err ? Function("error","throw error") (err) :console.log(data.toString());
}

hdfs.createReadStream('hdfs://localhost:8020/user/cloudera/consultaBicing/numerobicis', function(err, data){
    if(err) {
        return console.log(err);
    };
    console.log(data.toString());
}); 


hdfs.readFile('hdfs://localhost:8020/user/cloudera/consultaBicing/numerobicis', function(err,dat    a) {
    if (err) {
        return console.log(err)
    };

    console.log('imprimiendo');
    console.log(data.toString());
}); 

有人可以向我提供有关 HDFS 和 node.js 的信息吗?

4

1 回答 1

1

using the build in functions from the module you can do this:

note: the URL location is the location on HDFS not the local file path

var fileLocationURL = '/user/cloudera/orotherpath/textfileonHDFS.txt';
var WebHDFS = require('webhdfs');
var hdfs = WebHDFS.createClient({
  user: 'webuser',
  host: 'localhost',
  port: 50070,
  path: '/webhdfs/v1'
});

readHDFSFile(fileLocationURL);


function readHDFSFile(locationOfHDFSFile){


var remoteFileStream = hdfs.createReadStream(locationOfHDFSFile);

remoteFileStream.on('error', function onError (err) {
  // Do something with the error
console.log(err);
});

remoteFileStream.on('data', function onChunk (chunk) {
  // Do something with the data chunk
console.log(chunk.toString());
});

remoteFileStream.on('finish', function onFinish () {
  // Upload is done
});

}
于 2014-08-13T09:32:40.010 回答