更新时间:2022-07-19 GMT+08:00
分享

HTTP REST API

功能简介

REST应用开发代码样例中所涉及的文件操作主要包括创建文件、读写文件、追加文件、删除文件。完整和详细的接口请参考官网上的描述以了解其使用:http://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-hdfs/WebHDFS.html

准备运行环境

  1. 安装客户端。在节点上安装客户端,如安装到“/opt/client”目录,可安装方法可参考《MapReduce服务用户指南》的“客户端管理”章节。

    1. 确认服务端HDFS组件已经安装,并正常运行。
    2. 客户端运行环境已安装1.7或1.8版本的JDK。
    3. 获取并解压缩安装包“MRS_HDFS_Client.tar”。执行如下命令解压。

      tar -xvf MRS_HDFS_Client.tar

      tar -xvf MRS_HDFS_ClientConfig.tar

      由于不兼容老版本客户端,建议用户获取与服务端集群相同版本的客户端安装包进行安装部署。

    4. 进入解压文件夹,即“MRS_HDFS_ClientConfig”,执行下列命令安装客户端。

      sh install.sh /opt/client

      其中“/opt/client”为用户自定义路径,此处仅为举例。

    5. 进入客户端安装目录/opt/client,执行下列命令初始化环境变量。

      source bigdata_env

    6. 执行下列命令进行用户认证,这里以hdfs为例,用户可根据实际用户名修改(普通集群请跳过此步操作)。
      kinit hdfs

      kinit一次的时效24小时。24小时后再次运行样例,需要重新执行kinit

    7. 在客户端目录准备文件“testFile”“testFileAppend”,文件内容分别“Hello, webhdfs user!”和“Welcome back to webhdfs!”,执行如下命令准备文件。

      touch testFile

      vi testFile

      写入“Hello, webhdfs user!”保存退出。

      touch testFileAppend

      vi testFileAppend

      写入“Welcome back to webhdfs!”保存退出。

  2. MRS集群默认只支持HTTPS服务访问,若使用HTTPS服务访问,执行3;若使用HTTP服务访问(仅安全集群支持),执行4
  3. 与HTTP服务访问相比,以HTTPS方式访问HDFS时,由于使用了SSL安全加密,需要确保Curl命令所支持的SSL协议在集群中已添加支持。若不支持,可对应修改集群中SSL协议。例如,若Curl仅支持TLSv1协议,修改方法如下。

    登录MRS Manager页面,单击“服务管理 > HDFS > 服务配置”,在“参数类别”选择“全部配置”,在“搜索”框里搜索“hadoop.ssl.enabled.protocols”,查看参数值是否包含“TLSv1”,若不包含,则在配置项“hadoop.ssl.enabled.protocols”中追加“,TLSv1”。清空“ssl.server.exclude.cipher.list”配置项的值 ,否则以HTTPS访问不了HDFS。单击“保存配置”,并勾选“重新启动受影响的服务或实例。”,单击“是”,重启HDFS服务。

    TLSv1协议存在安全漏洞,请谨慎使用。

  4. 登录MRS Manager页面,单击“服务管理 > HDFS > 服务配置”,在“参数类别”选择“全部配置”,在“搜索”框里搜索“dfs.http.policy”,然后勾选“HTTP_AND_HTTPS”,单击“保存配置”,并勾选“重新启动受影响的服务或实例。”,单击“是”,重启HDFS服务。

