- 浏览: 114768 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (109)
- hive (5)
- web (1)
- spring (7)
- struts2 (1)
- s2sh (2)
- mysql (3)
- hadoop (31)
- hbase (6)
- java (8)
- ubuntu (8)
- pig (2)
- Interview (2)
- zookeeper (1)
- system (1)
- 遥控 (1)
- linux (3)
- myeclipse (2)
- Oracle (1)
- redis (9)
- ibatis (2)
- 架构 (2)
- 解析xml (1)
- autoProxy (0)
- jedis (6)
- http://www.infoq.com/cn/articles/tq-redis-copy-build-scalable-cluster (1)
- xmemcached (1)
- 图片服务器 (1)
- 对象池 (0)
- netty (1)
最新评论
-
laoma102:
已经不好使了,能找到最新的吗
spring官方文档 -
di1984HIT:
不错,。不错~
pig安装
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.apache.hadoop.io.IOUtils;
public class HDFS_File {
//read the file from HDFS
public void ReadFile(Configuration conf, String FileName){
try{
FileSystem hdfs = FileSystem.get(conf);
FSDataInputStream dis = hdfs.open(new Path(FileName));
IOUtils.copyBytes(dis, System.out, 4096, false);
dis.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//copy the file from HDFS to local
public void GetFile(Configuration conf, String srcFile, String dstFile){
try {
FileSystem hdfs = FileSystem.get(conf);
Path srcPath = new Path(srcFile);
Path dstPath = new Path(dstFile);
hdfs.copyToLocalFile(true,srcPath, dstPath);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//copy the local file to HDFS
public void PutFile(Configuration conf, String srcFile, String dstFile){
try {
FileSystem hdfs = FileSystem.get(conf);
Path srcPath = new Path(srcFile);
Path dstPath = new Path(dstFile);
hdfs.copyFromLocalFile(srcPath, dstPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//create the new file
public FSDataOutputStream CreateFile(Configuration conf, String FileName){
try {
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
FSDataOutputStream outputStream = hdfs.create(path);
return outputStream;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//rename the file name
public boolean ReNameFile(Configuration conf, String srcName, String dstName){
try {
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path fromPath = new Path(srcName);
Path toPath = new Path(dstName);
boolean isRenamed = hdfs.rename(fromPath, toPath);
return isRenamed;
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//delete the file
// tyep = true, delete the directory
// type = false, delece the file
public boolean DelFile(Configuration conf, String FileName, boolean type){
try {
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
boolean isDeleted = hdfs.delete(path, type);
return isDeleted;
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//Get HDFS file last modification time
public long GetFileModTime(Configuration conf, String FileName){
try{
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
FileStatus fileStatus = hdfs.getFileStatus(path);
long modificationTime = fileStatus.getModificationTime();
return modificationTime;
}catch(IOException e){
e.printStackTrace();
}
return 0;
}
//checke if a file exists in HDFS
public boolean CheckFileExist(Configuration conf, String FileName){
try{
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
boolean isExists = hdfs.exists(path);
return isExists;
}catch(IOException e){
e.printStackTrace();
}
return false;
}
//Get the locations of a file in the HDFS cluster
public List<String []> GetFileBolckHost(Configuration conf, String FileName){
try{
List<String []> list = new ArrayList<String []>();
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
FileStatus fileStatus = hdfs.getFileStatus(path);
BlockLocation[] blkLocations = hdfs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
int blkCount = blkLocations.length;
for (int i=0; i < blkCount; i++) {
String[] hosts = blkLocations[i].getHosts();
list.add(hosts);
}
return list;
}catch(IOException e){
e.printStackTrace();
}
return null;
}
//Get a list of all the nodes host names in the HDFS cluster
public String[] GetAllNodeName(Configuration conf){
try{
Configuration config = new Configuration();
FileSystem fs = FileSystem.get(config);
DistributedFileSystem hdfs = (DistributedFileSystem) fs;
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
String[] names = new String[dataNodeStats.length];
for (int i = 0; i < dataNodeStats.length; i++) {
names[i] = dataNodeStats[i].getHostName();
}
return names;
}catch(IOException e){
e.printStackTrace();
}
return null;
}
}
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
public class File_Operation {
public static void main(String args[]){
Configuration conf = new Configuration();
HDFS_File file = new HDFS_File();
//print all the node name
String[] host_name = file.GetAllNodeName(conf);
for (int i = 0; i<host_name.length; i++)
{
System.out.println("the host name:"+host_name);
}
//create the file
String File_Name = "my_test";
FSDataOutputStream fs = file.CreateFile(conf, File_Name);
if (fs != null){
try {
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(File_Name+"create OK");
}
else{
System.out.println(File_Name+"create fail");
}
//check the file exists
if (file.CheckFileExist(conf, File_Name) == true){
System.out.println(File_Name+"the file exists");
}
else{
System.out.println(File_Name+"the file not exists");
}
//delete the file
if (file.DelFile(conf, File_Name, false) == true){
System.out.println(File_Name+"the file delete");
}
else{
System.out.println(File_Name+"the file not delete");
}
//copy the file to HDFS
String srcFile = "/home/jackydai/my";
String ToFile = "/home/jackydai/my11";
String dstFile = "/user/jackydai/my";
file.PutFile(conf, srcFile, dstFile);
System.out.println("copy file ok!");
//check the file last modfiy time
long mod_time = file.GetFileModTime(conf, dstFile);
Date d = new Date(mod_time);
System.out.println("the modefile time"+d);
//get the locations of a file in HDFS
List<String []> list = file.GetFileBolckHost(conf, dstFile);
for (int i = 0; i < list.size(); i++){
for(int j = 0; j < list.get(i).length; j++){
System.out.println("the bolck host name:"+list.get(i)[j]);
}
}
System.out.println("host name over!");
//read the file
file.ReadFile(conf, dstFile);
System.out.println("read over!");
//copy the file to local
file.GetFile(conf, dstFile, ToFile);
System.out.println("copy ok");
}
}
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.apache.hadoop.io.IOUtils;
public class HDFS_File {
//read the file from HDFS
public void ReadFile(Configuration conf, String FileName){
try{
FileSystem hdfs = FileSystem.get(conf);
FSDataInputStream dis = hdfs.open(new Path(FileName));
IOUtils.copyBytes(dis, System.out, 4096, false);
dis.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//copy the file from HDFS to local
public void GetFile(Configuration conf, String srcFile, String dstFile){
try {
FileSystem hdfs = FileSystem.get(conf);
Path srcPath = new Path(srcFile);
Path dstPath = new Path(dstFile);
hdfs.copyToLocalFile(true,srcPath, dstPath);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//copy the local file to HDFS
public void PutFile(Configuration conf, String srcFile, String dstFile){
try {
FileSystem hdfs = FileSystem.get(conf);
Path srcPath = new Path(srcFile);
Path dstPath = new Path(dstFile);
hdfs.copyFromLocalFile(srcPath, dstPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//create the new file
public FSDataOutputStream CreateFile(Configuration conf, String FileName){
try {
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
FSDataOutputStream outputStream = hdfs.create(path);
return outputStream;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//rename the file name
public boolean ReNameFile(Configuration conf, String srcName, String dstName){
try {
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path fromPath = new Path(srcName);
Path toPath = new Path(dstName);
boolean isRenamed = hdfs.rename(fromPath, toPath);
return isRenamed;
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//delete the file
// tyep = true, delete the directory
// type = false, delece the file
public boolean DelFile(Configuration conf, String FileName, boolean type){
try {
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
boolean isDeleted = hdfs.delete(path, type);
return isDeleted;
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//Get HDFS file last modification time
public long GetFileModTime(Configuration conf, String FileName){
try{
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
FileStatus fileStatus = hdfs.getFileStatus(path);
long modificationTime = fileStatus.getModificationTime();
return modificationTime;
}catch(IOException e){
e.printStackTrace();
}
return 0;
}
//checke if a file exists in HDFS
public boolean CheckFileExist(Configuration conf, String FileName){
try{
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
boolean isExists = hdfs.exists(path);
return isExists;
}catch(IOException e){
e.printStackTrace();
}
return false;
}
//Get the locations of a file in the HDFS cluster
public List<String []> GetFileBolckHost(Configuration conf, String FileName){
try{
List<String []> list = new ArrayList<String []>();
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(FileName);
FileStatus fileStatus = hdfs.getFileStatus(path);
BlockLocation[] blkLocations = hdfs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
int blkCount = blkLocations.length;
for (int i=0; i < blkCount; i++) {
String[] hosts = blkLocations[i].getHosts();
list.add(hosts);
}
return list;
}catch(IOException e){
e.printStackTrace();
}
return null;
}
//Get a list of all the nodes host names in the HDFS cluster
public String[] GetAllNodeName(Configuration conf){
try{
Configuration config = new Configuration();
FileSystem fs = FileSystem.get(config);
DistributedFileSystem hdfs = (DistributedFileSystem) fs;
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
String[] names = new String[dataNodeStats.length];
for (int i = 0; i < dataNodeStats.length; i++) {
names[i] = dataNodeStats[i].getHostName();
}
return names;
}catch(IOException e){
e.printStackTrace();
}
return null;
}
}
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
public class File_Operation {
public static void main(String args[]){
Configuration conf = new Configuration();
HDFS_File file = new HDFS_File();
//print all the node name
String[] host_name = file.GetAllNodeName(conf);
for (int i = 0; i<host_name.length; i++)
{
System.out.println("the host name:"+host_name);
}
//create the file
String File_Name = "my_test";
FSDataOutputStream fs = file.CreateFile(conf, File_Name);
if (fs != null){
try {
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(File_Name+"create OK");
}
else{
System.out.println(File_Name+"create fail");
}
//check the file exists
if (file.CheckFileExist(conf, File_Name) == true){
System.out.println(File_Name+"the file exists");
}
else{
System.out.println(File_Name+"the file not exists");
}
//delete the file
if (file.DelFile(conf, File_Name, false) == true){
System.out.println(File_Name+"the file delete");
}
else{
System.out.println(File_Name+"the file not delete");
}
//copy the file to HDFS
String srcFile = "/home/jackydai/my";
String ToFile = "/home/jackydai/my11";
String dstFile = "/user/jackydai/my";
file.PutFile(conf, srcFile, dstFile);
System.out.println("copy file ok!");
//check the file last modfiy time
long mod_time = file.GetFileModTime(conf, dstFile);
Date d = new Date(mod_time);
System.out.println("the modefile time"+d);
//get the locations of a file in HDFS
List<String []> list = file.GetFileBolckHost(conf, dstFile);
for (int i = 0; i < list.size(); i++){
for(int j = 0; j < list.get(i).length; j++){
System.out.println("the bolck host name:"+list.get(i)[j]);
}
}
System.out.println("host name over!");
//read the file
file.ReadFile(conf, dstFile);
System.out.println("read over!");
//copy the file to local
file.GetFile(conf, dstFile, ToFile);
System.out.println("copy ok");
}
}
发表评论
-
mapreduce Bet
2012-04-11 15:00 924import java.io.IOException; imp ... -
hadoop 输出格式
2012-04-05 17:18 729http://blog.csdn.net/dajuezhao/ ... -
hadoop mapreduce 原理
2012-03-31 16:14 699http://www.cnblogs.com/forfutur ... -
hadoop搭建问题
2012-03-30 13:23 810file:///E:/hadoop/搭建/hadoop集群搭建 ... -
hadoop输出文件格式
2012-03-26 10:09 657http://apps.hi.baidu.com/share/ ... -
hadoop 学习
2012-03-26 09:48 658http://hi.baidu.com/shuyan50/bl ... -
hadoop提高性能建议
2012-03-22 22:40 686http://langyu.iteye.com/blog/91 ... -
hadoop例子
2012-03-22 22:09 736http://www.hadoopor.com/thread- ... -
hadoop
2012-04-25 13:16 754精通HADOOP http://blog.csdn.net/ ... -
Hadoop Hive与Hbase整合
2012-03-07 15:02 362http://www.open-open.com/lib/vi ... -
hive hadoop 代码解析
2012-04-25 13:16 797http://www.tbdata.org/archives/ ... -
Hadoop MapReduce操作MySQL
2012-03-05 17:33 894http://www.javabloger.com/artic ... -
hdfs 操作类自己的
2012-03-02 17:57 562package operateFile; import ... -
hadoo 文件常用操作
2012-03-02 15:53 779http://www.360doc.com/content/1 ... -
Mapper,Reducer,Wrapper的Java模板
2012-03-02 08:24 1118http://www.easyigloo.org/?p=114 ... -
hadoop基础知识
2012-03-02 08:00 744http://www.blogjava.net/killme2 ... -
hadoop 自己封装的接口
2012-04-25 13:16 695http://www.360doc.com/content/1 ... -
HadoopFileUtil
2012-03-01 14:42 1840import java.io.File; import jav ... -
hadoop ExtendedFileUtil
2012-03-01 14:34 1056在Hadoop编写生产环境的任务时,定义以下任务,要求是相同的 ... -
hadoop StringUtil
2012-03-01 14:33 863import java.util.*; public cla ...
相关推荐
Java API提供了访问HDFS的接口,例如`org.apache.hadoop.fs.FileSystem`类,可以用于读取、写入和管理文件系统中的文件。 2. **Hadoop MapReduce**:MapReduce是Hadoop用于并行处理和分析大数据的编程模型。在GROUP...
例如,我们可以创建一个`HdfsTemplate`的bean,用于执行HDFS操作。配置中需要提供HDFS的地址、端口以及认证信息等。 3. **编写Java代码**:在Spring环境中,我们可以注入`HdfsTemplate`,然后调用其提供的方法来...
本篇文章将详细讲解Hadoop HDFS文件操作、MapReduce(MR)示例以及如何实现TopN问题。 首先,HDFS是分布式文件系统,设计用于跨大量廉价硬件节点存储和处理大规模数据。它具有高容错性,能够自动数据复制,确保数据...
它提供了一系列API和工具类,用于简化与HDFS的交互,包括文件的上传、下载、删除、目录创建等操作。通过这些工具,用户可以方便地管理和操作存储在HDFS上的文件和目录。 项目的主要特性和功能 1. 文件上传与下载 ...
这需要在项目中引入Hadoop的相关依赖,并使用如`FileSystem`类的API进行操作。 此外,对于大数据开发人员,图形化工具如Hue、Cloudera Manager等也可以提供友好的Web界面,方便在Windows客户端访问HDFS。这些工具...
在Windows平台上,利用Hadoop HDFS(分布式文件系统)处理大量数据已经成为常见的操作。本示例将详述如何使用Eclipse集成开发环境(IDE)的Hadoop插件,执行一个基于HDFS的中文分词任务,对《唐诗三百首》进行分析,...
`FSDirectory`类管理NameNode的文件系统目录,`FSNamesystem`类封装了所有的HDFS元数据操作。 总的来说,Hadoop的HDFS源代码分析涉及了分布式文件系统的实现细节,包括文件的分块、复制策略、元数据管理、故障恢复...
实验内容包括编程实现HDFS操作以及使用Hadoop的Shell命令来完成相同任务。 首先,我们需要理解HDFS的基本操作,如上传文件、追加内容和覆盖文件。这些操作在大数据处理场景中至关重要,因为它们允许我们高效地管理...
在Eclipse中进行HDFS操作,我们需要使用Hadoop的Java API。首先,确保你的开发环境中已经安装了Hadoop并配置了相关的环境变量,包括HADOOP_HOME和PATH。接下来,创建一个新的Java项目,并引入Hadoop的相关依赖库,...
实验二:“熟悉常用的HDFS操作”旨在帮助学习者深入理解Hadoop分布式文件系统(HDFS)在大数据处理中的核心地位,以及如何通过Shell命令和Java API进行高效操作。HDFS在Hadoop架构中扮演着存储大数据的核心角色,为...
JAVA API中包含了Hadoop的HDFS客户端库,它提供了与HDFS通信所需的类和方法,使程序员能够轻松地在应用程序中实现文件系统功能。 综上所述,Hadoop HDFS应用的知识点涵盖了HDFS的分布式文件系统概念、支持的多种...
6. **MyBatis映射**:虽然HDFS操作主要是文件I/O,但在某些场景下,可能需要将文件元数据存储在关系数据库中,以便于管理和查询。MyBatis可以帮助我们方便地完成这些数据库操作。 7. **前端界面**:使用HTML、CSS和...
以上就是使用Java API操作Hadoop HDFS创建目录的完整过程。在实际应用中,你可能需要处理异常,优化错误处理,并根据具体需求进行更复杂的操作,比如读写文件、检查文件是否存在等。通过熟练掌握HDFS的Java API,你...
8. **异常处理**:在实际编程中,需要适当地处理HDFS操作可能抛出的异常,如`FileNotFoundException`、`IOException`等。 9. **最佳实践**:在使用HDFS API时,遵循最佳实践,如批量操作以减少网络开销,使用缓冲区...
它允许应用程序与Hadoop集群通信,执行基本的HDFS操作。 2. `hadoop-hdfs.jar`:这个JAR包包含了HDFS的主要实现。它提供了HDFS的客户端API,供应用程序使用,以便读写HDFS上的文件和目录。 3. `hadoop-common.jar`...
本次实验的主要目标是通过对HDFS(Hadoop Distributed File System)的操作实践,加深学生对HDFS在Hadoop架构中的作用及其基本操作的理解。实验内容包括两大部分:一是通过Shell命令对HDFS进行基本的文件管理操作;...
本篇将深入探讨Hadoop平台上的HDFS,以及如何在该平台上进行文件操作。 一、Hadoop平台基础 Hadoop是基于Java开发的,它主要由两个关键部分组成:HDFS和MapReduce。HDFS为大数据提供高容错性的分布式存储,而...
总的来说,`HdfsService`类提供了一种封装Hadoop HDFS操作的方式,使得开发者能够更加方便地在Java应用程序中集成HDFS的功能。通过使用Hadoop的API,我们可以高效地进行分布式文件系统的操作,从而充分利用Hadoop的...
这些实验步骤旨在帮助学生掌握HDFS的常用操作,包括文件系统的交互、数据读写以及高级功能,如文件追加和目录管理。同时,通过使用Java API,学生将加深对Hadoop生态系统和文件系统API的理解,为后续的大数据处理和...
4. **Configuration**: 这是Hadoop的配置类,包含了HDFS操作所需的参数,如NameNode地址、通信超时等。`Configuration conf = new Configuration();`初始化配置,然后可以使用`conf.set("fs.defaultFS", "hdfs://...