Carl Nolan’s ramblings on development
Updated post can be found here: http://blogs.msdn.com/b/carlnol/archive/2013/02/08/hdinsight-net-hdfs-file-access.aspx
If you grab the latest installment of Microsoft Distribution of Hadoop you will notice, in addition to the C library, a Managed C++ solution for HDFS file access. This solution now enables one to consume HDFS files from within a .Net environment.
The purpose of this post is first to ensure folks know about the new Windows HDFS Managed library (WinHdfsManaged), provided alongside the native C library, and secondly to give a few samples of its usage from C#.
Let’s start with a simple class diagram of the Win HDFS Managed library:
The main premise is that the HdfsFileSystem is your starting point, from which one can acquire a HdfsFileHandle or a HdfsFileStream. From the HdfsFileHandle you can perform operations analogous to normal HDFS file operations. From the HdfsFileStream you can perform operations one would normally expect when working with .Net Streams.
So let’s run through some sample file operations.
As in all operations one firstly needs to get a connection to the HDFS cluster. This is achieved by calling a Connect() method and specifying the host, name or IP address, and access port:
Once one has the connection one can then easily perform a directory traversal to enquire into the files and directories:
Here is a sample output created from the test application:
In addition to getting directory information one can also query on a file or directory directly:
The HdfsFileSystem class also supports other operations such as copying and moving files, file renaming, deleting files, modifying security, checking a file exists, etc. The copy and move operations support copying and moving these files between systems.
So now onto creating and reading files.
Processing HDFS files is not that dissimilar from normal .Net file operations. Once one has opened a file for reading, operations are available for operations such as reading a byte, line, or block of bytes:
The OpenFile operations support parameter overrides for the file block size and replication factors, whereas a value of zero implies the default values will be used.
The HdfsFileHandle operations are very similar:
If one wants to read the full contents of a file into a second Stream, the HdfsFileStream makes this a simple process:
There are other options available for reading the full contents of a file. The first option is to perform a ReadLine() until a null is returned, processed using a StreamReader:
Alternatively, for more efficient reading of files, one can read the blocks of data into a byte array:
Other operations that are supported are PositionalReadByte(), PositionalReadBytes(), and Seek(). These operations allow reading the contents of a file from specific positions.
One final sample worth noting is copying a HDFS file to a local file using byte reads:
The reason a chunk size is specified in this case is to sync the size being used for HDFS file access to the byte array used for writing the local file.
If one has a Stream reference one can also get the associated file information:
Also one can modify the file properties:
So now onto writing files.
As in the case for reading, writing operations are supported for writing a byte, line, and block of bytes:
The chunk size when opening a file is set to correspond to the size of the buffer used for writing the data.
As in the reading case, if one wants to copy a file from the local file system to an HDFS file one would write:
All one has to do is read, in byte chunks, data from the local file and write the corresponding bytes to the HDFS file.
Of course one can also use the CopyTo operation:
The HdfsFileHandle operations are again very similar:
A quick word is warranted on appending to a file. Although the API currently supports open files for Append, this is only supported in Hadoop version 1.0.0 and above.
The code for the managed and unmanaged libraries for HDFS file access can be found in the folder:
The download not only consists of the compiled libraries but also the full source code and sample C# application that this post is based upon. You can compile the source or just use the delivered assemblies.
One final word is warranted about environment variables.
As the C library being used by the Managed wrapper is actually calling Java code, one needs to define some additional directories in the Path and CLASSPATH environment variables.
For the Path one needs to include the following directories for the Java SDK:
For the CLASSPATH (if the environment is not defined it will need to be created) one needs to include the following directories for the Isotope JAR files:
Once configured you are good to go. If one does get a File Not Found exception then chances are that your environment variables are not configured correctly.
Happy .Net Coding!
İ cant see the download link for the project
If you RDP into the headnode you can find the source at:
I copied the .NET solution from the Hadoop cluster machine to my local machine and build. Managed Test project showed error as <hdfs.h> could not found or loaded. Therefore in WinHdfsManaged project - properties - c/c++ -General - Additional Include Libraries; I have set path to Native project -C:\kunal\Learning\Research\Hadoop\tools\WinLibHdfs\src\x64\Debug;C:\kunal\Learning\Research\Hadoop\tools\WinLibHdfs\src\WinHdfsNative
These are my local machine paths. Bold are the paths of directories took from Hadoop cluster machine. Then I got linker error as WinHdfs.lib can not be linked. I thought name should be WinHdfsNative.lib.
Therefore WinHdfsManaged project - properties - Linker-General - Additional Directory Libraries; I have added bold paths as C:\kunal\Learning\Research\Hadoop\tools\WinLibHdfs\src\WinHdfsNative\lib;C:\kunal\Learning\Research\Hadoop\tools\WinLibHdfs\src\x64\Debug;%(AdditionalLibraryDirectories)
Also WinHdfsManaged project - properties - Linker - Input - Additional Dependcies; I have renamed winhdfs.lib to winhdfsnative.lib. Then added reference of WinHdfsManaged project in WinHdfsManagedTest project by using Add reference option;
Then project was successfully compiled and EXE was generated. I copied the EXE to hadoop cluster machine. Then opened Computer-Properties-EnvironmentVariables -User variables; modified Path and CLASSPATH variable to values mentioned in the blog post at the bottom section. Please note that, I am not admin on Hadoop machine therefore not able to modify Path and CLASSPATH system environment variables.
Still I receive WinHdfrsManaged.dll not found exception. I have never worked on Interoperability and Java. I may be wrong somewhere.
Any help is appreciated!!
Check out the newsgroup posting on this exact matter:
If you still cannot get it working let me know.
I have looks your code in code blog its good but one thing i have to know how to build the Windows HDFS Managed library (WinHdfsManaged) from hadoop.
what are the steps to follow getting the hdfs managed library?.
"If you grab the latest installment of Microsoft Distribution of Hadoop you will notice, in addition to the C library,
a Managed C++ solution for HDFS file access. This solution now enables one to consume HDFS files from within a .Net environment.
The purpose of this post is first to ensure folks know about the new Windows HDFS Managed library (WinHdfsManaged), provided
alongside the native C library, and secondly to give a few samples of its usage from C#."
On the headnode have a look in the directory:
This contains a single solution that has been compiler as x64/Release.
This is the source for the WinLib library and a managed wrapper.
There is also a sample C# projects that shows how to use the library.
How to install hadoop software and get the winlibhdfs library in c:/app folder
Note: Hadoop software not like exe format.
At the moment you have to be on the Hadoop on Azure program:
Based on your knowledge i followed the several steps:
1) I Entered the azure link https://www.hadooponazure.com/
2) Inside the azure portal i have clicked the Apache Hadoop software library
3) Then entered the link http://hadoop.apache.org/
4) under the hadoop portal i clicked the link "Hadoop Distributed File System (HDFS™)":
5) Then i clicked the link "Download Hadoop from the release page".
Then i found the certains files like this
Next what step follow to build the winhdfs libraray?.
The Developer Preview for the Apache Hadoop- based Services for Windows Azure is available by invitation @ connect.microsoft.com/.../Survey.aspx. At the moment the code is onlt available when RDPing into the head node of an Azure deployment.
How many days to receive invitation code from microsoft survey.
did anyone succeed to create dll from this solution ???...
Carl can you help.