Horse soldier hadoop2.7.3_ using Java to access HDFs

Source: Internet
Author: User

  • Accessing HDFs through a Java program:
    • The HDFS system will store the data used in the Core-site.xml specified by the Hadoop.tmp.dir, which defaults to/tmp/hadoop-${user.name}, because the/tmp directory will be deleted when the system restarts. Therefore, the directory location should be modified. Modify Core-site.xml (modified on all sites)
      12345 <property>    <name>hadoop.tmp.dir</name>    <value>/var/hadoop</value></property>
    • HDFs Namenode-format
    • The permissions system on Windows and the permissions system on Linux, during testing, for simplicity, you can turn off permission checking on Namenode hdfs-site.xml, adding the configuration:
      12345 <property>    <name>dfs.permissions.enabled</name>    <value>false</value></property>
      Restart namenode:hadoop-daemon.sh Stop Namenode, hadoop-daemon.sh start Namenode
  • Reading files from HDFs
    Url.seturlstreamhandlerfactory (New fsurlstreamhandlerfactory ()); InputStream in = new URL ("hdfs:// 192.168.56.100:9000/test.data "). OpenStream (); Ioutils.copybytes (in, System.out, 4096, true);              

  • Learn to use the FileSystem class
    Configuration conf = newConfiguration (); Conf.set ("Fs.defaultfs", "hdfs://192.168.56.100:9000"); FileSystem FileSystem =filesystem.get (conf);                Boolean B = filesystem.exists (New Path ("/hello"));                                System.out.println (b);                Boolean success = Filesystem.mkdirs (New Path ("/mashibing")); SYSTEM.OUT.PRINTLN (Success); Success = Filesystem.delete (New Path ("/mashibing"), true); SYSTEM.OUT.PRINTLN (Success); Fsdataoutputstream out = filesystem.create (new Path ("/test.data"), true); FileInputStream fis = new FileInputStream ("C:/test/core-site.xml"); Ioutils.copybytes (FIS, out, 4096, true); filestatus[] statuses = filesystem.liststatus (new Path ("/"));//system.out.println (statuses.length); for( Filestatus status:statuses) {System.out.println (Status.getpath ()); System.out.println (Status.getpermission ()); System.out.println (Status.getreplication ()); }
  • Thinking of how Baidu network disk implementation?
  • What does Core-site do with it? What does Hdfs-site do with it?

Horse soldier hadoop2.7.3_ using Java to access HDFs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.