大数据(5) - HDFS中的常用API操作
一、安装java
二、IntelliJ IDEA(2018)安装和破解与初期配置 参考链接
1.进入官网下载IntelliJ IDEA https://www.jetbrains.com/idea/download/#section=windows,选择收费版,下面再破解
2.点击下载进入下载页开始下载,如果没有弹出下载框,请点击图中位置
3.下载之后双击,开始安装,点击next
4.选择安装路径,最好不要安装到C盘,这里我安装到D盘IDEA文件夹,点击next
5.选择安装32位或者64位的,根据你电脑选择,点击next
6.这里默认就行了,点击install
7.开始安装
.
8.安装完成,勾选启动
9.点击OK
.
10.点击接受
11.破解
1.下载破解补丁JetbrainsCrack-2.6.2.jar 地址:http://download.csdn.net/detail/gnail_oug/9824630
2.将你下载好的JetbrainsCrack-2.6.2.jar拷贝到你的Idea安装目录的bin目录下,如下图红色框所示
3.在安装的idea下面的bin目录下面有2个文件 : 一个是idea64.exe.vmoptions,还有一个是idea.exe.vmoptions(如上图绿框)。
用记事本打开 分别在最下面一行增加一行:-javaagent:D:\IDEA\bin\JetbrainsCrack-2.6.2.jar
注意:“D:\IDEA\bin\JetbrainsCrack-2.6.2.jar”是我对应的JetbrainsCrack-2.6.2.jar的位置,你用你自己的安装路径。
4.重新启动软件,再到Acrivation code中选择界面的时候,写入下面注册代码(到2099年到期)
- ThisCrackLicenseId-{
- "licenseId":"ThisCrackLicenseId",
- "licenseeName":"idea",
- "assigneeName":"",
- "assigneeEmail":"idea@163.com",
- "licenseRestriction":"For This Crack, Only Test! Please support genuine!!!",
- "checkConcurrentUse":false,
- "products":[
- {"code":"II","paidUpTo":"2099-12-31"},
- {"code":"DM","paidUpTo":"2099-12-31"},
- {"code":"AC","paidUpTo":"2099-12-31"},
- {"code":"RS0","paidUpTo":"2099-12-31"},
- {"code":"WS","paidUpTo":"2099-12-31"},
- {"code":"DPN","paidUpTo":"2099-12-31"},
- {"code":"RC","paidUpTo":"2099-12-31"},
- {"code":"PS","paidUpTo":"2099-12-31"},
- {"code":"DC","paidUpTo":"2099-12-31"},
- {"code":"RM","paidUpTo":"2099-12-31"},
- {"code":"CL","paidUpTo":"2099-12-31"},
- {"code":"PC","paidUpTo":"2099-12-31"}
- ],
- "hash":"2911276/0",
- "gracePeriodDays":7,
- "autoProlongated":false}
5.能够进入应用界面,说明破解成功(如果还不成功去http://idea.lanyus.com/这个网站看一下)
12.配置 File >> Settings... (Ctrl + Alt + S)
1.关闭reopen选项,打开Intellij IDEA 时不会默认打开上次关闭的项目
2.安装maven
2.1 解压apache-maven-3.3.9文件夹放到D盘根目录 apache-maven-3.3.9压缩文件下载
2.1修改maven的settings.xml配置,在apache-maven-3.3.9/conf/settings.xml 已经配置好了,直接使用即可
2.2 在D盘目录下,创建一个m2文件夹
2.3 将刚才修改后的settings.xml文件移动到该m2文件夹下
2.4 修改idea中关于maven的配置(检查setttings和defaultSettings中的maven设置是否一样)
3.项目编码设置(一共6处修改)
File >> Settings ...
File >> Other settings >> Settings for New Projects ...
4.创建项目
设置JDK主目录路径
File >> Projects structure ... (Ctrl + Shift + Alt + S) >> 如下两图,设置你自己java的安装路径
5.导入依赖
View >> Tool windows >> Maven Projects
6.常用快捷键
ctrl + shift + enter:补全代码后方缺失的符号
shift + enter:直接换行
ctrl + alt + t:弹出可以包裹当前代码的语法列表
alt + enter:相当于eclipse的ctrl + 1,错误智能修复提示
ctrl + p:提示当前方法可以传递的参数类型以及参数个数
ctrl + d:复制当前行到下一行,类似eclipse中的ctrl + alt + 光标下
ctrl + x:剪切当前光标所在行
ctrl + y:删除当前光标所在行
ctrl + shift + 光标上或下:移动当前行代码
ctrl + alt + 光标左右:可以在查看代码的时候,切换上一次或下一次查看的视图
ctrl + o::弹出当前类中可以覆写的方法列表
ctrl + alt + v:生成一个变量接受某个方法返回的值
ctrl + shift + i : 查看方法定义代码
HDFS中的常用API操作
在项目中创建一个HDFSClient的java类
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.*;
- import org.apache.hadoop.io.IOUtils;
- import org.junit.Test;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- /*
- * 使用javaAPI来操作hdfs
- */
- public class HDFSClient {
- /*
- * 在hdfs中创建文件夹
- */
- @Test
- public void testMkdir() throws URISyntaxException, IOException, InterruptedException {
- /*
- * util.Shell: Failed to locate the winutils binary in the hadoop binary path
- * 解决办法:
- * 1.下载winutils.exe 链接:http://public-repo-1.hortonworks.com/hdp-win-alpha/winutils.exe
- * 2.创建文件夹,比方说 D:\winutils\bin
- * 3.复制winutils.exe里面D:\winutils\bin
- * 4.将环境变量设置HADOOP_HOME为D:\winutils
- * 5.System.setProperty("hadoop.home.dir", "D://winutils");
- */
- System.setProperty("hadoop.home.dir", "D://winutils");
- // 创建配置文件对象
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- fileSystem.mkdirs(new Path("/user/admin/404"));
- fileSystem.close();
- }
- /*
- * 下载文件
- */
- @Test
- public void testCopytoLocalFile() throws URISyntaxException, IOException, InterruptedException{
- System.setProperty("hadoop.home.dir", "D://winutils");
- // 创建配置文件对象
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- fileSystem.copyToLocalFile(
- false,
- new Path("/user/admin/403/hello_world2.txt"),
- new Path("d:\\copy_words.txt")
- );
- fileSystem.close();
- }
- /**
- * 文件删除
- */
- @Test
- public void testDelete() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- fileSystem.delete(new Path("/user/admin/403/"), true);
- fileSystem.close();
- }
- /**
- * 上传文件
- * JavaAPI在操作文件 上传时,如果文件已经存在于HDFS中,则先删除HDFS中的文件,再上传
- * 但是如果使用shell操作,则会提示,该文件已存在
- */
- @Test
- public void testCopyFromLocal() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- // fileSystem.copyFromLocalFile(new Path("d:\\copy_words.txt"), new Path("/"));
- fileSystem.copyFromLocalFile(new Path("d:" + File.separator + "copy_words.txt"), new Path("/"));
- fileSystem.close();
- }
- /**
- * 重命名文件
- */
- @Test
- public void testReanme() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- fileSystem.rename(new Path("/user/admin/403/abc.txt")
- , new Path("/user/admin/403/aaa.txt"));
- fileSystem.close();
- }
- /**
- * 展示目录列表
- */
- @Test
- public void testListFile() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
- while(listFiles.hasNext()){
- LocatedFileStatus fileStatus = listFiles.next();
- System.out.println("文件名称:" + fileStatus.getPath().getName());
- System.out.println("文件长度:" + fileStatus.getLen());
- System.out.println("文件权限:" + fileStatus.getPermission());
- System.out.println("文件所属组" + fileStatus.getGroup());
- //文件块信息
- BlockLocation[] blockLocations = fileStatus.getBlockLocations();
- for(BlockLocation blockLocation : blockLocations){
- String[] hosts = blockLocation.getHosts();
- for(String host : hosts){
- System.out.println(host);
- }
- }
- System.out.println("--------------这是一个毫无用处的分割线--------------------");
- }
- }
- /**
- * 罗列目录或文件
- */
- @Test
- public void testListStatus() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
- for(FileStatus status : listStatus){
- if(status.isFile()){
- System.out.println("文件:" + status.getPath().getName());
- }else{
- System.out.println("目录:" + status.getPath().getName());
- }
- }
- fileSystem.close();
- }
- /**
- * 通过流的操作上传一个文件到HDFS
- */
- @Test
- public void putFileToHDFS() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- //读取当前操作系统本地的文件
- FileInputStream inputStream = new FileInputStream(new File("d:\\copy_words.txt"));
- //创建HDFS的输出流,用于将本地文件流中的数据拷贝到HDFS中
- FSDataOutputStream outputStream = fileSystem.create(new Path("/jinji_words.txt"));
- //流的对拷
- IOUtils.copyBytes(inputStream, outputStream, conf);
- fileSystem.close();
- }
- /**
- * 通过流的方式,下载文件
- */
- @Test
- public void getFileFromHDFS() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- FSDataInputStream inputStream = fileSystem.open(new Path("/hello_world.txt"));
- FileOutputStream outputStream = new FileOutputStream(new File("d:\\demo.txt"));
- IOUtils.copyBytes(inputStream, outputStream, conf);
- fileSystem.close();
- }
- /**
- * 按照文件块进行下载
- * 可以在下载文件的过程中,设置每次要下载的字节数
- * 例如:我们下载一个文件的一个文件块 自己上传一个bin/hdfs dfs -put ~/softwares/installtions/hadoop-2.7.2.tar.gz /
- * 下载hadoop安装包(200多兆)的第一个文件块(128M)
- */
- @Test
- public void readFileSeek1() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- FSDataInputStream inputStream = fileSystem.open(new Path("/hadoop-2.7.2.tar.gz"));
- FileOutputStream outputStream = new FileOutputStream(new File("d:\\hadoop-2.7.2.tar.gz.part1"));
- byte[] bytes = new byte[1024];//一次读取1KB的数据
- for(int i = 0; i < 1024 * 128; i++){
- inputStream.read(bytes);
- outputStream.write(bytes);
- }
- IOUtils.closeStream(inputStream);
- IOUtils.closeStream(outputStream);
- }
- /**
- * 下载第二个文件块
- */
- @Test
- public void readFileSeek2() throws URISyntaxException, IOException, InterruptedException {
- System.setProperty("hadoop.home.dir", "D://winutils");
- Configuration conf = new Configuration();
- FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.213:8020"), conf, "admin");
- FSDataInputStream inputStream = fileSystem.open(new Path("/hadoop-2.7.2.tar.gz"));
- FileOutputStream outputStream = new FileOutputStream(new File("d:\\hadoop-2.7.2.tar.gz.part2"));
- inputStream.seek(128 * 1024 * 1024);
- // IOUtils.copyBytes(inputStream, outputStream, conf);
- //与上边的操作等价:
- byte[] bytes = new byte[1024];//一次读取1KB的数据
- for(int i = 0; i < 77829046/1024; i++){
- inputStream.read(bytes);
- outputStream.write(bytes);
- }
- IOUtils.closeStream(inputStream);
- IOUtils.closeStream(outputStream);
- fileSystem.close();
- }
- }
注意:把linux下面/home/admin/modules/hadoop-2.7.2/etc/hadoop文件夹中的log4j.properties、core-site.xml、 hdfs-site.xml、mapred-site.xml、yarn-site.xml五个文件复制到wiondw下面项目中的:项目路径\src\main\resources 文件夹中
大数据(5) - HDFS中的常用API操作的更多相关文章
- 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化
第6章 HBase API 操作6.1 环境准备6.2 HBase API6.2.1 判断表是否存在6.2.2 抽取获取 Configuration.Connection.Admin 对象的方法以及关 ...
- 大数据除了Hadoop还有哪些常用的工具?
大数据除了Hadoop还有哪些常用的工具? 1.Hadoop大数据生态平台Hadoop 是一个能够对大量数据进行分布式处理的软件框架.但是 Hadoop 是以一种可靠.高效.可伸缩的方式进行处理的.H ...
- 漫谈ELK在大数据运维中的应用
漫谈ELK在大数据运维中的应用 圈子里关于大数据.云计算相关文章和讨论是越来越多,愈演愈烈.行业内企业也争前恐后,群雄逐鹿.而在大数据时代的运维挑站问题也就日渐突出,任重而道远了.众所周知,大数据平台 ...
- jackson学习之三:常用API操作
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Java+大数据开发——HDFS详解
1. HDFS 介绍 • 什么是HDFS 首先,它是一个文件系统,用于存储文件,通过统一的命名空间--目录树来定位文件. 其次,它是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有各自的角 ...
- 我要进大厂之大数据Hadoop HDFS知识点(1)
01 我们一起学大数据 老刘今天开始了大数据Hadoop知识点的复习,Hadoop包含三个模块,这次先分享出Hadoop中的HDFS模块的基础知识点,也算是对今天复习的内容进行一次总结,希望能够给想学 ...
- hadoop学习;大数据集在HDFS中存为单个文件;安装linux下eclipse出错解决;查看.class文件插件
sudo apt-get install eclipse 安装后打开eclipse,提示出错 An error has occurred. See the log file /home/pengeor ...
- 老李分享:大数据测试之HDFS文件系统
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...
- 大数据学习——hdfs客户端流式操作代码的实现
package cn.itcast.bigdata.hdfs.diceng; import org.apache.hadoop.conf.Configuration; import org.apach ...
随机推荐
- React Native Navigator组件回调
在push的时候定义回调函数: this.props.navigator.push({ component: nextVC, title: titleName, passProps: { //回调 g ...
- HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)
HTML-DEV-ToolLink:https://github.com/easonjim/HTML-DEV-ToolLink 常用的在线字符串编解码.代码压缩.美化.JSON格式化.正则表达式.时间 ...
- 精通JavaScript攻击框架:AttackAPI
多年来客户端安全一直未引起人们的足够重视,但是如今情况发生了急剧转变,客户端安全已经成为信息安全领域的焦点之一.Web恶意软件.AJAX蠕虫.浏览历史暴破.登录检测.傀儡控制技术网络端口扫描以及浏览器 ...
- 表格中的IE BUG
在表格应用了跨列单元格后,在IE6/7下当跨列单元格中的元素长度超过其跨列单元格中第一个单元格的宽度时会产生换行,如下所示: 解决方法: 1. 设置 table 的 'table-layout' 特性 ...
- 【面试问题】—— 2019.3月前端面试之JS原理&CSS基础&Vue框架
前言:三月中旬面试了两家公司,一家小型公司只有面试,另一家稍大型公司笔试之后一面定夺.笔试部分属于基础类型,网上的复习资料都有. 面试时两位面试官都有考到一些实际工作中会用到,但我还没接触过的知识点. ...
- 用C++实现文件压缩(1 哈弗曼编码)
今天下午想把文件压缩写一下,因为我觉得这个还是比较锻炼技术的,对数据结构的要求应该比较高,权当练习了吧. 我采用的压缩方式是Huffman编码,不过比较囧的是,我拼写拼错了,我拼的是haffman,在 ...
- 如何将微信小程序页面内容充满整个屏幕
修改该页面的wxss文件 /* pages/weather/weather.wxss */ .weather{ position: fixed; height: 100%; width: 100%; ...
- MySQL错误Another MySQL daemon already running with the same unix socket.v
etc/init.d/mysqld start 结果显示 Another MySQL daemon already running with the same unix socket.显示另一个MyS ...
- Spring Cloud简单入门教程
原文地址:http://www.cnblogs.com/skyblog/p/5127690.html 按照官方的话说:Spring Cloud 为开发者提供了在分布式系统(如配置管理.服务发现.断路器 ...
- WORD文档书签管理
最近在浏览一个word超长文档,在文档中有几处要点用颜色做了标记,但是在下次查找的时候无法定位,还得一页页去翻,而且无法通过目录概览的形式查看总共做了多少处标记 于是想到了书签 原本以为在视图中能够设 ...