HDFS Java code for CRUD

Source: Internet
Author: User
Keywords HDFS Java code CRUD HDFS CRUD tools
Tags *.h file apache close code configuration create directory eclipse

Reminder: If you want IDE or eclipse IDE to run on the HDFS must give you the directory used to assign permissions to users under windows, for convenience, it is recommended to all permissions 777

Create a directory command

hdfs dfs -mkdir myproject
Assign permissions command hdfs dfs -chmod 777 myproject HDFS Censoring tools package hdfs; 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.util.ArrayList; import java.util.List; / ** * Created by LiuwenSheng on 2017/8/7. * / public class HDFSUtils {private final String FILE_PROTOCOL = "file: ///"; private final String HDFS_PROTOCOL = "hdfs: // xxx: 9000 / myproject"; // where xxx represents your IP address eg 192.168.18.1 / * * * Used to select files from local files to upload to hdfs * @param conf org.apache.hadoop.conf.Configuration Configuration Information * @param localPath String Path to save local files * @param hdfsPath String Path to upload to HDFS eg : /a/b.txt * @throws IOException * / public void file2HDFS (Configuration conf, String localPath, String hdfsPath) throws IOException {FileSystem fs = FileSystem.get (conf); fs.copyFromLocalFile (new Path (FILE_PROTOCOL + loc @param path File Paths * @return * @throws IOException * / public boolean () * / public * boolean isExit (Configuration conf, String path) throws IOException {FileSystem fs = FileSystem.get (conf); boolean exit = fs.exists (new Path (HDFS_PROTOCOL + path)); fs.close (); return exit; * Delete Files * @param conf org.apache.hadoop.conf.Configuration Configuration Information * @param path File Path to Delete * @throws IOException * / public Boolean deleteFormHDFS (Configuration conf, String path) throws IOException {FileSystem fs = FileSystem @get (conf); boolean isDel = fs.deleteOnExit (newPath (HDFS_PROTOCOL + path)); fs.close (); return isDel;} / ** * Download HDFS files to local * @param conf configuration information * @param hdfsPath hdfs file path * @ param localPath local file path * @throws IOException * / public void file2Loacl (Configuration conf, String hdfsPath, String localPath) throws IOException {FileSystem fs Fbs.copyToLocalFile (newPath (HDFS_PROTOCOL + hdfsPath), newPath (FILE_PROTOCOL + localPath)); fs.close ();} / ** * Create hdfs directory * @param conf configuration information * @param path The directory path to create * @return create successfully * @throws IOException * / public Boolean mkHDFSDir (Configuration conf, String path) throws IOException {FileSystem fs = FileSystem.get (conf); Path mypath = new Path (HDFS_PROTOCOL ifmk = fs.mkdirs (mypath);} fs.close (); return ismk;} / ** * View the list of files in the current directory * @param conf configuration information * @param path view directory * @return file status array * @throws IOException * / public List lsHDFSDir (Configuration conf, String path) throws IOException {List list = new ArrayList (); FileSystem fs = FileSystem (FileStatus fileStatus) {list.add (fileStatus.getPath (). toString ());} fs. close (); return list;}} Test case package test; import hdfs.HDFSUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.junit.Before; import org.junit.Test; import javax.ws.rs.core.Context; import java.io.IOException; import java.util.List; / ** * Created by lws on 2017/8/7. * / public class MyHDFSTest {private HDFSUtils hdfsUtils = new @Ffore public void beforeBegining () {conf.set ("fs.defaultFS", "hdfs: // xxx: 9000 /"); // xxx here for you. HDFSUtils (); private Configuration conf = new Configuration Such as 192.168.12.8} @Test public void test_file2HDFS () throws IOException {hdfsUtils.file2HDFS (conf, "D: \\ BugReport.txt", "/ myuser2 / 1");} @Test public void test_deleteFormHDFS ( ) throws IOException {boolean is = hdfsUtils.isExit (conf, "/ myuser / BugReport.txt");} @Test public void test_isExit () throws IOException {hdfsUtils.deleteFormHDFS System.out.print (is);} @Test public void test_file2Local () throws IOException {hdfsUti @test public void test_mkHDFSDir () throws IOException {boolean a = hdfsUtils.mkHDFSDir (conf, "/ myuser / liming") ls.file2Loacl (conf, "/myuser/BugReport.txt", "D: // test" ); System.out.print (a);} @Test public void test_lsHDFSDir () throws IOException {List list = hdfsUtils.lsHDFSDir (conf, "/ myuser2"); for (String file: list) {System.out.println (file);}}}
Related Article

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.