libhdfs.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <meta content="Apache Forrest" name="Generator">
  6. <meta name="Forrest-version" content="0.8">
  7. <meta name="Forrest-skin-name" content="pelt">
  8. <meta name="http-equiv" content="Content-Type">
  9. <meta name="content" content="text/html;">
  10. <meta name="charset" content="utf-8">
  11. <title>C API to HDFS: libhdfs</title>
  12. <link type="text/css" href="skin/basic.css" rel="stylesheet">
  13. <link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
  14. <link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
  15. <link type="text/css" href="skin/profile.css" rel="stylesheet">
  16. <script src="skin/getBlank.js" language="javascript" type="text/javascript"></script><script src="skin/getMenu.js" language="javascript" type="text/javascript"></script><script src="skin/fontsize.js" language="javascript" type="text/javascript"></script>
  17. <link rel="shortcut icon" href="images/favicon.ico">
  18. </head>
  19. <body onload="init()">
  20. <script type="text/javascript">ndeSetTextSize();</script>
  21. <div id="top">
  22. <!--+
  23. |breadtrail
  24. +-->
  25. <div class="breadtrail">
  26. <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://hadoop.apache.org/">Hadoop</a> &gt; <a href="http://hadoop.apache.org/core/">Core</a><script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
  27. </div>
  28. <!--+
  29. |header
  30. +-->
  31. <div class="header">
  32. <!--+
  33. |start group logo
  34. +-->
  35. <div class="grouplogo">
  36. <a href="http://hadoop.apache.org/"><img class="logoImage" alt="Hadoop" src="images/hadoop-logo.jpg" title="Apache Hadoop"></a>
  37. </div>
  38. <!--+
  39. |end group logo
  40. +-->
  41. <!--+
  42. |start Project Logo
  43. +-->
  44. <div class="projectlogo">
  45. <a href="http://hadoop.apache.org/core/"><img class="logoImage" alt="Hadoop" src="images/core-logo.gif" title="Scalable Computing Platform"></a>
  46. </div>
  47. <!--+
  48. |end Project Logo
  49. +-->
  50. <!--+
  51. |start Search
  52. +-->
  53. <div class="searchbox">
  54. <form action="http://www.google.com/search" method="get" class="roundtopsmall">
  55. <input value="hadoop.apache.org" name="sitesearch" type="hidden"><input onFocus="getBlank (this, 'Search the site with google');" size="25" name="q" id="query" type="text" value="Search the site with google">&nbsp;
  56. <input name="Search" value="Search" type="submit">
  57. </form>
  58. </div>
  59. <!--+
  60. |end search
  61. +-->
  62. <!--+
  63. |start Tabs
  64. +-->
  65. <ul id="tabs">
  66. <li>
  67. <a class="unselected" href="http://hadoop.apache.org/core/">Project</a>
  68. </li>
  69. <li>
  70. <a class="unselected" href="http://wiki.apache.org/hadoop">Wiki</a>
  71. </li>
  72. <li class="current">
  73. <a class="selected" href="index.html">Hadoop 0.19 Documentation</a>
  74. </li>
  75. </ul>
  76. <!--+
  77. |end Tabs
  78. +-->
  79. </div>
  80. </div>
  81. <div id="main">
  82. <div id="publishedStrip">
  83. <!--+
  84. |start Subtabs
  85. +-->
  86. <div id="level2tabs"></div>
  87. <!--+
  88. |end Endtabs
  89. +-->
  90. <script type="text/javascript"><!--
  91. document.write("Last Published: " + document.lastModified);
  92. // --></script>
  93. </div>
  94. <!--+
  95. |breadtrail
  96. +-->
  97. <div class="breadtrail">
  98. &nbsp;
  99. </div>
  100. <!--+
  101. |start Menu, mainarea
  102. +-->
  103. <!--+
  104. |start Menu
  105. +-->
  106. <div id="menu">
  107. <div onclick="SwitchMenu('menu_selected_1.1', 'skin/')" id="menu_selected_1.1Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Documentation</div>
  108. <div id="menu_selected_1.1" class="selectedmenuitemgroup" style="display: block;">
  109. <div class="menuitem">
  110. <a href="index.html">Overview</a>
  111. </div>
  112. <div class="menuitem">
  113. <a href="quickstart.html">Hadoop Quick Start</a>
  114. </div>
  115. <div class="menuitem">
  116. <a href="cluster_setup.html">Hadoop Cluster Setup</a>
  117. </div>
  118. <div class="menuitem">
  119. <a href="mapred_tutorial.html">Hadoop Map/Reduce Tutorial</a>
  120. </div>
  121. <div class="menuitem">
  122. <a href="commands_manual.html">Hadoop Command Guide</a>
  123. </div>
  124. <div class="menuitem">
  125. <a href="hdfs_shell.html">Hadoop FS Shell Guide</a>
  126. </div>
  127. <div class="menuitem">
  128. <a href="distcp.html">Hadoop DistCp Guide</a>
  129. </div>
  130. <div class="menuitem">
  131. <a href="native_libraries.html">Hadoop Native Libraries</a>
  132. </div>
  133. <div class="menuitem">
  134. <a href="streaming.html">Hadoop Streaming</a>
  135. </div>
  136. <div class="menuitem">
  137. <a href="hadoop_archives.html">Hadoop Archives</a>
  138. </div>
  139. <div class="menuitem">
  140. <a href="hdfs_user_guide.html">HDFS User Guide</a>
  141. </div>
  142. <div class="menuitem">
  143. <a href="hdfs_design.html">HDFS Architecture</a>
  144. </div>
  145. <div class="menuitem">
  146. <a href="hdfs_permissions_guide.html">HDFS Admin Guide: Permissions</a>
  147. </div>
  148. <div class="menuitem">
  149. <a href="hdfs_quota_admin_guide.html">HDFS Admin Guide: Quotas</a>
  150. </div>
  151. <div class="menuitem">
  152. <a href="SLG_user_guide.html">HDFS Utilities</a>
  153. </div>
  154. <div class="menupage">
  155. <div class="menupagetitle">HDFS C API</div>
  156. </div>
  157. <div class="menuitem">
  158. <a href="hod_user_guide.html">HOD User Guide</a>
  159. </div>
  160. <div class="menuitem">
  161. <a href="hod_admin_guide.html">HOD Admin Guide</a>
  162. </div>
  163. <div class="menuitem">
  164. <a href="hod_config_guide.html">HOD Config Guide</a>
  165. </div>
  166. <div class="menuitem">
  167. <a href="capacity_scheduler.html">Capacity Scheduler</a>
  168. </div>
  169. <div class="menuitem">
  170. <a href="api/index.html">API Docs</a>
  171. </div>
  172. <div class="menuitem">
  173. <a href="jdiff/changes.html">API Changes</a>
  174. </div>
  175. <div class="menuitem">
  176. <a href="http://wiki.apache.org/hadoop/">Wiki</a>
  177. </div>
  178. <div class="menuitem">
  179. <a href="http://wiki.apache.org/hadoop/FAQ">FAQ</a>
  180. </div>
  181. <div class="menuitem">
  182. <a href="releasenotes.html">Release Notes</a>
  183. </div>
  184. <div class="menuitem">
  185. <a href="changes.html">Change Log</a>
  186. </div>
  187. </div>
  188. <div id="credit"></div>
  189. <div id="roundbottom">
  190. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  191. <!--+
  192. |alternative credits
  193. +-->
  194. <div id="credit2"></div>
  195. </div>
  196. <!--+
  197. |end Menu
  198. +-->
  199. <!--+
  200. |start content
  201. +-->
  202. <div id="content">
  203. <div title="Portable Document Format" class="pdflink">
  204. <a class="dida" href="libhdfs.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  205. PDF</a>
  206. </div>
  207. <h1>C API to HDFS: libhdfs</h1>
  208. <div id="minitoc-area">
  209. <ul class="minitoc">
  210. <li>
  211. <a href="#C+API+to+HDFS%3A+libhdfs">C API to HDFS: libhdfs</a>
  212. </li>
  213. <li>
  214. <a href="#The+APIs">The APIs</a>
  215. </li>
  216. <li>
  217. <a href="#A+sample+program">A sample program</a>
  218. </li>
  219. <li>
  220. <a href="#How+to+link+with+the+library">How to link with the library</a>
  221. </li>
  222. <li>
  223. <a href="#Common+problems">Common problems</a>
  224. </li>
  225. <li>
  226. <a href="#libhdfs+is+thread+safe">libhdfs is thread safe</a>
  227. </li>
  228. </ul>
  229. </div>
  230. <a name="N10019"></a><a name="C+API+to+HDFS%3A+libhdfs"></a>
  231. <h2 class="h3">C API to HDFS: libhdfs</h2>
  232. <div class="section">
  233. <p>
  234. libhdfs is a JNI based C api for Hadoop's DFS. It provides C apis to a subset of the HDFS APIs to manipulate DFS files and the filesystem. libhdfs is part of the hadoop distribution and comes pre-compiled in ${HADOOP_HOME}/libhdfs/libhdfs.so .
  235. </p>
  236. </div>
  237. <a name="N10023"></a><a name="The+APIs"></a>
  238. <h2 class="h3">The APIs</h2>
  239. <div class="section">
  240. <p>
  241. The libhdfs APIs are a subset of: <a href="api/org/apache/hadoop/fs/FileSystem.html">hadoop fs APIs</a>.
  242. </p>
  243. <p>
  244. The header file for libhdfs describes each API in detail and is available in ${HADOOP_HOME}/src/c++/libhdfs/hdfs.h
  245. </p>
  246. </div>
  247. <a name="N10034"></a><a name="A+sample+program"></a>
  248. <h2 class="h3">A sample program</h2>
  249. <div class="section">
  250. <pre class="code">
  251. #include "hdfs.h"
  252. int main(int argc, char **argv) {
  253. hdfsFS fs = hdfsConnect("default", 0);
  254. const char* writePath = "/tmp/testfile.txt";
  255. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
  256. if(!writeFile) {
  257. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  258. exit(-1);
  259. }
  260. char* buffer = "Hello, World!";
  261. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
  262. if (hdfsFlush(fs, writeFile)) {
  263. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  264. exit(-1);
  265. }
  266. hdfsCloseFile(fs, writeFile);
  267. }
  268. </pre>
  269. </div>
  270. <a name="N1003F"></a><a name="How+to+link+with+the+library"></a>
  271. <h2 class="h3">How to link with the library</h2>
  272. <div class="section">
  273. <p>
  274. See the Makefile for hdfs_test.c in the libhdfs source directory (${HADOOP_HOME}/src/c++/libhdfs/Makefile) or something like:
  275. gcc above_sample.c -I${HADOOP_HOME}/src/c++/libhdfs -L${HADOOP_HOME}/libhdfs -lhdfs -o above_sample
  276. </p>
  277. </div>
  278. <a name="N10049"></a><a name="Common+problems"></a>
  279. <h2 class="h3">Common problems</h2>
  280. <div class="section">
  281. <p>
  282. The most common problem is the CLASSPATH is not set properly when calling a program that uses libhdfs. Make sure you set it to all the hadoop jars needed to run Hadoop itself. Currently, there is no way to programmatically generate the classpath, but a good bet is to include all the jar files in ${HADOOP_HOME} and ${HADOOP_HOME}/lib as well as the right configuration directory containing hadoop-site.xml
  283. </p>
  284. </div>
  285. <a name="N10053"></a><a name="libhdfs+is+thread+safe"></a>
  286. <h2 class="h3">libhdfs is thread safe</h2>
  287. <div class="section">
  288. <p>Concurrency and Hadoop FS "handles" - the hadoop FS implementation includes a FS handle cache which caches based on the URI of the namenode along with the user connecting. So, all calls to hdfsConnect will return the same handle but calls to hdfsConnectAsUser with different users will return different handles. But, since HDFS client handles are completely thread safe, this has no bearing on concurrency.
  289. </p>
  290. <p>Concurrency and libhdfs/JNI - the libhdfs calls to JNI should always be creating thread local storage, so (in theory), libhdfs should be as thread safe as the underlying calls to the Hadoop FS.
  291. </p>
  292. </div>
  293. </div>
  294. <!--+
  295. |end content
  296. +-->
  297. <div class="clearboth">&nbsp;</div>
  298. </div>
  299. <div id="footer">
  300. <!--+
  301. |start bottomstrip
  302. +-->
  303. <div class="lastmodified">
  304. <script type="text/javascript"><!--
  305. document.write("Last Published: " + document.lastModified);
  306. // --></script>
  307. </div>
  308. <div class="copyright">
  309. Copyright &copy;
  310. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  311. </div>
  312. <!--+
  313. |end bottomstrip
  314. +-->
  315. </div>
  316. </body>
  317. </html>