8288分类目录 8288分类目录 8288分类目录
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

java操作hdfs 创建文件 删除文件 读取文件

来源:本站原创 浏览:98次 时间:2022-01-22

package test;


import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;


/**
 * java实现 HDFS  因为是测试  所以直接throws Exception开发不建议使用
 * @author LiMing E-mail:1151143484@qq.com
 * @date 2017年5月14日 下午12:28:29
 */
public class HelloHDFS {
public static void main(String[] args)   {

  //测试1
/* URL简单使用 
URL url =new URL("https://www.baidu.com");
InputStream in  =url.openStream();
//copyBytes 把in中数据 输入到out中  1024是缓冲长度  true是传输完成后是否关闭流
IOUtils.copyBytes(in, System.out, 1024,true);
*/

try{

//测试2
/*//URL默认不支持hdfs连接 如果使用hdfs连接还需要设置
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
//读取hdfs中上传的hello.txt 之前就上传到根目录了
URL  url=new URL("hdfs://192.168.66.8:9000/hello.txt");
InputStream in  =url.openStream();
IOUtils.copyBytes(in, System.out, 1024,true);*/

/*Configuration conf=new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.66.8:9000");
FileSystem fileSystem=FileSystem.get(conf);
//创建文件夹 在根目录下创建文件夹liming 成功返回true
boolean suc1=  fileSystem.mkdirs(new Path("/liming"));
System.out.println(suc1);

//判断文件或目录存在与否 存在返回true
boolean suc2=  fileSystem.exists(new Path("/liming"));
System.out.println(suc2);

//删除指定文件/文件夹  删除是会放到一个类似回收站的地方  如果第二个参数设置为true那么就会直接彻底删除
boolean suc3=  fileSystem.delete(new Path("/liming"),true);
System.out.println(suc3);


//删除之后判断存在不存在
boolean suc4=  fileSystem.exists(new Path("/liming"));
System.out.println(suc4);*/


//测试3
/*//写文件到hdfs系统   创建test.data并写入内容
Configuration conf=new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.66.8:9000");
FileSystem fileSystem=FileSystem.get(conf);
FSDataOutputStream out=fileSystem.create(new Path("/test.data"),true) ;
FileInputStream  fileInputStream=new FileInputStream("c:/test.txt");
IOUtils.copyBytes(fileInputStream, out, 1024,true);
//创建之后读取内容 看看是不是存在
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
URL url2=new URL("hdfs://192.168.66.8:9000/test.data");
InputStream in2  =url2.openStream();
IOUtils.copyBytes(in2, System.out, 1024,true);
*/

//测试4
/*//也可以用原始的方式
Configuration conf=new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.66.8:9000");
FileSystem fileSystem=FileSystem.get(conf);
FSDataOutputStream out2=fileSyste����,����m.create(new Path("/test2.data"),true) ;
FileInputStream  fileInputStream2=new FileInputStream("c:/test.txt");
byte[] buf=new byte[1024];
int len =  fileInputStream2.read(buf);
while (len != -1) {
out2.write(buf);
len =  fileInputStream2.read(buf);
}
fileInputStream2.close();
out2.close();
//查看是否存在
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
URL url3=new URL("hdfs://192.168.66.8:9000/test2.data");
InputStream  in3  =url3.openStream();
IOUtils.copyBytes(in3, System.out, 1024,true);*/



}catch(Exception e){
e.printStackTrace();
}



}

}


源码下载地址:http://download.csdn.net/detail/qq_36291682/9842125


所用jar包  可能不是全部使用 但是导入的时候是全部导入了:

 hadoop-common-2.7.3.jar
  activation-1.1.jar
  apacheds-i18n-2.0.0-M15.jar
  apacheds-kerberos-codec-2.0.0-M15.jar
  api-asn1-api-1.0.0-M20.jar
  api-util-1.0.0-M20.jar
  asm-3.2.jar
  avro-1.7.4.jar
  commons-beanutils-1.7.0.jar
  commons-beanutils-core-1.8.0.jar
  commons-cli-1.2.jar
  commons-codec-1.4.jar
  commons-collections-3.2.2.jar
  commons-compress-1.4.1.jar
  commons-configuration-1.6.jar
  commons-digester-1.8.jar
  commons-httpclient-3.1.jar
  commons-io-2.4.jar
  commons-lang-2.6.jar
  commons-logging-1.1.3.jar
  commons-math3-3.1.1.jar
  commons-net-3.1.jar
  curator-client-2.7.1.jar
  curator-framework-2.7.1.jar
  curator-recipes-2.7.1.jar
  gson-2.2.4.jar
  guava-11.0.2.jar
  hadoop-annotations-2.7.3.jar
  hadoop-auth-2.7.3.jar
  hamcrest-core-1.3.jar
  htrace-core-3.1.0-incubating.jar
  httpclient-4.2.5.jar
  httpcore-4.2.5.jar
  jackson-core-asl-1.9.13.jar
  jackson-jaxrs-1.9.13.jar
  jackson-mapper-asl-1.9.13.jar
  jackson-xc-1.9.13.jar
  java-xmlbuilder-0.4.jar
  jaxb-api-2.2.2.jar
  jaxb-impl-2.2.3-1.jar
  jersey-core-1.9.jar
  jersey-json-1.9.jar
  jersey-server-1.9.jar
  jets3t-0.9.0.jar
  jettison-1.1.jar
  jetty-6.1.26.jar
  jetty-util-6.1.26.jar
  jsch-0.1.42.jar
  jsp-api-2.1.jar
  jsr305-3.0.0.jar
  junit-4.11.jar
  log4j-1.2.17.jar
  mockito-all-1.8.5.jar
  netty-3.6.2.Final.jar
  paranamer-2.3.jar
  protobuf-java-2.5.0.jar
  servlet-api-2.5.jar
  slf4j-api-1.7.10.jar
  slf4j-log4j12-1.7.10.jar
  snappy-java-1.0.4.1.jar
  stax-api-1.0-2.jar
  xmlenc-0.52.jar
  xz-1.0.jar
  zookeeper-3.4.6.jar
hadoop-hdfs-2.7.3.jar


  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net