利用JavaAPI訪問HDFS的檔案

來源:互聯網
上載者:User

1、重讀設定檔core-site.xml

要利用Java用戶端來存取HDFS上的檔案,不得不說的是設定檔hadoop-0.20.2/conf/core-site.xml了,最初我就是在這裡吃了大虧,所以我死活連不上HDFS,檔案無法建立、讀取。

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>
<!--- global properties -->
<property>
<name>hadoop.tmp.dir</name>
<value>/home/zhangzk/hadoop</value>
<description>A base for other temporary directories.</description>
</property>
<!-- file system properties -->
<property>
<name>fs.default.name</name>
<value>hdfs://linux-zzk-113:9000</value>
</property>
</configuration>

配置項:hadoop.tmp.dir表示命名節點上存放中繼資料的目錄位置,對於資料節點則為該節點上存放檔案資料的目錄。 

配置項:fs.default.name表示命名的IP地址和連接埠號碼,預設值是file:///,對於JavaAPI來講,串連HDFS必須使用這裡的配置的URL地址,對於資料節點來講,資料節點通過該URL來訪問命名節點。

 

2、利用JavaAPI來訪問HDFS的檔案與目錄

package com.demo.hdfs;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

/**
 * @author zhangzk
 *
 */
public class FileCopyToHdfs {

 public static void main(String[] args) throws Exception {
  try {
   //uploadToHdfs();   
   //deleteFromHdfs();
   //getDirectoryFromHdfs();
   appendToHdfs();
   readFromHdfs();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally
  {
   System.out.println("SUCCESS");
  }
 }

 

 /**上傳檔案到HDFS上去*/

 private static void uploadToHdfs() throws FileNotFoundException,IOException {
  String localSrc = "d://qq.txt";
  String dst = "hdfs://192.168.0.113:9000/user/zhangzk/qq.txt";
  InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
  Configuration conf = new Configuration();
  
  FileSystem fs = FileSystem.get(URI.create(dst), conf);
  OutputStream out = fs.create(new Path(dst), new Progressable() {
   public void progress() {
    System.out.print(".");
   }
  });
  IOUtils.copyBytes(in, out, 4096, true);
 }
 

 /**從HDFS上讀取檔案*/
 private static void readFromHdfs() throws FileNotFoundException,IOException {
  String dst = "hdfs://192.168.0.113:9000/user/zhangzk/qq.txt";  
  Configuration conf = new Configuration();  
  FileSystem fs = FileSystem.get(URI.create(dst), conf);
  FSDataInputStream hdfsInStream = fs.open(new Path(dst));
  
  OutputStream out = new FileOutputStream("d:/qq-hdfs.txt");
  byte[] ioBuffer = new byte[1024];
  int readLen = hdfsInStream.read(ioBuffer);

  while(-1 != readLen){
  out.write(ioBuffer, 0, readLen);  
  readLen = hdfsInStream.read(ioBuffer);
  }
  out.close();
  hdfsInStream.close();
  fs.close();
 }
 

 /**以append方式將內容添加到HDFS上檔案的末尾;注意:檔案更新,需要在hdfs-site.xml中添<property><name>dfs.append.support</name><value>true</value></property>*/
 private static void appendToHdfs() throws FileNotFoundException,IOException {
  String dst = "hdfs://192.168.0.113:9000/user/zhangzk/qq.txt";  
  Configuration conf = new Configuration();  
  FileSystem fs = FileSystem.get(URI.create(dst), conf);  
  FSDataOutputStream out = fs.append(new Path(dst));

  int readLen = "zhangzk add by hdfs java api".getBytes().length;

  while(-1 != readLen){
  out.write("zhangzk add by hdfs java api".getBytes(), 0, readLen);
  }
  out.close();
  fs.close();
 }
 

 /**從HDFS上刪除檔案*/
 private static void deleteFromHdfs() throws FileNotFoundException,IOException {
  String dst = "hdfs://192.168.0.113:9000/user/zhangzk/qq-bak.txt";  
  Configuration conf = new Configuration();  
  FileSystem fs = FileSystem.get(URI.create(dst), conf);
  fs.deleteOnExit(new Path(dst));
  fs.close();
 }
 

 /**遍曆HDFS上的檔案和目錄*/
 private static void getDirectoryFromHdfs() throws FileNotFoundException,IOException {
  String dst = "hdfs://192.168.0.113:9000/user/zhangzk";  
  Configuration conf = new Configuration();  
  FileSystem fs = FileSystem.get(URI.create(dst), conf);
  FileStatus fileList[] = fs.listStatus(new Path(dst));
  int size = fileList.length;
  for(int i = 0; i < size; i++){
  System.out.println("name:" + fileList[i].getPath().getName() + "/t/tsize:" + fileList[i].getLen());
  }
  fs.close();
 } 

}

 

注意:對於append操作,從hadoop-0.21版本開始就不支援了,關於Append的操作可以參考Javaeye上的一篇文檔。

http://dongyajun.javaeye.com/blog/643391

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.