1.环境搭建

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>

2.问题解决

Windows开发Hadoop应用环境配置

  • 解压hadoop安装包到C:/
  • 将winutils.exe和hadoop.dll拷贝到hadoop的bin目录下
  • 在windows配置HADOOP_HOME环境变量
  • 重启开发工具idea,否则开发工具无法识别HADOOP_HOME
  • 在Windows主机配置CentOS的主机名和IP的映射关系

C:\Windows\System32\drivers\etc\hosts

192.168.169.139 CentOS

HDFS权限不足导致写失败?

org.apache.hadoop.security.AccessControlException: Permission denied: user=HIAPAD, access=WRITE, inode="/":root:supergroup:drwxr-xr-x
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkFsPermission(FSPermissionChecker.java:271)
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:257)
...

解决方案

方案1

etc/hadoop/hdfs-site.xml

<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>

关闭HDFS文件权限检查,修改完成后,重启HDFS服务

方案2

-DHADOOP_USER_NAME=root

设置JAVA虚拟机启动参数java XXX -Dxx=xxx

3.HDFS

package com.baizhi.hdfs;/**

- @Author:luoht
- @Description:
- @Date:Create in 16:21 2019/1/3
*/ import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.*; /** - @program: hadoop_01
- @description:
- @author: luoht
- @create: 2019-01-03 16:21
**/ public class TestHDFS {
private FileSystem fileSystem;
private Configuration conf; @Before
public void before() throws IOException {
conf = new Configuration();
conf.addResource("core-site.xml");
conf.addResource("hdfs-site.xml");
fileSystem=FileSystem.newInstance(conf);
} @Test
public void testConfig(){
String value = conf.get("dfs.replication");
System.out.println(value);
} @Test
public void testUpload01() throws IOException {
String file ="C:\\Users\\Administrator\\Desktop\\郁金香開了麽?.txt";
Path dst = new Path("/demo/access/springBoot.pdf");
FileInputStream inputStream = new FileInputStream(file);
FSDataOutputStream outputStream = fileSystem.create(dst, new Progressable() {
@Override
public void progress() {
System.out.println("..");
}
});
IOUtils.copyBytes(inputStream,outputStream,1024,true);
} @Test
public void testUpload02() throws IOException {
Path src = new Path("C:\\Users\\Administrator\\Desktop\\郁金香開了麽?.txt");
Path dst = new Path("/郁金香开了么?.txt");
fileSystem.copyFromLocalFile(src,dst);
} @Test
public void testDownload01() throws IOException {
String file="C:\\Users\\Administrator\\Desktop\\郁金香不开了.txt";
Path dst = new Path("/郁金香开了么?.txt");
FileOutputStream outputStream = new FileOutputStream(file);
InputStream inputStream = fileSystem.open(dst);
IOUtils.copyBytes(inputStream,outputStream,1024,true);
} @Test
public void testDownload02() throws IOException {
Path dst = new Path("C:\\Users\\Administrator\\Desktop\\郁金香不开了.txt");
Path src = new Path("/demo/access/springBoot.pdf");
fileSystem.copyToLocalFile(false,src,dst,true);
} @Test
public void testDelete() throws IOException {
Path src = new Path("/demo");
/*true 代表递归删除子文件*/
fileSystem.delete(src,true);
} @Test
public void testExits() throws IOException {
Path src = new Path("/郁金香开了么?.txt");
boolean exists = fileSystem.exists(src);
//Junit之 断言 -静态导包
assertTrue(exists);
} @Test
public void testMkdir() throws IOException {
Path src = new Path("/demo.assess");
boolean exists = fileSystem.exists(src);
if (!exists){
fileSystem.mkdirs(src);
}
} @Test
public void testListFiles() throws IOException {
Path src = new Path("/tt");
RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(src, true);
while (files.hasNext()){
LocatedFileStatus fileStatus = files.next();
System.out.println(fileStatus.getPath()+"路径 "+fileStatus.isFile()+"是否文件 "+fileStatus.getLen()+"文件大小");
System.out.println("------------------------------");
BlockLocation[] locations = fileStatus.getBlockLocations();
for (BlockLocation location : locations) {
System.out.println("offset"+location.getOffset()+"length"+location.getLength());
}
System.out.println("================================");
}
} @Test
public void testDeleteWithTrash() throws IOException {
Trash trash = new Trash(fileSystem, conf);
Path dst = new Path("/123.txt");
trash.moveToTrash(dst);
} @After
public void after() throws IOException{
fileSystem.close();
} }

