当我们安装好并正常运行hdfs后输入http://xxxxxxxxx:50070会进入下图所示的页面。 其中Browse the filesystem 是查看文件系统的入口。
但是在发现这个链接一直无效。通过Chrome的开发工具可以看这个链接访问地址是:nn_browsedfscontent.jsp
下面是nn_browsedfscontent.jsp的代码
1 <%@ page 2 contentType="text/html; charset=UTF-8" 3 import="java.io.*" 4 import="java.security.PrivilegedExceptionAction" 5 import="java.util.*" 6 import="javax.servlet.*" 7 import="javax.servlet.http.*" 8 import="org.apache.hadoop.conf.Configuration" 9 import="org.apache.hadoop.hdfs.*" 10 import="org.apache.hadoop.hdfs.server.namenode.*" 11 import="org.apache.hadoop.hdfs.server.datanode.*" 12 import="org.apache.hadoop.hdfs.protocol.*" 13 import="org.apache.hadoop.hdfs.security.token.delegation.*" 14 import="org.apache.hadoop.io.Text" 15 import="org.apache.hadoop.security.UserGroupInformation" 16 import="org.apache.hadoop.security.token.Token" 17 import="org.apache.hadoop.util.*" 18 import="java.text.DateFormat" 19 import="java.net.InetAddress" 20 import="java.net.URLEncoder" 21 %> 22 <%! 23 static String getDelegationToken(final NameNode nn, 24 HttpServletRequest request, Configuration conf) 25 throws IOException, InterruptedException { 26 final UserGroupInformation ugi = JspHelper.getUGI(request, conf); 27 Token<DelegationTokenIdentifier> token = 28 ugi.doAs( 29 new PrivilegedExceptionAction<Token<DelegationTokenIdentifier>>() 30 { 31 public Token<DelegationTokenIdentifier> run() throws IOException { 32 return nn.getDelegationToken(new Text(ugi.getUserName())); 33 } 34 }); 35 return token.encodeToUrlString(); 36 } 37 38 public void redirectToRandomDataNode( 39 NameNode nn, 40 HttpServletRequest request, 41 HttpServletResponse resp, 42 Configuration conf 43 ) throws IOException, InterruptedException { 44 String tokenString = null; 45 if (UserGroupInformation.isSecurityEnabled()) { 46 tokenString = getDelegationToken(nn, request, conf); 47 } 48 FSNamesystem fsn = nn.getNamesystem(); 49 String datanode = fsn.randomDataNode(); 50 String redirectLocation; 51 String nodeToRedirect; 52 int redirectPort; 53 if (datanode != null) { 54 redirectPort = Integer.parseInt(datanode.substring(datanode.indexOf(':') 55 + 1)); 56 nodeToRedirect = datanode.substring(0, datanode.indexOf(':')); 57 } 58 else { 59 nodeToRedirect = nn.getHttpAddress().getHostName(); 60 redirectPort = nn.getHttpAddress().getPort(); 61 } 62 String fqdn = InetAddress.getByName(nodeToRedirect).getCanonicalHostName(); 63 redirectLocation = "http://" + fqdn + ":" + redirectPort + 64 "/browseDirectory.jsp?namenodeInfoPort=" + 65 nn.getHttpAddress().getPort() + 66 "&dir=/" + 67 (tokenString == null ? "" : 68 JspHelper.getDelegationTokenUrlParam(tokenString)); 69 resp.sendRedirect(redirectLocation); 70 } 71 %> 72 73 <html> 74 75 <title></title> 76 77 <body> 78 <% 79 NameNode nn = (NameNode)application.getAttribute("name.node"); 80 Configuration conf = (Configuration) application.getAttribute(JspHelper.CURRENT_CONF); 81 redirectToRandomDataNode(nn, request, response, conf); 82 %> 83 <hr> 84 85 <h2>Local logs</h2> 86 <a href="/logs/">Log</a> directory 87 88 <% 89 out.println(ServletUtil.htmlFooter()); 90 %>
从代码中可以看出实际是跳转到一台datanode的browseDirectory.jsp,如:
http://xxxxxxx:50075/browseDirectory.jsp?namenodeInfoPort=50070&dir=/
而xxxxxxx是集群中一台datanode的主机名。
那么就可以从以下两个点出发去看为什么无效:
1.50075端口是否正常
2.使用的机器是否能通过主机面访问datanode
检查之后发现自己这两个都没有配置......
最后通过在hdfs-site.xml中添加如下配置
<property>
<name>dfs.datanode.http.address</name>
<value>10.0.0.234:50075</value>
</property>
在hosts中添加datanode的主机命和ip修复了这个问题
记录、共勉。
如果本文对您有帮助,点一下右下角的“推荐”