实现对HDFS增删改查CRUD等操作

1 查找

列出某个目录下的文件名称,hdfs命令如下所示:

hdfs dfs –ls/usr/app

java代码片段:

[plain] view plain copy print?
  1. public void list(String srcPath) {
  2. Configuration conf = new Configuration();
  3. LOG.info("[Defaultfs] :" +conf.get("fs.default.name"));
  4. conf.set("hadoop.job.ugi","app,app");   //It is not necessary for the default user.
  5. FileSystem fs;
  6. try {
  7. fs= FileSystem.get(conf);
  8. RemoteIterator<LocatedFileStatus>rmIterator = fs.listLocatedStatus(new Path(srcPath));
  9. while (rmIterator.hasNext()) {
  10. Path path = rmIterator.next().getPath();
  11. if(fs.isDirectory(path)){
  12. LOG.info("-----------DirectoryName: "+path.getName());
  13. }
  14. else if(fs.isFile(path)){
  15. LOG.info("-----------FileName: "+path.getName());
  16. }
  17. }
  18. }catch (IOException e) {
  19. LOG.error("list fileSysetm object stream.:" , e);
  20. new RuntimeException(e);
  21. }
  22. }

输出结果:

2014-03-11 22:38:15,329 INFO  (com.hdfs.client.SyncDFS:48) ------------File Name: README.txt

2014-03-11 22:38:15,331 INFO  (com.hdfs.client.SyncDFS:45) ------------Directory Name: blog_blogpost

2014-03-11 22:38:15,333 INFO  (com.hdfs.client.SyncDFS:45) ------------Directory Name: test

读取文件中的内容,hdfs命令如下:

hdfs dfs –cat /input

java 代码:

[plain] view plain copy print?
  1. public void readFile(String file){
  2. Configurationconf = new Configuration();
  3. FileSystemfs;
  4. try {
  5. fs= FileSystem.get(conf);
  6. Pathpath = new Path(file);
  7. if(!fs.exists(path)){
  8. LOG.warn("file'"+ file+"' doesn't exist!");
  9. return ;
  10. }
  11. FSDataInputStreamin = fs.open(path);
  12. Stringfilename = file.substring(file.lastIndexOf('/') + 1, file.length());
  13. OutputStreamout = new BufferedOutputStream(new FileOutputStream(
  14. new File(filename)));
  15. byte[] b = new byte[1024];
  16. int numBytes = 0;
  17. while ((numBytes = in.read(b)) > 0) {
  18. out.write(b,0, numBytes);
  19. }
  20. in.close();
  21. out.close();
  22. fs.close();
  23. }catch (IOException e) {
  24. LOG.error("ifExists fs Exception caught! :" , e);
  25. new RuntimeException(e);
  26. }
  27. }

获取文件的修改时间,java代码:

[plain] view plain copy print?
  1. /**
  2. * Gets the information about the file modifiedtime.
  3. * @param source
  4. * @throws IOException
  5. */
  6. public void getModificationTime(String source) throws IOException{
  7. Configurationconf = new Configuration();
  8. FileSystemfs = FileSystem.get(conf);
  9. PathsrcPath = new Path(source);
  10. // Check if the file alreadyexists
  11. if (!(fs.exists(srcPath))) {
  12. System.out.println("No such destination " + srcPath);
  13. return;
  14. }
  15. // Get the filename out of thefile path
  16. Stringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());
  17. FileStatusfileStatus = fs.getFileStatus(srcPath);
  18. long modificationTime =fileStatus.getModificationTime();
  19. LOG.info("modified datetime: " + System.out.format("File %s; Modification time : %0.2f%n",filename,modificationTime));
  20. }

获取文件块定位信息,java代码:

[plain] view plain copy print?
  1. /**
  2. * Gets the file block location info
  3. * @param source
  4. * @throws IOException
  5. */
  6. public void getBlockLocations(String source) throws IOException{
  7. Configurationconf = new Configuration();
  8. FileSystemfs = FileSystem.get(conf);
  9. PathsrcPath = new Path(source);
  10. // Check if the file alreadyexists
  11. if (!(ifExists(source))) {
  12. System.out.println("No such destination " + srcPath);
  13. return;
  14. }
  15. // Get the filename out of thefile path
  16. Stringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());
  17. FileStatusfileStatus = fs.getFileStatus(srcPath);
  18. BlockLocation[]blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
  19. int blkCount = blkLocations.length;
  20. System.out.println("File :" + filename + "stored at:");
  21. for (int i=0; i < blkCount; i++) {
  22. String[]hosts = blkLocations[i].getHosts();
  23. LOG.info("host ip:" +System.out.format("Host %d: %s %n", i, hosts));
  24. }
  25. }

