hadoop2.6.0部署好環境後寫的一個簡單的test

實用maven創建java工程,引入hadoop的依賴

[html] view plaincopy
  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>junit</groupId>  
  4.         <artifactId>junit</artifactId>  
  5.         <version>3.8.1</version>  
  6.         <scope>test</scope>  
  7.     </dependency>  
  8.     <dependency>  
  9.         <groupId>org.apache.hadoop</groupId>  
  10.         <artifactId>hadoop-hdfs</artifactId>  
  11.         <version>2.6.0</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.         <groupId>org.apache.hadoop</groupId>  
  15.         <artifactId>hadoop-client</artifactId>  
  16.         <version>2.6.0</version>  
  17.     </dependency>  
  18.     <dependency>  
  19.         <groupId>org.apache.hadoop</groupId>  
  20.         <artifactId>hadoop-mapreduce-examples</artifactId>  
  21.         <version>2.6.0</version>  
  22.         <type>pom</type>  
  23.     </dependency>  
  24. </dependencies>  

另外給main方法設置成jar包入口

[html] view plaincopy
  1. <build>  
  2.     <finalName>${project.artifactId}</finalName>  
  3.     <plugins>  
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-shade-plugin</artifactId>  
  7.             <version>1.4</version>  
  8.             <executions>  
  9.                 <execution>  
  10.                     <phase>package</phase>  
  11.                     <goals>  
  12.                         <goal>shade</goal>  
  13.                     </goals>  
  14.                     <configuration>  
  15.                         <transformers>  
  16.                             <transformer  
  17.                                 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  18.                                 <mainClass>my.hadoopstudy.dfs.Test</mainClass>  
  19.                             </transformer>  
  20.                         </transformers>  
  21.                     </configuration>  
  22.                 </execution>  
  23.             </executions>  
  24.         </plugin>  
  25.         <plugin>  
  26.             <groupId>org.apache.maven.plugins</groupId>  
  27.             <artifactId>maven-source-plugin</artifactId>  
  28.             <version>2.1.2</version>  
  29.             <configuration>  
  30.                 <attach>true</attach>  
  31.             </configuration>  
  32.             <executions>  
  33.                 <execution>  
  34.                     <phase>compile</phase>  
  35.                     <goals>  
  36.                         <goal>jar</goal>  
  37.                     </goals>  
  38.                 </execution>  
  39.             </executions>  
  40.         </plugin>  
  41.     </plugins>  
  42. </build>  
然後去寫test這個類

