Carlota
Carlota

Reputation: 1237

Node.js and HDFS

I need to read information about node.js and HDFS. I work with Centos. I have a file in HDFS and I want to read and print it in console. I wrote a node.js program to write a file and it works. But when I want to print an HDFS file, it doesn't work.

Here is my code:

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());
}); 

Could anybody provide me with information on HDFS and node.js?

Upvotes: 0

Views: 4258

Answers (1)

Havnar
Havnar

Reputation: 2628

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
});

}

Upvotes: 1

Related Questions