获取Hadoop集群中data node的DNS主机名,java代码:

[plain] view plain copy print?
  1. public void getHostnames () throwsIOException{
  2. Configurationconfig = new Configuration();
  3. FileSystemfs = FileSystem.get(config);
  4. DistributedFileSystemhdfs = (DistributedFileSystem) fs;
  5. DatanodeInfo[]dataNodeStats = hdfs.getDataNodeStats();
  6. String[]names = new String[dataNodeStats.length];
  7. for (int i = 0; i < dataNodeStats.length; i++) {
  8. names[i]= dataNodeStats[i].getHostName();
  9. LOG.info("datenode hostname:"+(dataNodeStats[i].getHostName()));
  10. }
  11. }

2 创建

创建一个目录,指定具体的文件路径。hdfs命令如下:

[plain] view plain copy print?
  1. hdfs dfs –mkdir/usr/app/tmp

java代码:

[java] view plain copy print?
  1. public void mkdir(String dir){
  2. Configurationconf = new Configuration();
  3. FileSystemfs = null;
  4. try {
  5. fs= FileSystem.get(conf);
  6. Pathpath = new Path(dir);
  7. if(!fs.exists(path)){
  8. fs.mkdirs(path);
  9. LOG.debug("create directory '"+dir+"' successfully!");
  10. }else{
  11. LOG.debug("directory '"+dir+"' exits!");
  12. }
  13. }catch (IOException e) {
  14. LOG.error("FileSystem get configuration with anerror");
  15. e.printStackTrace();
  16. }finally{
  17. if(fs!= null){
  18. try {
  19. fs.close();
  20. }catch (IOException e) {
  21. LOG.error("close fs object stream. :" , e);
  22. new RuntimeException(e);
  23. }
  24. }
  25. }
  26. }

将本地文件上传到hdfs上去,java代码如下:

[plain] view plain copy print?
  1. public void copyFromLocal (String source, String dest) {
  2. Configurationconf = new Configuration();
  3. FileSystemfs;
  4. try {
  5. fs= FileSystem.get(conf);
  6. PathsrcPath = new Path(source);
  7. PathdstPath = new Path(dest);
  8. // Check if the file alreadyexists
  9. if (!(fs.exists(dstPath))) {
  10. LOG.warn("dstPathpath doesn't exist" );
  11. LOG.error("No such destination " + dstPath);
  12. return;
  13. }
  14. // Get the filename out of thefile path
  15. Stringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());
  16. try{
  17. //if the file exists in thedestination path, it will throw exception.
  18. //                                   fs.copyFromLocalFile(srcPath,dstPath);
  19. //remove and overwrite files withthe method
  20. //copyFromLocalFile(booleandelSrc, boolean overwrite, Path src, Path dst)
  21. fs.copyFromLocalFile(false, true, srcPath, dstPath);
  22. LOG.info("File " + filename + "copied to " + dest);
  23. }catch(Exception e){
  24. LOG.error("copyFromLocalFile exception caught!:" , e);
  25. new RuntimeException(e);
  26. }finally{
  27. fs.close();
  28. }
  29. }catch (IOException e1) {
  30. LOG.error("copyFromLocal IOException objectstream. :" ,e1);
  31. new RuntimeException(e1);
  32. }
  33. }

添加一个文件到指定的目录下,java代码如下:

