Establishing Connections to filesystem

The APIs for establishing connections to filesystem and returning filesystem handles are:

The API hdfsConnectAsNewUserInstance() is not supported for connections to filesystem fileservers.

These APIs behave in the same way:

It is possible to have more than one open connection at a time. For each connection, simply return the filesystem handle to a different instance of hdfsFS, as in this example:

    //Connect to Cluster 1 (picked up from /opt/mapr/conf/mapr-clusters.conf)
        hdfsFS fs1 = hdfsConnectNewInstance("default", 7222);
    //Connect to Cluster 2
        hdfsFS fs2 = hdfsConnectNewInstance("n1c", 7222);
    //Connect to Cluster 3
        hdfsFS fs3 = hdfsConnectNewInstance("n1d", 7222);

You can then obtain file handles for files in each connected cluster, as in this example. For each cluster, this example code calls hdfsOpenFile(), passing in the handle to the filesystem, the absolute path to a file (and the file is created before being opened, if it doesn’t already exist) and a file-access flag that specifies to open the file in write-only mode. This mode truncates existing files to offset 0, deleting their content.

Ignore the last three parameters for this example. hdfsOpenFile() returns a handle to the file or an error message, if the open operation fails.

     //Create files for write operations on all clusters
        const char* writePath = "/tmp/write-file1.txt";
        hdfsFile writeFile1 = hdfsOpenFile(fs1, writePath, O_WRONLY, 0, 0, 0);
        if (!writeFile1) {
        fprintf(stderr, "Failed to open %s for writing on Cluster 1!\n", writePath);
            exit(-2);
     }
        hdfsFile writeFile2 = hdfsOpenFile(fs2, writePath, O_WRONLY, 0, 0, 0);
        if (!writeFile2) {
        fprintf(stderr, "Failed to open %s for writing on Cluster 2!\n", writePath);
            exit(-2);
     }
         hdfsFile writeFile3 = hdfsOpenFile(fs3, writePath, O_WRONLY, 0, 0, 0);
     if (!writeFile3) {
        fprintf(stderr, "Failed to open %s for writing on Cluster 3!\n", writePath);
            exit(-2);
     }
         fprintf(stderr, "Opened %s for writing successfully on all 3 clusters...\n", writePath);

After working with the files, close them and disconnect from the filesystems, as in this example:

// Close all files     
if (writeFile1)
        hdfsCloseFile(fs1, writeFile1);
if (writeFile2)
        hdfsCloseFile(fs2, writeFile2);
if (writeFile3)
        hdfsCloseFile(fs3, writeFile3);
 
// Disconnect from all clusters
hdfsDisconnect(fs1);
hdfsDisconnect(fs3);
hdfsDisconnect(fs3);