通过hadoopAPI访问文件-成都快上网建站

通过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://kswjz.com/article/ijsjie.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流