[java] view plain copy print?
  1. public void addFile(String source, String dest)  {
  2. // Conf object will readthe HDFS configuration parameters
  3. Configurationconf = new Configuration();
  4. FileSystemfs;
  5. try {
  6. fs= FileSystem.get(conf);
  7. // Get the filename out of thefile path
  8. Stringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());
  9. // Create the destination pathincluding the filename.
  10. if (dest.charAt(dest.length() - 1) != '/') {
  11. dest= dest + "/" + filename;
  12. }else {
  13. dest= dest + filename;
  14. }
  15. // Check if the file alreadyexists
  16. Pathpath = new Path(dest);
  17. if (fs.exists(path)) {
  18. LOG.error("File " + dest + " already exists");
  19. return;
  20. }
  21. // Create a new file and writedata to it.
  22. FSDataOutputStreamout = fs.create(path);
  23. InputStreamin = new BufferedInputStream(new FileInputStream(
  24. new File(source)));
  25. byte[] b = new byte[1024];
  26. int numBytes = 0;
  27. //In this way read and write datato destination file.
  28. while ((numBytes = in.read(b)) > 0) {
  29. out.write(b,0, numBytes);
  30. }
  31. in.close();
  32. out.close();
  33. fs.close();
  34. }catch (IOException e) {
  35. LOG.error("addFile Exception caught! :" , e);
  36. new RuntimeException(e);
  37. }
  38. }

3 修改

重新命名hdfs中的文件名称,java代码如下:

[java] view plain copy print?
  1. public void renameFile (String fromthis, String tothis){
  2. Configurationconf = new Configuration();
  3. FileSystemfs;
  4. try {
  5. fs= FileSystem.get(conf);
  6. PathfromPath = new Path(fromthis);
  7. PathtoPath = new Path(tothis);
  8. if (!(fs.exists(fromPath))) {
  9. LOG.info("No such destination " + fromPath);
  10. return;
  11. }
  12. if (fs.exists(toPath)) {
  13. LOG.info("Already exists! " + toPath);
  14. return;
  15. }
  16. try{
  17. boolean isRenamed = fs.rename(fromPath,toPath);     //renames file name indeed.
  18. if(isRenamed){
  19. LOG.info("Renamed from " + fromthis + " to " + tothis);
  20. }
  21. }catch(Exception e){
  22. LOG.error("renameFile Exception caught! :" , e);
  23. new RuntimeException(e);
  24. }finally{
  25. fs.close();
  26. }
  27. }catch (IOException e1) {
  28. LOG.error("fs Exception caught! :" , e1);
  29. new RuntimeException(e1);
  30. }
  31. }

 

4 删除

在hdfs上,删除指定的一个文件。Java代码:

[java] view plain copy print?
  1. public void deleteFile(String file)  {
  2. Configurationconf = new Configuration();
  3. FileSystemfs;
  4. try {
  5. fs= FileSystem.get(conf);
  6. Pathpath = new Path(file);
  7. if (!fs.exists(path)) {
  8. LOG.info("File " + file + " does not exists");
  9. return;
  10. }
  11. /*
  12. * recursively delete the file(s) if it is adirectory.
  13. * If you want to mark the path that will bedeleted as
  14. * a result of closing the FileSystem.
  15. *  deleteOnExit(Path f)
  16. */
  17. fs.delete(new Path(file), true);
  18. fs.close();
  19. }catch (IOException e) {
  20. LOG.error("deleteFile Exception caught! :" , e);
  21. new RuntimeException(e);
  22. }
  23. }

Appendix 完整代码