操作步骤

  1. 登录MRS Manager,单击“服务管理”,选择“HDFS”,单击进入HDFS服务状态页面。

    由于webhdfs是http/https访问的,需要主NameNode的IP和http/https端口。

    1. 单击“实例”,进入图1界面,找到“NameNode(hacluster,主)”的主机名(host)和对应的IP。
      图1 HDFS实例
    2. 单击“服务配置”,进入图2界面,找到“namenode.http.port”(9870)和“namenode.https.port”(9871)。

      MRS 1.9.2以下版本,上述端口默认值分别为25002和25003,详见MRS用户指南,组件端口信息章节。

      图2 HDFS服务配置

  2. 参考如下链接,创建目录。

    http://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Make_a_Directory

    单击链接,如图3所示。

    图3 创建目录样例命令

    进入到客户端的安装目录下,此处为“/opt/client”,创建名为“huawei”的目录。

    1. 执行下列命令,查看当前是否存在名为“huawei”的目录。

      hdfs dfs -ls /

      执行结果如下。

      linux1:/opt/client # hdfs dfs -ls /
      16/04/22 16:10:02 INFO hdfs.PeerCache: SocketCache disabled.
      Found 7 items
      -rw-r--r--   3 hdfs   supergroup          0 2016-04-20 18:03 /PRE_CREATE_DIR.SUCCESS
      drwxr-x---   - flume  hadoop              0 2016-04-20 18:02 /flume
      drwx------   - hbase  hadoop              0 2016-04-22 15:19 /hbase
      drwxrwxrwx   - mapred hadoop              0 2016-04-20 18:02 /mr-history
      drwxrwxrwx   - spark  supergroup          0 2016-04-22 15:19 /sparkJobHistory
      drwxrwxrwx   - hdfs   hadoop              0 2016-04-22 14:51 /tmp
      drwxrwxrwx   - hdfs   hadoop              0 2016-04-22 14:50 /user

      当前路径下不存在“huawei”目录。

    2. 执行图3中的命令创建以“huawei”为名的目录。其中,用1中查找到的主机名或IP和端口分别替代命令中的<HOST>和<PORT>,在<PATH>中输入想要创建的目录“huawei”

      用主机名或IP代替<HOST>都可以,要注意HTTP和HTTPS的端口不同。

      • 执行下列命令访问HTTP。
        linux1:/opt/client # curl -i -X PUT --negotiate -u: "http://linux1:9870/webhdfs/v1/huawei?op=MKDIRS"

        其中linux1表示<HOST>,9870表示<PORT>。

      • 运行结果。
        HTTP/1.1 401 Authentication required
        Date: Thu, 05 May 2016 03:10:09 GMT
        Pragma: no-cache
        Date: Thu, 05 May 2016 03:10:09 GMT
        Pragma: no-cache
        X-Frame-Options: SAMEORIGIN
        WWW-Authenticate: Negotiate
        Set-Cookie: hadoop.auth=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; HttpOnly
        Content-Length: 0
        HTTP/1.1 200 OK
        Cache-Control: no-cache
        Expires: Thu, 05 May 2016 03:10:09 GMT
        Date: Thu, 05 May 2016 03:10:09 GMT
        Pragma: no-cache
        Expires: Thu, 05 May 2016 03:10:09 GMT
        Date: Thu, 05 May 2016 03:10:09 GMT
        Pragma: no-cache
        Content-Type: application/json
        X-Frame-Options: SAMEORIGIN
        WWW-Authenticate: Negotiate YGoGCSqGSIb3EgECAgIAb1swWaADAgEFoQMCAQ+iTTBLoAMCARKiRARCArhuv39Ttp6lhBlG3B0JAmFjv9weLp+SGFI+t2HSEHN6p4UVWKKy/kd9dKEgNMlyDu/o7ytzs0cqMxNsI69WbN5H
        Set-Cookie: hadoop.auth="u=hdfs&p=hdfs@HADOOP.COM&t=kerberos&e=1462453809395&s=wiRF4rdTWpm3tDST+a/Sy0lwgA4="; Path=/; Expires=Thu, 05-May-2016 13:10:09 GMT; HttpOnly
        Transfer-Encoding: chunked
        {"boolean":true}linux1:/opt/client # 

        返回值{"boolean":true}说明创建成功。

      • 执行下列命令访问HTTPS。
        linux1:/opt/client # curl -i -k -X PUT --negotiate -u: "https://linux1:9871/webhdfs/v1/huawei?op=MKDIRS"

        其中linux1表示<HOST>,9871表示<PORT>。

      • 运行结果。
        HTTP/1.1 401 Authentication required
        Date: Fri, 22 Apr 2016 08:13:37 GMT
        Pragma: no-cache
        Date: Fri, 22 Apr 2016 08:13:37 GMT
        Pragma: no-cache
        X-Frame-Options: SAMEORIGIN
        WWW-Authenticate: Negotiate
        Set-Cookie: hadoop.auth=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Secure; HttpOnly
        Content-Length: 0
        HTTP/1.1 200 OK
        Cache-Control: no-cache
        Expires: Fri, 22 Apr 2016 08:13:37 GMT
        Date: Fri, 22 Apr 2016 08:13:37 GMT
        Pragma: no-cache
        Expires: Fri, 22 Apr 2016 08:13:37 GMT
        Date: Fri, 22 Apr 2016 08:13:37 GMT
        Pragma: no-cache
        Content-Type: application/json
        X-Frame-Options: SAMEORIGIN
        WWW-Authenticate: Negotiate YGoGCSqGSIb3EgECAgIAb1swWaADAgEFoQMCAQ+iTTBLoAMCARKiRARCugB+yT3Y+z8YCRMYJHXF84o1cyCfJq157+NZN1gu7D7yhMULnjr+7BuUdEcZKewFR7uD+DRiMY3akg3OgU45xQ9R
        Set-Cookie: hadoop.auth="u=hdfs&p=hdfs@HADOOP.COM&t=kerberos&e=1461348817963&s=sh57G7iVccX/Aknoz410yJPTLHg="; Path=/; Expires=Fri, 22-Apr-2016 18:13:37 GMT; Secure; HttpOnly
        Transfer-Encoding: chunked
        
        {"boolean":true}linux1:/opt/client # 

        返回值{"boolean":true}说明创建成功。

    3. 再执行下列命令进行查看,可以看到路径下出现“huawei”目录。
      linux1:/opt/client # hdfs dfs -ls /
      16/04/22 16:14:25 INFO hdfs.PeerCache: SocketCache disabled.
      Found 8 items
      -rw-r--r--   3 hdfs   supergroup          0 2016-04-20 18:03 /PRE_CREATE_DIR.SUCCESS
      drwxr-x---   - flume  hadoop              0 2016-04-20 18:02 /flume
      drwx------   - hbase  hadoop              0 2016-04-22 15:19 /hbase
      drwxr-xr-x   - hdfs  supergroup          0 2016-04-22 16:13 /huawei
      drwxrwxrwx   - mapred hadoop              0 2016-04-20 18:02 /mr-history
      drwxrwxrwx   - spark  supergroup          0 2016-04-22 16:12 /sparkJobHistory
      drwxrwxrwx   - hdfs   hadoop              0 2016-04-22 14:51 /tmp
      drwxrwxrwx   - hdfs   hadoop              0 2016-04-22 16:10 /user

  3. 创建请求上传命令,获取集群分配的可写入DataNode节点地址的信息Location。

    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl -i -X PUT --negotiate -u: "http://linux1:9870/webhdfs/v1/huawei/testHdfs?op=CREATE"

      其中linux1表示<HOST>,9870表示<PORT>。

    • 运行结果。
      HTTP/1.1 401 Authentication required
      Date: Thu, 05 May 2016 06:09:48 GMT
      Pragma: no-cache
      Date: Thu, 05 May 2016 06:09:48 GMT
      Pragma: no-cache
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate
      Set-Cookie: hadoop.auth=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; HttpOnly
      Content-Length: 0
      
      HTTP/1.1 307 TEMPORARY_REDIRECT
      Cache-Control: no-cache
      Expires: Thu, 05 May 2016 06:09:48 GMT
      Date: Thu, 05 May 2016 06:09:48 GMT
      Pragma: no-cache
      Expires: Thu, 05 May 2016 06:09:48 GMT
      Date: Thu, 05 May 2016 06:09:48 GMT
      Pragma: no-cache
      Content-Type: application/octet-stream
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate YGoGCSqGSIb3EgECAgIAb1swWaADAgEFoQMCAQ+iTTBLoAMCARKiRARCzQ6w+9pNzWCTJEdoU3z9xKEyg1JQNka0nYaB9TndvrL5S0neAoK2usnictTFnqIincAjwB6SnTtht8Q16WDlHJX/
      Set-Cookie: hadoop.auth="u=hdfs&p=hdfs@HADOOP.COM&t=kerberos&e=1462464588403&s=qry87vAyYzSn9VsS6Rm6vKLhKeU="; Path=/; Expires=Thu, 05-May-2016 16:09:48 GMT; HttpOnly
      Location: http://linux1:25010/webhdfs/v1/huawei/testHdfs?op=CREATE&delegation=HgAFYWRtaW4FYWRtaW4AigFUf4lZdIoBVKOV3XQOCBSyXvFAp92alcRs4j-KNulnN6wUoBJXRUJIREZTIGRlbGVnYXRpb24UMTAuMTIwLjE3Mi4xMDk6MjUwMDA&namenoderpcaddress=hacluster&overwrite=false
      Content-Length: 0
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -i -k -X PUT --negotiate -u: "https://linux1:9871/webhdfs/v1/huawei/testHdfs?op=CREATE"

      其中linux1表示<HOST>,9871表示<PORT>。

    • 运行结果。
      HTTP/1.1 401 Authentication required
      Date: Thu, 05 May 2016 03:46:18 GMT
      Pragma: no-cache
      Date: Thu, 05 May 2016 03:46:18 GMT
      Pragma: no-cache
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate
      Set-Cookie: hadoop.auth=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Secure; HttpOnly
      Content-Length: 0
      
      HTTP/1.1 307 TEMPORARY_REDIRECT
      Cache-Control: no-cache
      Expires: Thu, 05 May 2016 03:46:18 GMT
      Date: Thu, 05 May 2016 03:46:18 GMT
      Pragma: no-cache
      Expires: Thu, 05 May 2016 03:46:18 GMT
      Date: Thu, 05 May 2016 03:46:18 GMT
      Pragma: no-cache
      Content-Type: application/octet-stream
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate YGoGCSqGSIb3EgECAgIAb1swWaADAgEFoQMCAQ+iTTBLoAMCARKiRARCZMYR8GGUkn7pPZaoOYZD5HxzLTRZ71angUHKubW2wC/18m9/OOZstGQ6M1wH2pGriipuCNsKIfwP93eO2Co0fQF3
      Set-Cookie: hadoop.auth="u=hdfs&p=hdfs@HADOOP.COM&t=kerberos&e=1462455978166&s=F4rXUwEevHZze3PR8TxkzcV7RQQ="; Path=/; Expires=Thu, 05-May-2016 13:46:18 GMT; Secure; HttpOnly
      Location: https://linux1:9865/webhdfs/v1/huawei/testHdfs?op=CREATE&delegation=HgAFYWRtaW4FYWRtaW4AigFUfwX3t4oBVKMSe7cCCBSFJTi9j7X64QwnSz59TGFPKFf7GhNTV0VCSERGUyBkZWxlZ2F0aW9uFDEwLjEyMC4xNzIuMTA5OjI1MDAw&namenoderpcaddress=hacluster&overwrite=false
      Content-Length: 0

  4. 根据获取的Location地址信息,可在HDFS文件系统上创建“/huawei/testHdfs”文件,并将本地“testFile”中的内容上传至“testHdfs”文件。

    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl -i -X PUT -T testFile --negotiate -u: "http://linux1:9864/webhdfs/v1/huawei/testHdfs?op=CREATE&delegation=HgAFYWRtaW4FYWRtaW4AigFUf4lZdIoBVKOV3XQOCBSyXvFAp92alcRs4j-KNulnN6wUoBJXRUJIREZTIGRlbGVnYXRpb24UMTAuMTIwLjE3Mi4xMDk6MjUwMDA&namenoderpcaddress=hacluster&overwrite=false"

      其中linux1表示<HOST>,9864表示<PORT>。

    • 运行结果。
      HTTP/1.1 100 Continue
      HTTP/1.1 201 Created
      Location: hdfs://hacluster/huawei/testHdfs
      Content-Length: 0
      Connection: close
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -i -k -X PUT -T testFile --negotiate -u: "https://linux1:9865/webhdfs/v1/huawei/testHdfs?op=CREATE&delegation=HgAFYWRtaW4FYWRtaW4AigFUfwX3t4oBVKMSe7cCCBSFJTi9j7X64QwnSz59TGFPKFf7GhNTV0VCSERGUyBkZWxlZ2F0aW9uFDEwLjEyMC4xNzIuMTA5OjI1MDAw&namenoderpcaddress=hacluster&overwrite=false"
    • 运行结果。
      HTTP/1.1 100 Continue
      HTTP/1.1 201 Created
      Location: hdfs://hacluster/huawei/testHdfs
      Content-Length: 0
      Connection: close

  5. 打开“/huawei/testHdfs”文件,并读取文件中上传写入的内容。

    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl -L --negotiate -u: "http://linux1:9870/webhdfs/v1/huawei/testHdfs?op=OPEN"

      其中linux1表示<HOST>,9870表示<PORT>。

    • 运行结果。
      Hello, webhdfs user!
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -k -L --negotiate -u: "https://linux1:9871/webhdfs/v1/huawei/testHdfs?op=OPEN"

      其中linux1表示<HOST>,9871表示<PORT>。

    • 运行结果。
      Hello, webhdfs user!

  6. 创建请求追加文件的命令,获取集群为已存在“/huawei/testHdfs”文件分配的可写入DataNode节点地址信息Location。

    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl -i -X POST --negotiate -u: "http://linux1:9870/webhdfs/v1/huawei/testHdfs?op=APPEND"

      其中linux1表示<HOST>,9870表示<PORT>。

    • 运行结果。
      HTTP/1.1 401 Authentication required
      Cache-Control: must-revalidate,no-cache,no-store
      Date: Thu, 05 May 2016 05:35:02 GMT
      Pragma: no-cache
      Date: Thu, 05 May 2016 05:35:02 GMT
      Pragma: no-cache
      Content-Type: text/html; charset=iso-8859-1
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate
      Set-Cookie: hadoop.auth=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; HttpOnly
      Content-Length: 1349
      
      HTTP/1.1 307 TEMPORARY_REDIRECT
      Cache-Control: no-cache
      Expires: Thu, 05 May 2016 05:35:02 GMT
      Date: Thu, 05 May 2016 05:35:02 GMT
      Pragma: no-cache
      Expires: Thu, 05 May 2016 05:35:02 GMT
      Date: Thu, 05 May 2016 05:35:02 GMT
      Pragma: no-cache
      Content-Type: application/octet-stream
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate YGoGCSqGSIb3EgECAgIAb1swWaADAgEFoQMCAQ+iTTBLoAMCARKiRARCTYvNX/2JMXhzsVPTw3Sluox6s/gEroHH980xMBkkYlCnO3W+0fM32c4/F98U5bl5dzgoolQoBvqq/EYXivvR12WX
      Set-Cookie: hadoop.auth="u=hdfs&p=hdfs@HADOOP.COM&t=kerberos&e=1462462502626&s=et1okVIOd7DWJ/LdhzNeS2wQEEY="; Path=/; Expires=Thu, 05-May-2016 15:35:02 GMT; HttpOnly
      Location: http://linux1:9864/webhdfs/v1/huawei/testHdfs?op=APPEND&delegation=HgAFYWRtaW4FYWRtaW4AigFUf2mGHooBVKN2Ch4KCBRzjM3jwSMlAowXb4dhqfKB5rT-8hJXRUJIREZTIGRlbGVnYXRpb24UMTAuMTIwLjE3Mi4xMDk6MjUwMDA&namenoderpcaddress=hacluster
      Content-Length: 0
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -i -k -X POST --negotiate -u: "https://linux1:9871/webhdfs/v1/huawei/testHdfs?op=APPEND"

      其中linux1表示<HOST>,9871表示<PORT>。

    • 运行结果。
      HTTP/1.1 401 Authentication required
      Cache-Control: must-revalidate,no-cache,no-store
      Date: Thu, 05 May 2016 05:20:41 GMT
      Pragma: no-cache
      Date: Thu, 05 May 2016 05:20:41 GMT
      Pragma: no-cache
      Content-Type: text/html; charset=iso-8859-1
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate
      Set-Cookie: hadoop.auth=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Secure; HttpOnly
      Content-Length: 1349
      
      HTTP/1.1 307 TEMPORARY_REDIRECT
      Cache-Control: no-cache
      Expires: Thu, 05 May 2016 05:20:41 GMT
      Date: Thu, 05 May 2016 05:20:41 GMT
      Pragma: no-cache
      Expires: Thu, 05 May 2016 05:20:41 GMT
      Date: Thu, 05 May 2016 05:20:41 GMT
      Pragma: no-cache
      Content-Type: application/octet-stream
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate YGoGCSqGSIb3EgECAgIAb1swWaADAgEFoQMCAQ+iTTBLoAMCARKiRARCXgdjZuoxLHGtM1oyrPcXk95/Y869eMfXIQV5UdEwBZ0iQiYaOdf5+Vk7a7FezhmzCABOWYXPxEQPNugbZ/yD5VLT
      Set-Cookie: hadoop.auth="u=hdfs&p=hdfs@HADOOP.COM&t=kerberos&e=1462461641713&s=tGwwOH9scmnNtxPjlnu28SFtex0="; Path=/; Expires=Thu, 05-May-2016 15:20:41 GMT; Secure; HttpOnly
      Location: https://linux1:9865/webhdfs/v1/huawei/testHdfs?op=APPEND&delegation=HgAFYWRtaW4FYWRtaW4AigFUf1xi_4oBVKNo5v8HCBSE3Fg0f_EwtFKKlODKQSM2t32CjhNTV0VCSERGUyBkZWxlZ2F0aW9uFDEwLjEyMC4xNzIuMTA5OjI1MDAw&namenoderpcaddress=hacluster

  7. 根据获取的Location地址信息,可将本地“testFileAppend”文件中的内容追加到HDFS文件系统上的“/huawei/testHdfs”文件。

    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl -i -X POST -T testFileAppend --negotiate -u: "http://linux1:9864/webhdfs/v1/huawei/testHdfs?op=APPEND&delegation=HgAFYWRtaW4FYWRtaW4AigFUf2mGHooBVKN2Ch4KCBRzjM3jwSMlAowXb4dhqfKB5rT-8hJXRUJIREZTIGRlbGVnYXRpb24UMTAuMTIwLjE3Mi4xMDk6MjUwMDA&namenoderpcaddress=hacluster"

      其中linux1表示<HOST>,9864表示<PORT>。

    • 运行结果。
      HTTP/1.1 100 Continue
      HTTP/1.1 200 OK
      Content-Length: 0
      Connection: close
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -i -k -X POST -T testFileAppend --negotiate -u: "https://linux1:9865/webhdfs/v1/huawei/testHdfs?op=APPEND&delegation=HgAFYWRtaW4FYWRtaW4AigFUf1xi_4oBVKNo5v8HCBSE3Fg0f_EwtFKKlODKQSM2t32CjhNTV0VCSERGUyBkZWxlZ2F0aW9uFDEwLjEyMC4xNzIuMTA5OjI1MDAw&namenoderpcaddress=hacluster"

      其中linux1表示<HOST>,9865表示<PORT>。

    • 运行结果。
      HTTP/1.1 100 Continue
      HTTP/1.1 200 OK
      Content-Length: 0
      Connection: close

  8. 打开“/huawei/testHdfs”文件,并读取文件中全部的内容。

    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl -L --negotiate -u: "http://linux1:9870/webhdfs/v1/huawei/testHdfs?op=OPEN"

      其中linux1表示<HOST>,9870表示<PORT>。

    • 运行结果。
      Hello, webhdfs user!
      Welcome back to webhdfs!
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -k -L --negotiate -u: "https://linux1:9871/webhdfs/v1/huawei/testHdfs?op=OPEN"

      其中linux1表示<HOST>,9871表示<PORT>。

    • 运行结果。
      Hello, webhdfs user!
      Welcome back to webhdfs!

  9. 可列出文件系统上“huawei”目录下所有目录和文件的详细信息。

    LISTSTATUS将在一个请求中返回所有子文件和文件夹的信息。
    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl --negotiate -u: "http://linux1:9870/webhdfs/v1/huawei/testHdfs?op=LISTSTATUS"

      其中linux1表示<HOST>,9870表示<PORT>。

    • 运行结果。
      {"FileStatuses":{"FileStatus":[
      {"accessTime":1462425245595,"blockSize":134217728,"childrenNum":0,"fileId":17680,"group":"supergroup","length":70,"modificationTime":1462426678379,"owner":"hdfs","pathSuffix":"","permission":"755","replication":3,"storagePolicy":0,"type":"FILE"}
      ]}}
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -k --negotiate -u: "https://linux1:9871/webhdfs/v1/huawei/testHdfs?op=LISTSTATUS"

      其中linux1表示<HOST>,9871表示<PORT>。

    • 运行结果。
      {"FileStatuses":{"FileStatus":[
      {"accessTime":1462425245595,"blockSize":134217728,"childrenNum":0,"fileId":17680,"group":"supergroup","length":70,"modificationTime":1462426678379,"owner":"hdfs","pathSuffix":"","permission":"755","replication":3,"storagePolicy":0,"type":"FILE"}
      ]}}

    带有大小参数和startafter参数的LISTSTATUS将有助于通过多个请求获取子文件和文件夹信息,从而避免获取大量子文件和文件夹信息时,用户界面变慢。

    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl --negotiate -u: "http://linux1:9870/webhdfs/v1/huawei/?op=LISTSTATUS&startafter=sparkJobHistory&size=1"

      其中linux1表示<HOST>,9870表示<PORT>。

    • 运行结果。
      {"FileStatuses":{"FileStatus":[
      {"accessTime":1462425245595,"blockSize":134217728,"childrenNum":0,"fileId":17680,"group":"supergroup","length":70,"modificationTime":1462426678379,"owner":"hdfs","pathSuffix":"testHdfs","permission":"755","replication":3,"storagePolicy":0,"type":"FILE"}
      ]}}
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -k --negotiate -u: "https://linux1:9871/webhdfs/v1/huawei/?op=LISTSTATUS&startafter=sparkJobHistory&size=1"

      其中linux1表示<HOST>,9871表示<PORT>。

    • 运行结果。
      {"FileStatuses":{"FileStatus":[
      {"accessTime":1462425245595,"blockSize":134217728,"childrenNum":0,"fileId":17680,"group":"supergroup","length":70,"modificationTime":1462426678379,"owner":"hdfs","pathSuffix":"testHdfs","permission":"755","replication":3,"storagePolicy":0,"type":"FILE"}
      ]}}

  10. 删除HDFS上的文件“/huawei/testHdfs”

    • 执行如下命令访问HTTP。
      linux1:/opt/client # curl -i -X DELETE  --negotiate -u: "http://linux1:9870/webhdfs/v1/huawei/testHdfs?op=DELETE"

      其中linux1表示<HOST>,9870表示<PORT>。

    • 运行结果。
      HTTP/1.1 401 Authentication required
      Date: Thu, 05 May 2016 05:54:37 GMT
      Pragma: no-cache
      Date: Thu, 05 May 2016 05:54:37 GMT
      Pragma: no-cache
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate
      Set-Cookie: hadoop.auth=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; HttpOnly
      Content-Length: 0
      HTTP/1.1 200 OK
      Cache-Control: no-cache
      Expires: Thu, 05 May 2016 05:54:37 GMT
      Date: Thu, 05 May 2016 05:54:37 GMT
      Pragma: no-cache
      Expires: Thu, 05 May 2016 05:54:37 GMT
      Date: Thu, 05 May 2016 05:54:37 GMT
      Pragma: no-cache
      Content-Type: application/json
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate YGoGCSqGSIb3EgECAgIAb1swWaADAgEFoQMCAQ+iTTBLoAMCARKiRARC9k0/v6Ed8VlUBy3kuT0b4RkqkNMCrDevsLGQOUQRORkzWI3Wu+XLJUMKlmZaWpP+bPzpx8O2Od81mLBgdi8sOkLw
      Set-Cookie: hadoop.auth="u=hdfs&p=hdfs@HADOOP.COM&t=kerberos&e=1462463677153&s=Pwxe5UIqaULjFb9R6ZwlSX85GoI="; Path=/; Expires=Thu, 05-May-2016 15:54:37 GMT; HttpOnly
      Transfer-Encoding: chunked
      {"boolean":true}linux1:/opt/client # 
    • 执行如下命令访问HTTPS。
      linux1:/opt/client # curl -i -k -X DELETE --negotiate -u: "https://linux1:9871/webhdfs/v1/huawei/testHdfs?op=DELETE"

      其中linux1表示<HOST>,9871表示<PORT>。

    • 运行结果。
      HTTP/1.1 401 Authentication required
      Date: Thu, 05 May 2016 06:20:10 GMT
      Pragma: no-cache
      Date: Thu, 05 May 2016 06:20:10 GMT
      Pragma: no-cache
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate
      Set-Cookie: hadoop.auth=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Secure; HttpOnly
      Content-Length: 0
      HTTP/1.1 200 OK
      Cache-Control: no-cache
      Expires: Thu, 05 May 2016 06:20:10 GMT
      Date: Thu, 05 May 2016 06:20:10 GMT
      Pragma: no-cache
      Expires: Thu, 05 May 2016 06:20:10 GMT
      Date: Thu, 05 May 2016 06:20:10 GMT
      Pragma: no-cache
      Content-Type: application/json
      X-Frame-Options: SAMEORIGIN
      WWW-Authenticate: Negotiate YGoGCSqGSIb3EgECAgIAb1swWaADAgEFoQMCAQ+iTTBLoAMCARKiRARCLY5vrVmgsiH2VWRypc30iZGffRUf4nXNaHCWni3TIDUOTl+S+hfjatSbo/+uayQI/6k9jAfaJrvFIfxqppFtofpp
      Set-Cookie: hadoop.auth="u=hdfs&p=hdfs@HADOOP.COM&t=kerberos&e=1462465210180&s=KGd2SbH/EUSaaeVKCb5zPzGBRKo="; Path=/; Expires=Thu, 05-May-2016 16:20:10 GMT; Secure; HttpOnly
      Transfer-Encoding: chunked
      {"boolean":true}linux1:/opt/client #

密钥管理系统通过HTTP REST API对外提供密钥管理服务,接口请参考官网:

http://hadoop.apache.org/docs/r2.7.2/hadoop-kms/index.html

由于REST API接口做了安全加固,防止脚本注入攻击。通过REST API的接口,无法创建包含 "<script ", "<iframe", "<frame", "javascript:" 这些关键字的目录和文件名。

分享:

    相关文档

    相关产品