成都创新互联网站制作重庆分公司

通过hadoopAPI访问文件

/**

成都创新互联服务项目包括港口网站建设、港口网站制作、港口网页制作以及港口网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,港口网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到港口省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

 * 通过Hadoop api访问

 * @throws IOException

 */

@Test

public void readFileByAPI() throws IOException{

Configuration conf = new Configuration();

conf.set("fs.defaultFS", "hdfs://192.168.75.201:8020/");

FileSystem fs = FileSystem.get(conf);

Path path = new Path("/user/index.html");

FSDataInputStream fis =fs.open(path);

byte[] bytes = new byte[1024];

int len = -1;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while((len = fis.read(bytes))!=-1){

baos.write(bytes, 0, len);

}

System.out.println(new String(baos.toByteArray()));

fis.close();

baos.close();

}

 

第二种方式:

/**

 * 通过hadoop api访问

 * @throws IOException

 */

@Test

public void readFileByAPI2() throws IOException{

Configuration conf = new Configuration();

conf.set("fs.defaultFS", "hdfs://192.168.75.201:8020/");

FileSystem fs = FileSystem.get(conf);

Path path = new Path("/user/index.html");

FSDataInputStream fis =fs.open(path);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

IOUtils.copyBytes(fis, baos, 1024);

System.out.println(new String(baos.toByteArray()));

fis.close();

baos.close();

}


本文名称:通过hadoopAPI访问文件
浏览地址:http://cxhlcq.com/article/ijsjie.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部