三、Hadoop 的 API的更多相关文章

  1. Hadoop 系列(三)Java API

    Hadoop 系列(三)Java API <dependency> <groupId>org.apache.hadoop</groupId> <artifac ...

  2. Python3调用Hadoop的API

    前言: 上一篇文章 我学习使用pandas进行简单的数据分析,但是各位...... Pandas处理.分析不了TB级别数据的大数据,于是再看看Hadoop. 另附上人心不足蛇吞象 对故事一的感悟:   ...

  3. 漫谈可视化Prefuse(三)---Prefuse API数据结构阅读有感

    前篇回顾:上篇<漫谈可视化Prefuse(二)---一分钟学会Prefuse>主要通过一个Prefuse的具体实例了解了构建一个Prefuse application的具体步骤.一个Pre ...

  4. hadoop的API对HDFS上的文件访问

    这篇文章主要介绍了使用hadoop的API对HDFS上的文件访问,其中包括上传文件到HDFS上.从HDFS上下载文件和删除HDFS上的文件,需要的朋友可以参考下hdfs文件操作操作示例,包括上传文件到 ...

  5. Zookeeper 系列(三)Zookeeper API

    Zookeeper 系列(三)Zookeeper API 本节首先介绍 Zookeeper 的 Shell 命令,再对 Java 操作 Zookeeper 的三种方式进行讲解,本节先介绍 Zookee ...

  6. 通过流的方式操作hadoop的API

    通过流的方式操作hadoop的API 功能: 可以直接用来操作hadoop的文件系统 可以用在mapreduce的outputformat中设置RecordWrite 参考: 概念理解 http:// ...

  7. ReactJS入门(三)—— 顶层API

    本文基本跟着官方文档把API都走一遍,但会有实例来解释应该怎么用,木有比我更详细的API文档咯. React.createClass 参数:CONFIG(object) 创建一个ReactClass( ...

  8. 从零开始,做一个NodeJS博客(三):API实现-加载网易云音乐听歌排行

    标签: NodeJS 0 研究了一天,翻遍了GitHub上各种网易云API库,也没有找到我想要的听歌排行API,可能这功能比较小众吧.但收获也不是没有,在 这里 明白了云音乐API加密的凶险,我等蒟蒻 ...

  9. hadoop: hdfs API示例

    利用hdfs的api,可以实现向hdfs的文件.目录读写,利用这一套API可以设计一个简易的山寨版云盘,见下图: 为了方便操作,将常用的文件读写操作封装了一个工具类: import org.apach ...

随机推荐

  1. 小任务之Canvas绘制时钟

    背景图的绘制(大圆.数字.小圆点) 掌握基础知识:圆的绘制(arc方法),关于圆的弧度的计算,数学中关于sin cos的用法 圆的弧度为2*Math.PI 12个数字分得弧度每个为2*Math.PI/ ...

  2. Java Struts2 (三)

    一.国际化概念(了解) 1.什么是国际化 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 2.什么需要国际 ...

  3. Python爬虫教程-23-数据提取-BeautifulSoup4(一)

    Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据,查看文档 https://www.crummy.com/software/BeautifulSoup/bs4/doc. ...

  4. java几种基本排序算法

    1.选择排序 原理:将数组的每一个元素和第一个元素相比较,如果小于第一个元素则交换,选出第一小的,依次选出第二小,第三小的.... 代码 int[] a = {1,3,2,5}; int i,j,te ...

  5. 【NLP_Stanford课堂】语言模型4

    平滑方法: 1. Add-1 smoothing 2. Add-k smoothing 设m=1/V,则有 从而每一项可以跟词汇表的大小相关 3. Unigram prior smoothing 将上 ...

  6. AutoHotkey的函数对象的Bind方法绑定参数的应用

    近期在写Excel数据批处理函数,想提取某列的每个数据是否匹配某某条件的所有单元格. 这种需求比较多,比如判断的值有:单元格值,字体颜色,单元格颜色等等, 判断条件有:相同,不同,正则,或在某多行字符 ...

  7. My SQL 和SQL Server区别

    MySQL 与SQL Server区别 今天了解了二者区别,整理网上查阅资料,总结列举如下: MSSQL == SQL server 是sybase与微软合作时期的产物. 对于程序开发人员而言,目前使 ...

  8. Hibernate、Mybatis与Spring Data JPA

    从零开始集成Springboot+MyBatis+JPA https://www.jianshu.com/p/e14c4a6f6871 MyBatis 与Hibernate的区别 http://xhr ...

  9. SVN安装操作流程

    SVN 安装操作流程 1.服务端安装流程 1.1 双击打开svn-server安装包 1.2 点击Next 1.3 勾选上“I accert the terms in the License Agre ...

  10. 1874 football game(三分法and method to compute the area of trianngle)

    FInd the max area. 1. 三分法 2. NAN (not comparable with number) http://acm.timus.ru/problem.aspx?space ...