hdfsjava(hdfsjava api下载文件到本地实验报告)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
本篇文章给大家谈谈hdfsjava,以及hdfsjava api下载文件到本地实验报告对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
- 1、如何使用Java API读写HDFS
- 2、关于用java写程序把本地文件上传到HDFS中的问题
- 3、hadoop里怎么运行java的tar包
- 4、编写一个JAVA类方法,通过该方法可以获取出存储在HDFS集群中根目录的所有文件?
- 5、使用Java API操作HDFS时,_方法用于获取文件列表?
如何使用Java API读写HDFS
Java API读写HDFS
public class FSOptr {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
makeDir(conf);
rename(conf);
delete(conf);
}
// 创建文件目录
private static void makeDir(Configuration conf) throws Exception {
FileSystem fs = FileSystem.get(conf);
Path dir = new Path("/user/hadoop/data/20140318");
boolean result = fs.mkdirs(dir);// 创建文件夹
System.out.println("make dir :" + result);
// 创建文件,并写入内容
Path dst = new Path("/user/hadoop/data/20140318/tmp");
byte[] buff = "hello,hadoop!".getBytes();
FSDataOutputStream outputStream = fs.create(dst);
outputStream.write(buff, 0, buff.length);
outputStream.close();
FileStatus files[] = fs.listStatus(dst);
for (FileStatus file : files) {
System.out.println(file.getPath());
}
fs.close();
}
// 重命名文件
private static void rename(Configuration conf) throws Exception {
FileSystem fs = FileSystem.get(conf);
Path oldName = new Path("/user/hadoop/data/20140318/1.txt");
Path newName = new Path("/user/hadoop/data/20140318/2.txt");
fs.rename(oldName, newName);
FileStatus files[] = fs.listStatus(new Path(
"/user/hadoop/data/20140318"));
for (FileStatus file : files) {
System.out.println(file.getPath());
}
fs.close();
}
// 删除文件
@SuppressWarnings("deprecation")
private static void delete(Configuration conf) throws Exception {
FileSystem fs = FileSystem.get(conf);
Path path = new Path("/user/hadoop/data/20140318");
if (fs.isDirectory(path)) {
FileStatus files[] = fs.listStatus(path);
for (FileStatus file : files) {
fs.delete(file.getPath());
}
} else {
fs.delete(path);
}
// 或者
fs.delete(path, true);
fs.close();
}
/**
* 下载,将hdfs文件下载到本地磁盘
*
* @param localSrc1
* 本地的文件地址,即文件的路径
* @param hdfsSrc1
* 存放在hdfs的文件地址
*/
public boolean sendFromHdfs(String hdfsSrc1, String localSrc1) {
Configuration conf = new Configuration();
FileSystem fs = null;
try {
fs = FileSystem.get(URI.create(hdfsSrc1), conf);
Path hdfs_path = new Path(hdfsSrc1);
Path local_path = new Path(localSrc1);
fs.copyToLocalFile(hdfs_path, local_path);
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 上传,将本地文件copy到hdfs系统中
*
* @param localSrc
* 本地的文件地址,即文件的路径
* @param hdfsSrc
* 存放在hdfs的文件地址
*/
public boolean sendToHdfs1(String localSrc, String hdfsSrc) {
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream(localSrc));
Configuration conf = new Configuration();// 得到配置对象
FileSystem fs; // 文件系统
try {
fs = FileSystem.get(URI.create(hdfsSrc), conf);
// 输出流,创建一个输出流
OutputStream out = fs.create(new Path(hdfsSrc),
new Progressable() {
// 重写progress方法
public void progress() {
// System.out.println("上传完一个设定缓存区大小容量的文件!");
}
});
// 连接两个流,形成通道,使输入流向输出流传输数据,
IOUtils.copyBytes(in, out, 10240, true); // in为输入流对象,out为输出流对象,4096为缓冲区大小,true为上传后关闭流
return true;
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return false;
}
/**
* 移动
*
* @param old_st原来存放的路径
* @param new_st移动到的路径
*/
public boolean moveFileName(String old_st, String new_st) {
try {
// 下载到服务器本地
boolean down_flag = sendFromHdfs(old_st, "/home/hadoop/文档/temp");
Configuration conf = new Configuration();
FileSystem fs = null;
// 删除源文件
try {
fs = FileSystem.get(URI.create(old_st), conf);
Path hdfs_path = new Path(old_st);
fs.delete(hdfs_path);
} catch (IOException e) {
e.printStackTrace();
}
// 从服务器本地传到新路径
new_st = new_st + old_st.substring(old_st.lastIndexOf("/"));
boolean uplod_flag = sendToHdfs1("/home/hadoop/文档/temp", new_st);
if (down_flag uplod_flag) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// copy本地文件到hdfs
private static void CopyFromLocalFile(Configuration conf) throws Exception {
FileSystem fs = FileSystem.get(conf);
Path src = new Path("/home/hadoop/word.txt");
Path dst = new Path("/user/hadoop/data/");
fs.copyFromLocalFile(src, dst);
fs.close();
}
// 获取给定目录下的所有子目录以及子文件
private static void getAllChildFile(Configuration conf) throws Exception {
FileSystem fs = FileSystem.get(conf);
Path path = new Path("/user/hadoop");
getFile(path, fs);
}
private static void getFile(Path path, FileSystem fs)throws Exception {
FileStatus[] fileStatus = fs.listStatus(path);
for (int i = 0; i fileStatus.length; i++) {
if (fileStatus[i].isDir()) {
Path p = new Path(fileStatus[i].getPath().toString());
getFile(p, fs);
} else {
System.out.println(fileStatus[i].getPath().toString());
}
}
}
//判断文件是否存在
private static boolean isExist(Configuration conf,String path)throws Exception{
FileSystem fileSystem = FileSystem.get(conf);
return fileSystem.exists(new Path(path));
}
//获取hdfs集群所有主机结点数据
private static void getAllClusterNodeInfo(Configuration conf)throws Exception{
FileSystem fs = FileSystem.get(conf);
DistributedFileSystem hdfs = (DistributedFileSystem)fs;
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
String[] names = new String[dataNodeStats.length];
System.out.println("list of all the nodes in HDFS cluster:"); //print info
for(int i=0; i dataNodeStats.length; i++){
names[i] = dataNodeStats[i].getHostName();
System.out.println(names[i]); //print info
}
}
//get the locations of a file in HDFS
private static void getFileLocation(Configuration conf)throws Exception{
FileSystem fs = FileSystem.get(conf);
Path f = new Path("/user/cluster/dfs.txt");
FileStatus filestatus = fs.getFileStatus(f);
BlockLocation[] blkLocations = fs.getFileBlockLocations(filestatus,0,filestatus.getLen());
int blkCount = blkLocations.length;
for(int i=0; i blkCount; i++){
String[] hosts = blkLocations[i].getHosts();
//Do sth with the block hosts
System.out.println(hosts);
}
}
//get HDFS file last modification time
private static void getModificationTime(Configuration conf)throws Exception{
FileSystem fs = FileSystem.get(conf);
Path f = new Path("/user/cluster/dfs.txt");
FileStatus filestatus = fs.getFileStatus(f);
long modificationTime = filestatus.getModificationTime(); // measured in milliseconds since the epoch
Date d = new Date(modificationTime);
System.out.println(d);
}
}
关于用java写程序把本地文件上传到HDFS中的问题
将这FileSystem hdfs = FileSystem.get(config);
改成FileSystem hdfs = FileSystem.get(URI.create("hdfs://master:9000"),config)
上面那句取得的是本地文件系统对象,改成下面这个才是取得hdfs文件系统对象,当你要操作本地文件对象的时候就要用上面那句取得本地文件对象,我在2.7.4刚开始也是跟你一样的错误,改为下面的就可以了

hadoop里怎么运行java的tar包
第一种:原生态运行jar包
1,利用eclipse编写Map-Reduce方法,一般引入Hadoop-core-1.1.2.jar。注意这里eclipse里没有安装hadoop的插件,只是引入其匝包,该eclipse可以安装在windows或者linux中,如果是在windows中安装的,且在其虚拟机安装的linux,可以通过共享文件夹来实现传递。
2,编写要测试的数据,如命名为tempdata
3,利用eclipse的export来打包已编写好的,在利用eclipse打包jar的时候,只需要选择src即可,一般只打包程序文件,并且需要选择main class,将该jar放到如/home/hadoop/docum/Test.jar
4,将要分析的数据传到hdfs上
hadoop fs -put /home/hadoop/myhadoopdata/tempdata ./testdata/
5,开始执行jar
hadoop jar /home/hadoop/Docum/Test.jar /user/hadoop/temperatur output
这是一种利用jar来运行的。
这里Test.jar在本地,jar没必要上传到hdfs上
参数依次为
本地mapred程序,hdfs的测试数据即输入文件,输出文件夹。
hadoop jar /home/hadoop/Temperature.jar inputpath outputpath
注意:这里可以不需要指定类的名称,而输出的文件夹outputpath不能事先已经存在。
第二种:伪分布式下运行WordCount
1,拷贝源代码
cp /usr/local/hadoop1.1.2/src/examples/org/apache/hadoop/examples/WordCount.java ~/ygch/hadoop/
2,编译源代码,放到指定的文件夹如这里的class下
javac
-classpath /usr/local/hadoop1.1.2/hadoop-core1.1.2.jar:
/usr/local/hadoop1.1.2/lib/commons-cli-1.2.jarWordCount.java -d class,
利用-classpath选项指定WordCount需要的jar包。hadoop目录下存放jar包的位置有两个:根目录和/lib目录。然后我们可以通过jar tvf *.jar查看jar包内容,进而可以知道WordCount需要的是哪几个jar包。
-d选项指定生成的类文件的位置,在编译的时候这个选项必须重新指定,不能让class文件生成在当前目录下。
3,将class文件打包成一个jar包:
jar cvf WordCount.jar -C classes/ .
注意不要忘记最后有一个点.,这个点点必须和前面要有空格,否则jar命令报错。该点指示jar命令的文件列表,表示指定目录下的所有文件。
4,生成input文件:
由于运行hadoop时指定的输入文件只能是HDFS文件系统中的文件,所以我们必须将要进行wordcount的文件从本地文件系统拷贝到HDFS文件系统中。
hadoop fs -mkdir input
hadoop fs -put testfile input
5. 运行jar包:
hadoop jar WordCount.jar org.apache.hadoop.examples.WordCount input output
在运行命令中由于WordCount.java中包括package信息,所以我们在指定类时要包含package的完整信息。
6. 查看结果
在hadoop程序运行完后,结果会放在output目录下,该目录是自动生成的。查看命令为:
hadoop fs -cat output/part-r-00000
使用-cat或者-text都行
第三种Hadoop直接执行Class文件
可以事先在eclipse中编译好class,然后直接利用hadoop来执行该class文件
在Hadoop集群中运行作业的时候,必须要将程序打包为jar文件。
在Hadoop本地和伪分布中可以运行jar文件,也可以直接运行class文件,注意直接运行class文件,必须是没有map和reducer的,直接获取FileSystem来进行操作。
如果类有包名,拷贝的时候也要将其包名拷贝,然后
编写一个JAVA类方法,通过该方法可以获取出存储在HDFS集群中根目录的所有文件?
public void listMyFile() throws Exception {
//获取FileSystem
//"hdfs"为伪造用户,使用hdfs用户进行访问
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.83.141:8020"), new Configuration(), "hdfs");
//获取指定目标目录下的所有文件信息
RemoteIteratorLocatedFileStatus iterator =
fileSystem.listFiles(new Path("/"), true);
//遍历迭代器
while (iterator.hasNext()) {
//获取每个文件详细信息
LocatedFileStatus fileStatus = iterator.next();
//获取每个文件的存储路径
System.out.println("路径:" + fileStatus.getPath() +
"---" + fileStatus.getPath().getName());
//获取文件的block存储信息
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
//打印每个文件的block数
System.out.println("block数量:" + blockLocations.length);
//打印每一个block副本的存储位置
for (BlockLocation blockLocation : blockLocations) {
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println("主机:" + host);
}
}
}
}
使用Java API操作HDFS时,_方法用于获取文件列表?
当使用 Java API 操作 HDFS 时,可以使用 FileSystem.listFiles() 方法来获取文件列表。该方法接受一个 Path 对象,表示要列举文件的目录,并返回一个 RemoteIteratorLocatedFileStatus 对象,该对象可用于迭代目录中的文件。
例如,下面的代码演示了如何使用 listFiles() 方法来获取 HDFS 上的文件列表:
// 定义 HDFS 连接配置
Configuration conf = new Configuration();
// 获取 HDFS FileSystem 对象
FileSystem fs = FileSystem.get(conf);
// 定义要列举文件的目录
Path dirPath = new Path("/user/hadoop");
// 获取文件列表
RemoteIteratorLocatedFileStatus fileIter = fs.listFiles(dirPath, true);
// 遍历文件列表
while (fileIter.hasNext()) {
// 获取当前文件信息
LocatedFileStatus fileStatus = fileIter.next();
// 输出文件名称和大小
System.out.println(fileStatus.getPath().getName() + " : " + fileStatus.getLen());
}
hdfsjava的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于hdfsjava api下载文件到本地实验报告、hdfsjava的信息别忘了在本站进行查找喔。