[java] view plain copy print?
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.apache.hadoop.conf.Configuration;
  12. import org.apache.hadoop.fs.BlockLocation;
  13. import org.apache.hadoop.fs.FSDataInputStream;
  14. import org.apache.hadoop.fs.FSDataOutputStream;
  15. import org.apache.hadoop.fs.FileStatus;
  16. import org.apache.hadoop.fs.FileSystem;
  17. import org.apache.hadoop.fs.LocatedFileStatus;
  18. import org.apache.hadoop.fs.Path;
  19. import org.apache.hadoop.fs.RemoteIterator;
  20. importorg.apache.hadoop.hdfs.DistributedFileSystem;
  21. import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
  22. public class SyncDFS {
  23. private static final Log LOG = LogFactory.getLog(SyncDFS.class);
  24. /**
  25. * Reads the directory name(s) and file name(s)from the specified parameter "srcPath"
  26. * @param srcPath
  27. */
  28. public void list(String srcPath) {
  29. Configuration conf = new Configuration();
  30. LOG.info("[Defaultfs] :" +conf.get("fs.default.name"));
  31. //                conf.set("hadoop.job.ugi","app,app");   //It is not necessary for the default user.
  32. FileSystem fs;
  33. try {
  34. fs= FileSystem.get(conf);
  35. RemoteIterator<LocatedFileStatus>rmIterator = fs.listLocatedStatus(new Path(srcPath));
  36. while (rmIterator.hasNext()) {
  37. Path path = rmIterator.next().getPath();
  38. if(fs.isDirectory(path)){
  39. LOG.info("-----------DirectoryName: "+path.getName());
  40. }
  41. else if(fs.isFile(path)){
  42. LOG.info("-----------FileName: "+path.getName());
  43. }
  44. }
  45. }catch (IOException e) {
  46. LOG.error("list fileSysetm object stream.:" , e);
  47. new RuntimeException(e);
  48. }
  49. }
  50. /**
  51. * Makes the specified directory if it doesn'texist.
  52. * @param dir
  53. */
  54. public void mkdir(String dir){
  55. Configurationconf = new Configuration();
  56. FileSystemfs = null;
  57. try {
  58. fs= FileSystem.get(conf);
  59. Pathpath = new Path(dir);
  60. if(!fs.exists(path)){
  61. fs.mkdirs(path);
  62. LOG.debug("create directory '"+dir+"' successfully!");
  63. }else{
  64. LOG.debug("directory '"+dir+"' exits!");
  65. }
  66. }catch (IOException e) {
  67. LOG.error("FileSystem get configuration with anerror");
  68. e.printStackTrace();
  69. }finally{
  70. if(fs!= null){
  71. try {
  72. fs.close();
  73. }catch (IOException e) {
  74. LOG.error("close fs object stream. :" , e);
  75. new RuntimeException(e);
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Reads the file content in console.
  82. * @param file
  83. */
  84. public void readFile(String file){
  85. Configurationconf = new Configuration();
  86. FileSystemfs;
  87. try {
  88. fs= FileSystem.get(conf);
  89. Pathpath = new Path(file);
  90. if(!fs.exists(path)){
  91. LOG.warn("file'"+ file+"' doesn't exist!");
  92. return ;
  93. }
  94. FSDataInputStreamin = fs.open(path);
  95. Stringfilename = file.substring(file.lastIndexOf('/') + 1, file.length());
  96. OutputStreamout = new BufferedOutputStream(new FileOutputStream(
  97. new File(filename)));
  98. byte[] b = new byte[1024];
  99. int numBytes = 0;
  100. while ((numBytes = in.read(b)) > 0) {
  101. out.write(b,0, numBytes);
  102. }
  103. in.close();
  104. out.close();
  105. fs.close();
  106. }catch (IOException e) {
  107. LOG.error("ifExists fs Exception caught! :" , e);
  108. new RuntimeException(e);
  109. }
  110. }
  111. public boolean ifExists(String source){
  112. if(source == null || source.length() ==0){
  113. return false;
  114. }
  115. Configurationconf = new Configuration();
  116. FileSystemfs = null;
  117. try {
  118. fs= FileSystem.get(conf);
  119. LOG.debug("judge file '"+source +  "'");
  120. return fs.exists(new Path(source));
  121. }catch (IOException e) {
  122. LOG.error("ifExists fs Exception caught! :" , e);
  123. new RuntimeException(e);
  124. return false;
  125. }finally{
  126. if(fs != null){
  127. try {
  128. fs.close();
  129. }catch (IOException e) {
  130. LOG.error("fs.close Exception caught! :" , e);
  131. new RuntimeException(e);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Recursively copies the source pathdirectories or files to the destination path of DFS.
  138. * It is the same functionality as thefollowing comand:
  139. *      hadoopfs -copyFromLocal <local fs><hadoop fs>
  140. * @param source
  141. * @param dest
  142. */
  143. public void copyFromLocal (String source, String dest) {
  144. Configurationconf = new Configuration();
  145. FileSystemfs;
  146. try {
  147. fs= FileSystem.get(conf);
  148. PathsrcPath = new Path(source);
  149. PathdstPath = new Path(dest);
  150. // Check if the file alreadyexists
  151. if (!(fs.exists(dstPath))) {
  152. LOG.warn("dstPathpath doesn't exist" );
  153. LOG.error("No such destination " + dstPath);
  154. return;
  155. }
  156. // Get the filename out of thefile path
  157. Stringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());
  158. try{
  159. //if the file exists in thedestination path, it will throw exception.
  160. //                                   fs.copyFromLocalFile(srcPath,dstPath);
  161. //remove and overwrite files withthe method
  162. //copyFromLocalFile(booleandelSrc, boolean overwrite, Path src, Path dst)
  163. fs.copyFromLocalFile(false, true, srcPath, dstPath);
  164. LOG.info("File " + filename + "copied to " + dest);
  165. }catch(Exception e){
  166. LOG.error("copyFromLocalFile exception caught!:" , e);
  167. new RuntimeException(e);
  168. }finally{
  169. fs.close();
  170. }
  171. }catch (IOException e1) {
  172. LOG.error("copyFromLocal IOException objectstream. :" ,e1);
  173. new RuntimeException(e1);
  174. }
  175. }
  176. public void renameFile (String fromthis, String tothis){
  177. Configurationconf = new Configuration();
  178. FileSystemfs;
  179. try {
  180. fs= FileSystem.get(conf);
  181. PathfromPath = new Path(fromthis);
  182. PathtoPath = new Path(tothis);
  183. if (!(fs.exists(fromPath))) {
  184. LOG.info("No such destination " + fromPath);
  185. return;
  186. }
  187. if (fs.exists(toPath)) {
  188. LOG.info("Already exists! " + toPath);
  189. return;
  190. }
  191. try{
  192. boolean isRenamed = fs.rename(fromPath,toPath);     //renames file name indeed.
  193. if(isRenamed){
  194. LOG.info("Renamed from " + fromthis + " to " + tothis);
  195. }
  196. }catch(Exception e){
  197. LOG.error("renameFile Exception caught! :" , e);
  198. new RuntimeException(e);
  199. }finally{
  200. fs.close();
  201. }
  202. }catch (IOException e1) {
  203. LOG.error("fs Exception caught! :" , e1);
  204. new RuntimeException(e1);
  205. }
  206. }
  207. /**
  208. * Uploads or adds a file to HDFS
  209. * @param source
  210. * @param dest
  211. */
  212. public void addFile(String source, String dest)  {
  213. // Conf object will readthe HDFS configuration parameters
  214. Configurationconf = new Configuration();
  215. FileSystemfs;
  216. try {
  217. fs= FileSystem.get(conf);
  218. // Get the filename out of thefile path
  219. Stringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());
  220. // Create the destination pathincluding the filename.
  221. if (dest.charAt(dest.length() - 1) != '/') {
  222. dest= dest + "/" + filename;
  223. }else {
  224. dest= dest + filename;
  225. }
  226. // Check if the file alreadyexists
  227. Pathpath = new Path(dest);
  228. if (fs.exists(path)) {
  229. LOG.error("File " + dest + " already exists");
  230. return;
  231. }
  232. // Create a new file and writedata to it.
  233. FSDataOutputStreamout = fs.create(path);
  234. InputStreamin = new BufferedInputStream(new FileInputStream(
  235. new File(source)));
  236. byte[] b = new byte[1024];
  237. int numBytes = 0;
  238. //In this way read and write datato destination file.
  239. while ((numBytes = in.read(b)) > 0) {
  240. out.write(b,0, numBytes);
  241. }
  242. in.close();
  243. out.close();
  244. fs.close();
  245. }catch (IOException e) {
  246. LOG.error("addFile Exception caught! :" , e);
  247. new RuntimeException(e);
  248. }
  249. }
  250. /**
  251. *Deletes the files if it is a directory.
  252. * @param file
  253. */
  254. public void deleteFile(String file)  {
  255. Configurationconf = new Configuration();
  256. FileSystemfs;
  257. try {
  258. fs= FileSystem.get(conf);
  259. Pathpath = new Path(file);
  260. if (!fs.exists(path)) {
  261. LOG.info("File " + file + " does not exists");
  262. return;
  263. }
  264. /*
  265. * recursively delete the file(s) if it is adirectory.
  266. * If you want to mark the path that will bedeleted as
  267. * a result of closing the FileSystem.
  268. *  deleteOnExit(Path f)
  269. */
  270. fs.delete(new Path(file), true);
  271. fs.close();
  272. }catch (IOException e) {
  273. LOG.error("deleteFile Exception caught! :" , e);
  274. new RuntimeException(e);
  275. }
  276. }
  277. /**
  278. * Gets the information about the file modifiedtime.
  279. * @param source
  280. * @throws IOException
  281. */
  282. public void getModificationTime(String source) throws IOException{
  283. Configurationconf = new Configuration();
  284. FileSystemfs = FileSystem.get(conf);
  285. PathsrcPath = new Path(source);
  286. // Check if the file alreadyexists
  287. if (!(fs.exists(srcPath))) {
  288. System.out.println("No such destination " + srcPath);
  289. return;
  290. }
  291. // Get the filename out of thefile path
  292. Stringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());
  293. FileStatusfileStatus = fs.getFileStatus(srcPath);
  294. long modificationTime =fileStatus.getModificationTime();
  295. LOG.info("modified datetime: " + System.out.format("File %s; Modification time : %0.2f%n",filename,modificationTime));
  296. }
  297. /**
  298. * Gets the file block location info
  299. * @param source
  300. * @throws IOException
  301. */
  302. public void getBlockLocations(String source) throws IOException{
  303. Configurationconf = new Configuration();
  304. FileSystemfs = FileSystem.get(conf);
  305. PathsrcPath = new Path(source);
  306. // Check if the file alreadyexists
  307. if (!(ifExists(source))) {
  308. System.out.println("No such destination " + srcPath);
  309. return;
  310. }
  311. // Get the filename out of thefile path
  312. Stringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());
  313. FileStatusfileStatus = fs.getFileStatus(srcPath);
  314. BlockLocation[]blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
  315. int blkCount = blkLocations.length;
  316. System.out.println("File :" + filename + "stored at:");
  317. for (int i=0; i < blkCount; i++) {
  318. String[]hosts = blkLocations[i].getHosts();
  319. LOG.info("host ip:" +System.out.format("Host %d: %s %n", i, hosts));
  320. }
  321. }
  322. public void getHostnames () throws IOException{
  323. Configurationconfig = new Configuration();
  324. FileSystemfs = FileSystem.get(config);
  325. DistributedFileSystemhdfs = (DistributedFileSystem) fs;
  326. DatanodeInfo[]dataNodeStats = hdfs.getDataNodeStats();
  327. String[]names = new String[dataNodeStats.length];
  328. for (int i = 0; i < dataNodeStats.length; i++) {
  329. names[i]= dataNodeStats[i].getHostName();
  330. LOG.info("datenode hostname:"+(dataNodeStats[i].getHostName()));
  331. }
  332. }
  333. /**
  334. * @param args
  335. */
  336. public static void main(String[] args) {
  337. SyncDFSdfs = new SyncDFS();
  338. dfs.list("/user/app");
  339. dfs.mkdir("/user/app");
  340. //                dfs.readFile("/user/app/README.txt");
  341. LOG.info("--------------" +
  342. dfs.ifExists("/user/warehouse/hbase.db/u_data/u.data")); //false
  343. LOG.info("--------------" + dfs.ifExists("/user/app/README.txt")); //true
  344. //copied the local file(s) to thedfs.
  345. //                dfs.copyFromLocal("/opt/test","/user/app");
  346. //delete the file(s) from the dfs
  347. //                dfs.deleteFile("/user/app/test");
  348. //rename diretory in dfs
  349. //                dfs.renameFile("/user/app/test","/user/app/log");
  350. //rename file in dfs
  351. //                dfs.renameFile("/user/app/log/derby.log","/user/app/log/derby_info.log");
  352. }
  353. }

java实现对HDFS增删改查(CRUD)等操作相关推荐

  1. 实现对HDFS增删改查CRUD等操作

    1 查找 列出某个目录下的文件名称,hdfs命令如下所示: hdfs dfs –ls/usr/app java代码片段: public void list(String srcPath) {Confi ...

  2. 实现对mysql增删改查_Java语言实现对MySql数据库中数据的增删改查操作的代码

    简单说操作的步骤: 1.连接数据库 2.将SQL语句发送到数据库 3.执行SQL语句 这里举个例子: 在一个数据库中有个students表,表中有学号(Id),姓名(Name),性别(Sex),地址( ...

  3. JAVA+SWING超市销售管理系统开发(JAVA实训作业增删改查)

    一个非常简单的超市销售系统,只涉及增删改查. 1. 登录 两种方式登录系统,这里主要分享管理员登录 最简单的方法,指定账号密码 JButton button_login = new JButton(& ...

  4. Android前端通过Http协议与J2EE后端数据交互。工具eclipse、MySQL、Tomcat。通过JoSn获取数据。Android端实现对MySQL增删改查功能。

    Android:目录 HttpThread.java package com.example.saads; import java.io.BufferedReader; import java.io. ...

  5. php xml 增删改查,PHP实现对xml进行简单的增删改查(CRUD)操作示例

    本文实例讲述了PHP实现对xml进行简单的增删改查(CRUD)操作.分享给大家供大家参考,具体如下: 假如有下面xml文件: 55.8 56 40 339 如何使用php对它进行CRUD?其实像这种简 ...

  6. 一步步实现:JPA的基本增删改查CRUD(jpa基于hibernate)

    1.创建一个JPA工程 首先,创建一个JPA工程(若不知道JPA创建或出现at least one user library must be selected等错误,请参考http://blog.cs ...

  7. JDBC:JAVA连接Mysql实现增删改查

    总有特别赶的时候,小高叫你如何速通JAVA连接数据库Mysql实现增删改查.CV战士 一.前置准备工作 1. 安装IDEA,配置JAVA环境 2. 安装Mysql,配置Mysql环境变量 3. 安装S ...

  8. MyBatisPlus03_MyBatisPlus的增删改查CRUD

    MyBatisPlus03_MyBatisPlus的增删改查CRUD 查询 无条件查询 List<User> list = mapper.selectList(null); eq查询 Qu ...

  9. Java连接Mysql数据库增删改查实现

    Java连接Mysql数据库增删改查实现 时间比较赶,我这里只实现查询,有时间再添加另外两个 难度 : ⭐⭐⭐(全星5颗星的情况下) 新建一个动态的网站工程, 把jar包全部复制进去,主要要那个mys ...

最新文章

  1. Cascade RPN,结构的艺术带来极致提升 | NeurIPS 2019
  2. Jquery基本知识点的总结
  3. LTE voice centric和data centric
  4. tensorrt动态输入分辨率尺寸
  5. 非常值得收藏的 IBM SPSS Modeler 算法简介
  6. 软件工程--需求分析
  7. RSA加密的填充模式
  8. SQLi-LABS(11~20关详解)
  9. php import用法,JavaScript中import怎么使用?
  10. 分布式系统CAP定理
  11. codeforces C. Diverse Permutation(构造)
  12. v210 启动脚本分析
  13. java map集合排序的_Java对Map集合进行排序
  14. Silverlight实用窍门系列:28.Silverlight制作随机分布雷达扫描点,模拟雷达扫描图之被扫描设备【附带源码实例】...
  15. Python面试笔记二
  16. kmeans算法实例及python代码-Python实现k-means算法
  17. python对数组分类_有效地从字典Python中对数组进行分类
  18. HDU 4511 小明系列故事——女友的考验 (AC自动机 + DP)题解
  19. 经典的观点挖掘算法(文本挖掘系列)
  20. android 360游戏sdk,360dev 单机游戏接入SDK

热门文章

  1. 【错误记录】Ubuntu 中 ROOT 用户无法启动 Visual Studio Code 开发环境 ( 推荐在普通用户下使用 VSCode 开发环境 )
  2. 【EventBus】发布-订阅模式 ( Android 中使用 发布-订阅模式 进行通信 )
  3. 【MATLAB】三维图形绘制 ( 绘制网格 + 等高线 | meshc 函数 | 绘制平面 + 等高线 | surfc 函数 )
  4. 【Netty】NIO 通道 ( Channel ) 组件
  5. 【Android 高性能音频】AAudio 音频流 数据回调细节 ( 数据回调函数优先级 | 数据回调函数 | 采样率 | 采样数 | 缓冲区调整 | 线程不安全 )
  6. NHibernate 状态的概念 以及 小测试
  7. 实践作业3 (2017-12-4)
  8. win7 无法复制粘贴
  9. Apache地址重写的几个问题
  10. 一些有关计组实验中Quartus中的名词或术语的解释