Hbase开发提高

首先需要安装配置HDFS和eclipse,此步骤可通过前面的博客查阅。运行此过程时可以考虑在eclipse中加入Hadoop插件对HDFS文件进行管理,此过程暂不做描述,可自行查找相关教程文档。

 

1. 通过终端开启Hadoop

$ start-all.sh

$jps

 

2. 打开eclipse,创建project,创建包和class

 

3. 导入jar包,Build Path → Configure Build Path... ,导入Hadoop下和文件lib下的所有jar包

 

4. 编写代码实现相应功能


  
  
  1. package com.jdk.hdfs;
  2.  
  3. import java.io.IOException;
  4. import java.net.URI;
  5.  
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.FileStatus;
  8. import org.apache.hadoop.fs.FileSystem;
  9. import org.apache.hadoop.fs.FileUtil;
  10. import org.apache.hadoop.fs.Path;
  11.  
  12. public class ls {
  13.  
  14. /**
  15.  * @param args
  16.  * @throws IOException 
  17.  */
  18. public static void main(String[] args) throws IOException {
  19. // 查看文件夹
  20. // String dir = "/";
  21. // Configuration conf = new Configuration(); 
  22. conf.set("fs.defaultfs", "hdfs://196.168.152.141:9000/");  //如果是在本地运行,则可以直接使用conf,不需要加URI链接,如果运行的是集群,则需要添加URI链接,下同
  23. // FileSystem fs = FileSystem.get(conf);
  24. // FileStatus[] filestatus = fs.listStatus(new Path(dir));
  25. // Path[] list = FileUtil.stat2Paths(filestatus);
  26. // for (Path path : list){
  27. // System.out.println(path.toString());
  28. // }
  29. // fs.close();
  30. // 新建文件夹
  31. // Configuration conf = new Configuration(); 
  32. // FileSystem fs = FileSystem.get(URI.create("hdfs://196.168.152.141:9000/"),conf);
  33. // fs.mkdirs(new Path("/hi"));    // 文件“hi”即为创建的文件夹
  34. // fs.close();
  35. // 删除文件夹
  36. // Configuration conf = new Configuration(); 
  37. // FileSystem fs = FileSystem.get(URI.create("hdfs://196.168.152.141:9000/"),conf);
  38. // fs.delete(new Path("/hi"),true);    // 文件“hi”即为删除的文件夹
  39. // fs.close();
  40. // 导入文件
  41. // Configuration conf = new Configuration(); 
  42. // FileSystem fs = FileSystem.get(URI.create("hdfs://196.168.152.141:9000/"),conf);
  43. // Path src = new Path("需要导入的文件的路径");
  44. // Path dst = new Path("导入到HDFS的目标文件夹,如刚才创建的“/hi”文件");
  45. // fs.close();
  46. }
  47. }

 

4. 新建class,编写代码


  
  
  1. package com.jdk.hdfs;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7.  
  8. import org.apache.hadoop.conf.Configuration;
  9. import org.apache.hadoop.fs.FSDataInputStream;
  10. import org.apache.hadoop.fs.FileSystem;
  11. import org.apache.hadoop.fs.Path;
  12.  
  13. public class ReadHDFS {
  14.  
  15. /**
  16.  * @param args
  17.  * @throws IOException 
  18.  */
  19. public static void main(String[] args) throws IOException {
  20. // 读取HDFS中的文件
  21. String dir = "HDFS中需要读取的目标文件及其目录";
  22. Configuration conf = new Configuration(); 
  23. FileSystem fs = FileSystem.get(conf);
  24. FSDataInputStream file = fs.open(new Path(dir));
  25. BufferedReader in = null;
  26. String line;
  27. in = new BufferedReader(new InputStreamReader(file,"UTF-8"));
  28. while((line = in.readLine()) != null){
  29. System.out.print(line);
  30. }
  31. if(in != null){
  32. in.close();
  33. }
  34. fs.close();
  35.  
  36. }
  37.  
  38. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值