[java] view plaincopy
  1. package my.hadoopstudy.dfs;  
  2.    
  3. import java.io.BufferedReader;  
  4. import java.io.FilterInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.net.URI;  
  8.   
  9. import org.apache.hadoop.conf.Configuration;  
  10. import org.apache.hadoop.fs.FSDataOutputStream;  
  11. import org.apache.hadoop.fs.FileStatus;  
  12. import org.apache.hadoop.fs.FileSystem;  
  13. import org.apache.hadoop.fs.Path;  
  14. import org.slf4j.Logger;  
  15. import org.slf4j.LoggerFactory;  
  16.    
  17. public class Test {  
  18.   
  19.     private static final Logger LOGGER = LoggerFactory.getLogger(Test.class);  
  20.     public static void main(String[] args) {  
  21.         String uri = "hdfs://192.168.184.128:9000/";  
  22.         Configuration config = new Configuration();  
  23.         FileSystem fs = null;  
  24.         try {  
  25.             fs = FileSystem.get(URI.create(uri), config);  
  26.         } catch (IOException e) {  
  27.             LOGGER.error("get url "+ uri+"error" + e);  
  28.         }  
  29.    
  30.         // 列出hdfs上/user/fkong/目錄下的所有文件和目錄  
  31.         FileStatus[] statuses = null;  
  32.         try {  
  33.             statuses = fs.listStatus(new Path("/usr/local"));  
  34.         } catch (Exception e) {  
  35.             LOGGER.error("listStatus  /usr/local error" + e);  
  36.         }  
  37.         for (FileStatus status : statuses) {  
  38.             LOGGER.info(status + "" );  
  39.         }  
  40.    
  41.         // 在hdfs的/user/fkong目錄下創建一個文件,並寫入一行文本  
  42.         FSDataOutputStream os = null;  
  43.         try {  
  44.             os = fs.create(new Path("/usr/local/test.log"));  
  45.             os.write("Hello World!".getBytes());  
  46.             os.flush();  
  47.         } catch (Exception e) {  
  48.             LOGGER.error("create /usr/local/test.log error " + e);  
  49.         }  
  50.         try {  
  51.             os.flush();  
  52.             if (os != null) {  
  53.                 os.close();  
  54.             }  
  55.         } catch (IOException e) {  
  56.             LOGGER.error("close /usr/local/test.log error " + e);  
  57.         }  
  58.    
  59.         // 顯示在hdfs的/user/fkong下指定文件的內容  
  60.         FilterInputStream is = null;  
  61.         BufferedReader bufferedReader = null;  
  62.         InputStreamReader isr = null;  
  63.         try {  
  64.             is = fs.open(new Path("/usr/local/test.log"));  
  65.             isr = new InputStreamReader(is, "UTF-8");  
  66.             bufferedReader = new BufferedReader(isr);  
  67.             String content = null;  
  68.             while ((content = bufferedReader.readLine()) != null) {  
  69.                 LOGGER.info("content:" + content);  
  70.             }  
  71.         } catch (Exception e) {  
  72.             LOGGER.error("open /usr/local/test.log error " + e);  
  73.         }  
  74.         LOGGER.info("成功");  
  75.     }  
  76. }  
寫完後將maven打成jar的形式,maven的命令是clean package

打成架包後將架包放入hadoop主機上運行。java -jar ***.jar就可以了

如果碰到由於路由器什麼的問題可以看

http://blog.csdn.net/qq_22929803/article/details/45871107這篇文章


