Big Data Learning--java code implements read, append, write operations on HDFs files
Last Update:2018-07-14
Source: Internet
Author: User
<span id="Label3"></p><p><p>In the previous session of the HDFs building process, the next learning is through the Java Code implementation of the HDFs file Operation.</p></p><p><p>This part of the study is to operate the HDFs file later in the Mapredce Used.</p></p><p><p>Write Java code on eclipse to manipulate files on HDFs first, you will import the jar packages in your Project. Here we build a MAVEN project to facilitate the import of Jar Packages.</p></p><p><p>It is important to note that importing a jar package that does not exist locally when using mave needs to be done in a network environment</p></p><p><p>Here we import the jar package with the following parts, configure the following sections in pom.xml, will automatically download the relevant jar package</p></p><pre><pre><dependencies> <dependency> <groupId>org.apache.hadoop</groupId> < artifactid>hadoop-common</artifactid> <version>2.7.3</version> </dependency > <dependency> <groupId>org.apache.hadoop</groupId> <artifactId> hadoop-client</artifactid> <version>2.7.3</version> </dependency> < dependency> <groupId>jdk.tools</groupId> <artifactid>jdk.tools</artifactid > <version>1.8</version> <scope>system</scope> <systempath>${ java_home}/lib/tools.jar</systempath> </dependency> </dependencies></pre></pre><p><p></p></p><p><p>Set up the Hdfsclient class and do the Following:</p></p><p><p>Write: Write operation, automatically create file and write data when file does not exist</p></p><p><p>Append: Append write operation, write file needs to exist</p></p><p><p>Read: Reading file operation</p></p><p><p>The code is as Follows:</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">java.io.BufferedReader;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">java.io.IOException;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">java.io.InputStreamReader;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">org.apache.hadoop.conf.Configuration;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">org.apache.hadoop.fs.FSDataInputStream;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">org.apache.hadoop.fs.FSDataOutputStream;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">org.apache.hadoop.fs.FileSystem;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">org.apache.hadoop.fs.Path;</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">hdfsclient {</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">main (string[] Args) {write (); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Append ();</span></span><span style="color: #000000"><span style="color: #000000">Read (); } </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">The main write file must be an existing file</span></span> <span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">Append () {Configuration conf</span></span>=<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">Configuration (); Conf.set (</span></span>"fs.defaultfs", "hdfs://192.168.98.141:9000"<span style="color: #000000"><span style="color: #000000">); Conf.set (</span></span>"dfs.support.append", "true"<span style="color: #000000"><span style="color: #000000">); Conf.set (</span></span>"dfs.client.block.write.replace-datanode-on-failure.policy", "never");<span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Configure Permissions</span></span>Conf.set ("dfs.client.block.write.replace-datanode-on-failure.enable", "true");<span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Configure Permissions</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Try</span></span><span style="color: #000000"><span style="color: #000000">{FileSystem FS</span></span>=<span style="color: #000000"><span style="color: #000000">Filesystem.get (conf); Fsdataoutputstream Outpustream</span></span>= Fs.append (<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Path ("/hp/abc.txt"));<span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">fsdataoutputstream file output stream</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Append file</span></span>Outpustream.writebytes ("xxxxxx");<span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Append Content</span></span><span style="color: #000000"><span style="color: #000000">Outpustream.close (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(ioexception E) {</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">TODO auto-generated Catch block</span></span><span style="color: #000000"><span style="color: #000000">E.printstacktrace (); } } </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Write File</span></span> <span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">Write () {Configuration conf</span></span>=<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">Configuration (); Conf.set (</span></span>"fs.defaultfs", "hdfs://192.168.98.141:9000"<span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #0000ff"><span style="color: #0000ff">Try</span></span><span style="color: #000000"><span style="color: #000000">{FileSystem FS</span></span>=<span style="color: #000000"><span style="color: #000000">Filesystem.get (conf); Fsdataoutputstream OutputStream</span></span>= Fs.create (<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Path ("/hp/abc.txt"<span style="color: #000000"><span style="color: #000000">)); Outputstream.writebytes (</span></span>"abcde"<span style="color: #000000"><span style="color: #000000">); Outputstream.close (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(ioexception E) {</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">TODO auto-generated Catch block</span></span><span style="color: #000000"><span style="color: #000000">E.printstacktrace (); } } </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">Read () {</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Configuration Objects</span></span>Configuration conf=<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">Configuration (); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">setting Namenode in the Configuration object</span></span>Conf.set ("fs.defaultfs", "hdfs://192.168.98.141:9000"<span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #0000ff"><span style="color: #0000ff">Try</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Create FileSystem</span></span>FileSystem fs=<span style="color: #000000"><span style="color: #000000">Filesystem.get (conf); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">wear piece input stream</span></span>Fsdatainputstream InputStream = Fs.open (<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Path ("/hp/abc.txt"<span style="color: #000000"><span style="color: #000000">)); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">stream of Byte-flow characters</span></span>InputStreamReader isr=<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">InputStreamReader (inputstream); BufferedReader BR</span></span>=<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">BufferedReader (isr); String Str</span></span>=<span style="color: #000000"><span style="color: #000000">Br.readline (); </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> while</span>(str!=<span style="color: #0000ff"><span style="color: #0000ff">NULL</span></span><span style="color: #000000"><span style="color: #000000">) {System.out.println (str); STR</span></span>=<span style="color: #000000"><span style="color: #000000">Br.readline (); } br.close (); Isr.close (); Inputstream.close (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(ioexception E) {</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">TODO auto-generated Catch block</span></span><span style="color: #000000"><span style="color: #000000">E.printstacktrace (); } }}</span></span></pre><p><p>Note: This can not write the Append method used at the same time will report errors, specifically how to resolve and why I did not understand</p></p><p><p>Big Data Learning--java code implements read, append, write operations on HDFs files</p></p></span>