目录
2. 需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入
在com.hadoop.hdfs包下,创建HdfsClient类,代码如下:
1.需求分析:要求将本地D盘的文件Test4.txt上传到HDFS根目录
1.需求分析:从HDFS上下载banzhang.txt到D盘上
1.需求:分块读取HDFS上的大文件,比如根目录下的/hadoop-2.7.2.tar.gz
已经准备好的hadoop jar包链接如下,要解压到非中文路径!!!
链接:https://pan.baidu.com/s/1BM4_O-HpDlB5xIgejadURg?pwd=9zuj
提取码:9zuj

编辑环境变量,将 %HADOOP_HOME%\bin 添加到Path中去

打开Eclipse,点击File >> New >> Maven Project




<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

在项目的src/main/java目录下,创建包名com.hadoop.hdfs


package com.hadoop.hdfs;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
public class HdfsClient{
@Test
public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
// conf.set("fs.defaultFS","hdfs://hadoop130:8020");
//1获取hdfs客户端对象
// FileSystem fs =FileSystem.get(conf);
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"),conf , "hadoop");
//2在hdfs上创建路径
fs.mkdirs(new Path("/fang/Test"));
//3关闭资源
fs.close();
System.out.print("over");
}
}
注:hdfs://hadoop130:8020这里是我自己的端口号和主机名,后面“hadoop”是用户名,这些根据自己的来设置!!!这里要保证自己的集群是开启的
运行时需要配置用户名称
客户端去操作HDFS时,是有一个用户身份的。默认情况下,HDFS客户端API会从JVM中获取一个参数来作为自己的用户身份:-DHADOOP_USER_NAME=hadoop,hadoop为用户名称。

执行后的结果图:


//1.文件上传
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
//1获取fs对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"),conf,"hadoop");
//2执行上传API
fs.copyFromLocalFile(new Path("D:/Hadoop2.x/shiyan.txt"), new Path("/shiyan.txt"));
//3关闭资源
fs.close();
System.out.print("执行完毕!");
}
//2.文件下载
@Test
public void testCopyLocalFile() throws IOException, InterruptedException, URISyntaxException {
//1.获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2执行下载操作
fs.copyToLocalFile(new Path("/shiyan.txt"), new Path("D:/Hadoop2.x/shiyan.txt"));
//fs.copyToLocalFile(false, null, null, false);
//3关闭资源
fs.close();
System.out.print("下载完成!");
}
//3.文件删除
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException {
//1.获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2文件删除
fs.delete(new Path("/0529"), true);
//3关闭资源
fs.close();
System.out.print("删除成功!");
}
//4文件更名
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException {
//1.获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2执行更名操作
fs.rename(new Path("/shiyan.txt"), new Path("/Test4.txt"));
//3关闭资源
fs.close();
System.out.print("文件更名成功!");
}
//5文件详情查看
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
//1.获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2查看文件详情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()) {
LocatedFileStatus fileStatus =listFiles.next();
//查看文件名称,权限,长度,块信息
System.out.println(fileStatus.getPath().getName());//文件名称
System.out.println(fileStatus.getPermission());//文件权限
System.out.println(fileStatus.getLen());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("--------分割线---------");
}
//3关闭资源
fs.close();
}
//6判断是文件还是文件夹
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException {
//1.获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2判断操作
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
if(fileStatus.isFile()) {
//文件
System.out.println("f:"+fileStatus.getPath().getName());
}else {
//文件夹
System.out.println("d:"+fileStatus.getPath().getName());
}
}
//3关闭资源
fs.close();
}
public class HDFSIO {
//把本地d盘上的Test4.txt文件上传到hdfs根目录
@Test
public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException{
//1获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2获取输入流
FileInputStream fis = new FileInputStream(new File("D:/Hadoop2.x/shiyan.txt"));
//3获取输出流
FSDataOutputStream fos = fs.create(new Path("/banzhang.txt"));
//4流的对拷
IOUtils.copyBytes(fis, fos, conf);
//5关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
//从HDFS上下载banzhang.txt文件到本地d盘上
@SuppressWarnings("unused")
@Test
public void getFileFormHDFS() throws IOException, InterruptedException, URISyntaxException{
//1获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2获取输入流
FSDataInputStream fis = fs.open(new Path("/banzhang.txt"));
//3获取输出流
FileOutputStream fos = new FileOutputStream(new File("d:/Hadoop2.x/banzhang.txt"));
//4流的对拷
IOUtils.copyBytes(fis, fos, conf);
//5关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
下载第一块:
//下载第一块
@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException {
//1获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.3.tar.gz"));
//3获取输出流
FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-3.1.3.tar.gz.part1"));
//4流的对拷(只拷贝128m)
byte[] buf = new byte[1024];
for (int i = 0; i < 1024*1024 ; i++) {
fis.read(buf);
fos.write(buf);
}
//5关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
下载第二块:
//下载第二块
@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException {
//1获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
//2获取输入法
FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.3.tar.gz"));
//3设置指定读取起点
//重点
fis.seek(1024*1024*128);
//4获取输出流
FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-3.1.3.tar.gz.part2"));
//5流的对拷
IOUtils.copyBytes(fis, fos, conf);
//6关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h
我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path
Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我的公司有一个巨大的数据库,该数据库接收来自多个来源的(许多)事件,用于监控和报告目的。到目前为止,数据中的每个新仪表板或图形都是一个新的Rails应用程序,在巨大的数据库中有额外的表,并且可以完全访问数据库内容。最近,有一个想法让外部(不是我们公司,而是姊妹公司)客户访问我们的数据,并且决定我们应该公开一个只读的RESTfulAPI来查询我们的数据。我的观点是-我们是否也应该为我们的自己
a=[3,4,7,8,3]b=[5,3,6,8,3]假设数组长度相同,是否有办法使用each或其他一些惯用方法从两个数组的每个元素中获取结果?不使用计数器?例如获取每个元素的乘积:[15,12,42,64,9](0..a.count-1).eachdo|i|太丑了...ruby1.9.3 最佳答案 使用Array.zip怎么样?:>>a=[3,4,7,8,3]=>[3,4,7,8,3]>>b=[5,3,6,8,3]=>[5,3,6,8,3]>>c=[]=>[]>>a.zip(b)do|i,j|c[[3,5],[4,3],[7,6],