[plain] view plaincopy
  1. 2015-05-20 15:11:02,638 DEBUG [org.apache.hadoop.metrics2.lib.MutableMetricsFactory]:42 - field org.apache.hadoop.metrics2.lib.MutableRate org.apache.hadoop.security.UserGroupInformation$UgiMetrics.loginSuccess with annotation @org.apache.hadoop.metrics2.annotation.Metric(about=, sampleName=Ops, always=false, type=DEFAULT, valueName=Time, value=[Rate of successful kerberos logins and latency (milliseconds)])  
  2. 2015-05-20 15:11:02,659 DEBUG [org.apache.hadoop.metrics2.lib.MutableMetricsFactory]:42 - field org.apache.hadoop.metrics2.lib.MutableRate org.apache.hadoop.security.UserGroupInformation$UgiMetrics.loginFailure with annotation @org.apache.hadoop.metrics2.annotation.Metric(about=, sampleName=Ops, always=false, type=DEFAULT, valueName=Time, value=[Rate of failed kerberos logins and latency (milliseconds)])  
  3. 2015-05-20 15:11:02,670 DEBUG [org.apache.hadoop.metrics2.lib.MutableMetricsFactory]:42 - field org.apache.hadoop.metrics2.lib.MutableRate org.apache.hadoop.security.UserGroupInformation$UgiMetrics.getGroups with annotation @org.apache.hadoop.metrics2.annotation.Metric(about=, sampleName=Ops, always=false, type=DEFAULT, valueName=Time, value=[GetGroups])  
  4. 2015-05-20 15:11:02,672 DEBUG [org.apache.hadoop.metrics2.impl.MetricsSystemImpl]:231 - UgiMetrics, User and group related metrics  
  5. 2015-05-20 15:11:02,867 DEBUG [org.apache.hadoop.security.Groups]:278 -  Creating new Groups object  
  6. 2015-05-20 15:11:02,879 DEBUG [org.apache.hadoop.util.NativeCodeLoader]:46 - Trying to load the custom-built native-hadoop library...  
  7. 2015-05-20 15:11:02,885 DEBUG [org.apache.hadoop.util.NativeCodeLoader]:55 - Failed to load native-hadoop with error: java.lang.UnsatisfiedLinkError: no hadoop in java.library.path  
  8. 2015-05-20 15:11:02,886 DEBUG [org.apache.hadoop.util.NativeCodeLoader]:56 - java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib  
  9. 2015-05-20 15:11:02,886 WARN  [org.apache.hadoop.util.NativeCodeLoader]:62 - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable  
  10. 2015-05-20 15:11:02,887 DEBUG [org.apache.hadoop.util.PerformanceAdvisory]:41 - Falling back to shell based  
  11. 2015-05-20 15:11:02,891 DEBUG [org.apache.hadoop.security.JniBasedUnixGroupsMappingWithFallback]:45 - Group mapping impl=org.apache.hadoop.security.ShellBasedUnixGroupsMapping  
  12. 2015-05-20 15:11:03,101 DEBUG [org.apache.hadoop.util.Shell]:396 - setsid exited with exit code 0  
  13. 2015-05-20 15:11:03,103 DEBUG [org.apache.hadoop.security.Groups]:91 - Group mapping impl=org.apache.hadoop.security.JniBasedUnixGroupsMappingWithFallback; cacheTimeout=300000; warningDeltaMs=5000  
  14. 2015-05-20 15:11:03,123 DEBUG [org.apache.hadoop.security.UserGroupInformation]:209 - hadoop login  
  15. 2015-05-20 15:11:03,124 DEBUG [org.apache.hadoop.security.UserGroupInformation]:144 - hadoop login commit  
  16. 2015-05-20 15:11:03,139 DEBUG [org.apache.hadoop.security.UserGroupInformation]:174 - using local user:UnixPrincipal: root  
  17. 2015-05-20 15:11:03,153 DEBUG [org.apache.hadoop.security.UserGroupInformation]:180 - Using user: "UnixPrincipal: root" with name root  
  18. 2015-05-20 15:11:03,154 DEBUG [org.apache.hadoop.security.UserGroupInformation]:190 - User entry: "root"  
  19. 2015-05-20 15:11:03,159 DEBUG [org.apache.hadoop.security.UserGroupInformation]:799 - UGI loginUser:root (auth:SIMPLE)  
  20. 2015-05-20 15:11:03,421 DEBUG [org.apache.hadoop.hdfs.BlockReaderLocal]:443 - dfs.client.use.legacy.blockreader.local = false  
  21. 2015-05-20 15:11:03,421 DEBUG [org.apache.hadoop.hdfs.BlockReaderLocal]:446 - dfs.client.read.shortcircuit = false  
  22. 2015-05-20 15:11:03,424 DEBUG [org.apache.hadoop.hdfs.BlockReaderLocal]:449 - dfs.client.domain.socket.data.traffic = false  
  23. 2015-05-20 15:11:03,424 DEBUG [org.apache.hadoop.hdfs.BlockReaderLocal]:452 - dfs.domain.socket.path =  
  24. 2015-05-20 15:11:03,477 DEBUG [org.apache.hadoop.hdfs.DFSClient]:634 - No KeyProvider found.  
  25. 2015-05-20 15:11:03,557 DEBUG [org.apache.hadoop.io.retry.RetryUtils]:74 - multipleLinearRandomRetry = null  
  26. 2015-05-20 15:11:03,634 DEBUG [org.apache.hadoop.ipc.Server]:233 - rpcKind=RPC_PROTOCOL_BUFFER, rpcRequestWrapperClass=class org.apache.hadoop.ipc.ProtobufRpcEngine$RpcRequestWrapper, rpcInvoker=org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker@31206beb  
  27. 2015-05-20 15:11:03,668 DEBUG [org.apache.hadoop.ipc.Client]:63 - getting client out of cache: org.apache.hadoop.ipc.Client@77be656f  
  28. 2015-05-20 15:11:04,503 DEBUG [org.apache.hadoop.util.PerformanceAdvisory]:109 - Both short-circuit local reads and UNIX domain socket are disabled.  
  29. 2015-05-20 15:11:04,519 DEBUG [org.apache.hadoop.hdfs.protocol.datatransfer.sasl.DataTransferSaslUtil]:183 - DataTransferProtocol not using SaslPropertiesResolver, no QOP found in configuration for dfs.data.transfer.protection  
  30. 2015-05-20 15:11:04,582 DEBUG [org.apache.hadoop.ipc.Client]:427 - The ping interval is 60000 ms.  
  31. 2015-05-20 15:11:04,585 DEBUG [org.apache.hadoop.ipc.Client]:697 - Connecting to /192.168.184.128:9000  
  32. 2015-05-20 15:11:04,754 DEBUG [org.apache.hadoop.ipc.Client]:961 - IPC Client (586084331) connection to /192.168.184.128:9000 from root: starting, having connections 1  
  33. 2015-05-20 15:11:04,756 DEBUG [org.apache.hadoop.ipc.Client]:1024 - IPC Client (586084331) connection to /192.168.184.128:9000 from root sending #0  
  34. 2015-05-20 15:11:04,815 DEBUG [org.apache.hadoop.ipc.Client]:1081 - IPC Client (586084331) connection to /192.168.184.128:9000 from root got value #0  
  35. 2015-05-20 15:11:04,816 DEBUG [org.apache.hadoop.ipc.ProtobufRpcEngine]:253 - Call: getListing took 276ms  
  36. 2015-05-20 15:11:04,859 INFO  [my.hadoopstudy.dfs.Test]:44 - FileStatus{path=hdfs://192.168.184.128:9000/usr/local/test; isDirectory=true; modification_time=1432093791363; access_time=0; owner=root; group=supergroup; permission=rwxr-xr-x; isSymlink=false}  
  37. 2015-05-20 15:11:04,860 INFO  [my.hadoopstudy.dfs.Test]:44 - FileStatus{path=hdfs://192.168.184.128:9000/usr/local/test.log; isDirectory=false; length=0; replication=3; blocksize=134217728; modification_time=1432103622198; access_time=1432103622198; owner=root; group=supergroup; permission=rw-r--r--; isSymlink=false}  
  38. 2015-05-20 15:11:04,863 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1657 - /usr/local/test.log: masked=rw-r--r--  
  39. 2015-05-20 15:11:04,923 DEBUG [org.apache.hadoop.ipc.Client]:1024 - IPC Client (586084331) connection to /192.168.184.128:9000 from root sending #1  
  40. 2015-05-20 15:11:04,930 DEBUG [org.apache.hadoop.ipc.Client]:1081 - IPC Client (586084331) connection to /192.168.184.128:9000 from root got value #1  
  41. 2015-05-20 15:11:04,930 DEBUG [org.apache.hadoop.ipc.ProtobufRpcEngine]:253 - Call: create took 11ms  
  42. 2015-05-20 15:11:04,940 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1806 - computePacketChunkSize: src=/usr/local/test.log, chunkSize=516, chunksPerPacket=127, packetSize=65532  
  43. 2015-05-20 15:11:04,959 DEBUG [org.apache.hadoop.hdfs.LeaseRenewer]:295 - Lease renewer daemon for [DFSClient_NONMAPREDUCE_2039563311_1] with renew id 1 started  
  44. 2015-05-20 15:11:04,965 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1873 - DFSClient writeChunk allocating new packet seqno=0, src=/usr/local/test.log, packetSize=65532, chunksPerPacket=127, bytesCurBlock=0  
  45. 2015-05-20 15:11:04,965 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1819 - Queued packet 0  
  46. 2015-05-20 15:11:04,966 DEBUG [org.apache.hadoop.hdfs.DFSClient]:586 - Allocating new block  
  47. 2015-05-20 15:11:04,971 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1819 - Queued packet 1  
  48. 2015-05-20 15:11:04,972 DEBUG [org.apache.hadoop.hdfs.DFSClient]:2137 - Waiting for ack for: 1  
  49. 2015-05-20 15:11:04,989 DEBUG [org.apache.hadoop.ipc.Client]:1024 - IPC Client (586084331) connection to /192.168.184.128:9000 from root sending #2  
  50. 2015-05-20 15:11:04,995 DEBUG [org.apache.hadoop.ipc.Client]:1081 - IPC Client (586084331) connection to /192.168.184.128:9000 from root got value #2  
  51. 2015-05-20 15:11:04,996 DEBUG [org.apache.hadoop.ipc.ProtobufRpcEngine]:253 - Call: addBlock took 11ms  
  52. 2015-05-20 15:11:05,015 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1394 - pipeline = 192.168.184.129:50010  
  53. 2015-05-20 15:11:05,017 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1394 - pipeline = 192.168.184.130:50010  
  54. 2015-05-20 15:11:05,017 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1605 - Connecting to datanode 192.168.184.129:50010  
  55. 2015-05-20 15:11:05,019 DEBUG [org.apache.hadoop.hdfs.DFSClient]:1614 - Send buf size 124928  
  56. 2015-05-20 15:11:05,021 DEBUG [org.apache.hadoop.ipc.Client]:1024 - IPC Client (586084331) connection to /192.168.184.128:9000 from root sending #3  
  57. 2015-05-20 15:11:05,023 DEBUG [org.apache.hadoop.ipc.Client]:1081 - IPC Client (586084331) connection to /192.168.184.128:9000 from root got value #3  
  58. 2015-05-20 15:11:05,025 DEBUG [org.apache.hadoop.ipc.ProtobufRpcEngine]:253 - Call: getServerDefaults took 4ms  
  59. 2015-05-20 15:11:05,033 DEBUG [org.apache.hadoop.hdfs.protocol.datatransfer.sasl.SaslDataTransferClient]:244 - SASL client skipping handshake in unsecured configuration for addr = /192.168.184.129, datanodeId = 192.168.184.129:50010  
  60. 2015-05-20 15:11:05,312 DEBUG [org.apache.hadoop.hdfs.DFSClient]:637 - DataStreamer block BP-824136034-192.168.184.128-1432019708326:blk_1073741848_1024 sending packet packet seqno:0 offsetInBlock:0 lastPacketInBlock:false lastByteOffsetInBlock: 12  
  61. 2015-05-20 15:11:05,494 DEBUG [org.apache.hadoop.hdfs.DFSClient]:876 - DFSClient seqno: 0 status: SUCCESS status: SUCCESS downstreamAckTimeNanos: 1635651  
  62. 2015-05-20 15:11:05,494 DEBUG [org.apache.hadoop.hdfs.DFSClient]:637 - DataStreamer block BP-824136034-192.168.184.128-1432019708326:blk_1073741848_1024 sending packet packet seqno:1 offsetInBlock:12 lastPacketInBlock:true lastByteOffsetInBlock: 12  
  63. 2015-05-20 15:11:05,502 DEBUG [org.apache.hadoop.hdfs.DFSClient]:876 - DFSClient seqno: 1 status: SUCCESS status: SUCCESS downstreamAckTimeNanos: 3076249  
  64. 2015-05-20 15:11:05,509 DEBUG [org.apache.hadoop.ipc.Client]:1024 - IPC Client (586084331) connection to /192.168.184.128:9000 from root sending #4  
  65. 2015-05-20 15:11:05,514 DEBUG [org.apache.hadoop.ipc.Client]:1081 - IPC Client (586084331) connection to /192.168.184.128:9000 from root got value #4  
  66. 2015-05-20 15:11:05,516 DEBUG [org.apache.hadoop.ipc.ProtobufRpcEngine]:253 - Call: complete took 8ms  
  67. 2015-05-20 15:11:05,543 DEBUG [org.apache.hadoop.ipc.Client]:1024 - IPC Client (586084331) connection to /192.168.184.128:9000 from root sending #5  
  68. 2015-05-20 15:11:05,552 DEBUG [org.apache.hadoop.ipc.Client]:1081 - IPC Client (586084331) connection to /192.168.184.128:9000 from root got value #5  
  69. 2015-05-20 15:11:05,554 DEBUG [org.apache.hadoop.ipc.ProtobufRpcEngine]:253 - Call: getBlockLocations took 23ms  
  70. 2015-05-20 15:11:05,557 DEBUG [org.apache.hadoop.hdfs.DFSClient]:273 - newInfo = LocatedBlocks{  
  71.   fileLength=12  
  72.   underConstruction=false  
  73.   blocks=[LocatedBlock{BP-824136034-192.168.184.128-1432019708326:blk_1073741848_1024; getBlockSize()=12; corrupt=false; offset=0; locs=[192.168.184.129:50010, 192.168.184.130:50010]; storageIDs=[DS-b0a4e12f-e888-4071-a1e8-ef38deb2c0d7, DS-777e9485-5f53-4813-b526-783a678f1281]; storageTypes=[DISK, DISK]}]  
  74.   lastLocatedBlock=LocatedBlock{BP-824136034-192.168.184.128-1432019708326:blk_1073741848_1024; getBlockSize()=12; corrupt=false; offset=0; locs=[192.168.184.129:50010, 192.168.184.130:50010]; storageIDs=[DS-b0a4e12f-e888-4071-a1e8-ef38deb2c0d7, DS-777e9485-5f53-4813-b526-783a678f1281]; storageTypes=[DISK, DISK]}  
  75.   isLastBlockComplete=true}  
  76. 2015-05-20 15:11:05,564 DEBUG [org.apache.hadoop.hdfs.DFSClient]:960 - Connecting to datanode 192.168.184.129:50010  
  77. 2015-05-20 15:11:05,587 DEBUG [org.apache.hadoop.hdfs.protocol.datatransfer.sasl.SaslDataTransferClient]:244 - SASL client skipping handshake in unsecured configuration for addr = /192.168.184.129, datanodeId = 192.168.184.129:50010  
  78. 2015-05-20 15:11:05,698 INFO  [my.hadoopstudy.dfs.Test]:75 - content:Hello World!  
  79. 2015-05-20 15:11:05,698 INFO  [my.hadoopstudy.dfs.Test]:80 - 成功  
  80. 2015-05-20 15:11:05,705 DEBUG [org.apache.hadoop.ipc.Client]:97 - stopping client from cache: org.apache.hadoop.ipc.Client@77be656f  
  81. 2015-05-20 15:11:05,705 DEBUG [org.apache.hadoop.ipc.Client]:103 - removing client from cache: org.apache.hadoop.ipc.Client@77be656f  
  82. 2015-05-20 15:11:05,705 DEBUG [org.apache.hadoop.ipc.Client]:110 - stopping actual client because no more references remain: org.apache.hadoop.ipc.Client@77be656f  
  83. 2015-05-20 15:11:05,709 DEBUG [org.apache.hadoop.ipc.Client]:1234 - Stopping client  
  84. 2015-05-20 15:11:05,710 DEBUG [org.apache.hadoop.ipc.Client]:1184 - IPC Client (586084331) connection to /192.168.184.128:9000 from root: closed  
  85. 2015-05-20 15:11:05,710 DEBUG [org.apache.hadoop.ipc.Client]:979 - IPC Client (586084331) connection to /192.168.184.128:9000 from root: stopped, remaining connections  
執行成功的日誌
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章