hadoop hdfs uri详解 一、hdfs基本命令: hadoop fs -cmd <args> 选项: cmd: 具体的操作,基本上与UNIX的命令行相同 args: 参数 二、hdfs资

hadoop hdfs uri详解

 转自 http://www.cnblogs.com/linjiqin/p/3147837.html

一、hdfs基本命令:
hadoop fs -cmd <args>
选项:
cmd: 具体的操作,基本上与UNIX的命令行相同
args: 参数

二、hdfs资源uri格式:
用法:scheme://authority/path
选项:
scheme–>协议名,file或hdfs
authority–>namenode主机名
path–>路径
范例:hdfs://localhost:54310/user/hadoop/test.txt
假设已经在/home/hadoop/hadoop-1.1.1/conf/core-site.xml里配置了fs.default.name=hdfs://localhost:54310,则仅使用/user/hadoop/test.txt即可。hdfs默认工作目录为/user/$USER,$USER是当前的登录用户名。

三、hdfs命令范例
hadoop fs -mkdir /user/hadoop
hadoop fs -ls /user
hadoop fs -lsr /user(递归的)
hadoop fs -put test.txt /user/hadoop(复制到hdfs://localhost:54310/user/hadoop目录下,首先要创建当前目录)
hadoop fs -get /user/hadoop/test.txt .(复制test.txt文件到本地当前目录下)
hadoop fs -cat /user/hadoop/test.txt
hadoop fs -tail /user/hadoop/test.txt(查看最后1000字节)
hadoop fs -rm /user/hadoop/test.txt
hadoop fs -help ls(查看ls命令的帮助文档)

四、在put时遇到的问题
异常信息: org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot create file/user/hadoopadmin. Name node is in safe mode.
解决办法:hadoop dfsadmin -safemode leave

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你使用的是 `FileStatus` 类的 `getPath()` 方法获取文件路径,那么在 HDFS 中返回的路径格式应该是 `hdfs://<namenode>:<port>/<path>`,而在本地文件系统中返回的路径格式应该是 `file:///path` 或 `file:/path`。 如果你使用 `FileStatus` 类的 `getPath().toUri().getPath()` 方法获取文件路径,那么返回的路径将不包含 `hdfs://namenode:port` 或 `file://` 前缀,只包含文件路径本身。这是因为 `toUri().getPath()` 方法只返回 URI 的路径部分,并去掉了协议部分。 下面是一个示例代码,演示如何获取 HDFS 文件和本地文件的路径: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class FileStatusExample { public static void main(String[] args) throws IOException, URISyntaxException { // HDFS 文件路径 String hdfsPath = "hdfs://namenode:port/user/hadoop/example.txt"; // 本地文件路径 String localPath = "file:///home/user/example.txt"; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI(hdfsPath), conf); // 获取 HDFS 文件状态 Path hdfsFile = new Path(hdfsPath); FileStatus hdfsStatus = fs.getFileStatus(hdfsFile); System.out.println("HDFS 文件路径:" + hdfsStatus.getPath()); // hdfs://namenode:port/user/hadoop/example.txt System.out.println("HDFS 文件路径(不包含前缀):" + hdfsStatus.getPath().toUri().getPath()); // /user/hadoop/example.txt // 获取本地文件状态 Path localFile = new Path(localPath); FileStatus localStatus = fs.getFileStatus(localFile); System.out.println("本地文件路径:" + localStatus.getPath()); // file:/home/user/example.txt System.out.println("本地文件路径(不包含前缀):" + localStatus.getPath().toUri().getPath()); // /home/user/example.txt fs.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值