一脸懵逼学习hadoop之HDFS的java客户端编写

1:eclipse创建一个项目,然后导入对应的jar包:

鼠标右击项目,点击properties或者alt+enter快捷键--->java build path--->libraries--->add library--->user library--->next--->user libraries--->new--->hdfsLib(根据自己的需要填写)---》add external jars(添加自己的需求包):

2:开始添加自己的需求包,路径如

  hadoop-2.4.1\share\hadoop\hdfs的hadoop-hdfs-2.4.1.jar和hadoop-2.4.1\share\hadoop\hdfs\lib下面的全部包;

  hadoop-2.4.1\share\hadoop\common的hadoop-common-2.4.1.jar和hadoop-2.4.1\share\hadoop\common\lib下面的全部包;

  1 package com.master01;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.IOException;
  5 import java.net.URISyntaxException;
  6 
  7 import org.apache.commons.io.IOUtils;
  8 import org.apache.hadoop.conf.Configuration;
  9 import org.apache.hadoop.fs.FSDataOutputStream;
 10 import org.apache.hadoop.fs.FileStatus;
 11 import org.apache.hadoop.fs.FileSystem;
 12 import org.apache.hadoop.fs.LocatedFileStatus;
 13 import org.apache.hadoop.fs.Path;
 14 import org.apache.hadoop.fs.RemoteIterator;
 15 
 16 public class HdfsTest {
 17 
 18     
 19     //public FileSystem fs = null;
 20     /*
 21     @Before
 22     public void init() throws IOException, InterruptedException, URISyntaxException{
 23         //读配置文件
 24         Configuration conf = new Configuration();
 25         //这里直接拷贝配置或者直接设置值
 26         conf.set("fs.defaultFS", "hdfs://master:9000/");
 27         
 28         //获取配置文件里面的内容
 29         fs = FileSystem.get(conf);
 30         //fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
 31     }
 32     */
 33     
 34     
 35     /**
 36      * 上传文件
 37      * @throws IOException 
 38      */
 39     public static void upload() throws IOException{
 40         //读配置文件
 41         //读取classpath下的core-site.xml配置文件,并且解析其的内容,封装到conf的对象中;
 42         Configuration conf = new Configuration();
 43         //这里直接拷贝配置或者直接设置值
 44         //也可以在代码中对conf的配置信息进行手动设置,会覆盖配置文件中的配置信息
 45         conf.set("fs.defaultFS", "hdfs://master:9000");
 46         
 47         //获取配置文件里面的内容
 48         //根据配置信息,去获取一个具体文件系统的客户端操作实例对象
 49         FileSystem fs = FileSystem.get(conf);
 50         //本地文件是输入流,hdfs是输出流
 51         
 52         //先搞出路径
 53         Path src = new Path("hdfs://master:9000/aa/test.txt");
 54         //搞出输出流,即向hdfs上面写内容
 55         FSDataOutputStream create = fs.create(src);
 56         
 57         //输入流就是读,本地文件,输入流
 58         FileInputStream fileInputStream = new FileInputStream("d:/test.txt");
 59         
 60         //将文件fileInputStream到create即完成上传到hdfs
 61         IOUtils.copy(fileInputStream, create);
 62     }
 63     
 64     
 65     //最快的上传文件的方法
 66     public void upload02() throws IllegalArgumentException, IOException, InterruptedException, URISyntaxException{
 67         //读配置文件
 68         Configuration conf = new Configuration();
 69         //这里直接拷贝配置或者直接设置值
 70         conf.set("fs.defaultFS", "hdfs://master:9000");
 71         
 72         //获取配置文件里面的内容
 73         FileSystem fs = FileSystem.get(conf);
 74         //FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
 75         fs.copyFromLocalFile(new Path("d:/test.txt"), new Path("hdfs://master:9000/aa/test.txt"));
 76     }
 77     
 78     
 79     /**
 80      * 下载文件
 81      * @throws IOException 
 82      * @throws IllegalArgumentException 
 83      */
 84     public void download02() throws IllegalArgumentException, IOException{
 85         //去配置文件
 86         Configuration conf = new Configuration();
 87         conf.set("fs.defaultFS", "hdfs://master:9000");
 88         
 89         //获取配置文件里面的内容
 90         FileSystem fs = FileSystem.get(conf);
 91         fs.copyToLocalFile(new Path("hdfs://master:9000/aa/test.txt"), new Path("d:/test2.txt"));
 92         
 93     }
 94     
 95     /***
 96      * 创建文件夹的方法
 97      * @throws IOException 
 98      */
 99     public void mkdir02() throws IOException{
100         //主配置文件
101         Configuration conf = new Configuration();
102         //设置配置文件的值
103         conf.set("fs.defaultFS", "hdfs://master:9000");
104         //获取配置文件里面的内容
105         FileSystem fs = FileSystem.get(conf);
106         
107         //文件夹的创建
108         fs.mkdirs(new Path("hdfs://master:9000/aaa/bbb/ccc"));
109     }
110     
111     
112     /**
113      * 删除文件
114      * @throws IOException 
115      */
116     public void remove02() throws IOException{
117         //主配置文件
118         Configuration conf = new Configuration();
119         //设置值
120         conf.set("fs.defaultFS", "hdfs://master:9000");
121         //获取配置文件里面的内容
122         FileSystem fs = FileSystem.get(conf);
123         
124         //执行删除操作
125         fs.delete(new Path("hdfs://master:9000/aaa/bbb/ccc"), true);
126     }
127     
128     /**
129      * 文件的移动
130      * @throws IOException 
131      */
132     public void move() throws IOException{
133         //主配置文件
134         Configuration conf = new Configuration();
135         //设置值
136         conf.set("fs.defaultFS", "hdfs://master:9000");
137         //获取配置文件里面的内容
138         FileSystem fs = FileSystem.get(conf);
139         
140         //移动操作
141         fs.rename(new Path("hdfs://master:9000/aa/test.txt"), new Path("hdfs://master:9000/aaa/bbb"));
142     }
143     
144     /***
145      * 查看文件的信息
146      * @throws IOException 
147      */
148     public void listFiles() throws IOException{
149         //主配置文件
150         Configuration conf = new Configuration();
151         //设置值
152         conf.set("fs.defaultFS", "hdfs://master:9000");
153         //获取配置文件里面的内容
154         FileSystem fs = FileSystem.get(conf);
155                 
156         //查看的是文件,不是文件夹
157         //listFiles列出的是文件信息,而且提供递归遍历
158         RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("hdfs://master:9000/"), true);
159         //迭代输出信息
160         while(listFiles.hasNext()){
161             LocatedFileStatus file = listFiles.next();
162             //文件路径
163             Path path = file.getPath();
164             System.out.println(path.getName());
165         }
166         
167         System.out.println("=============================================");
168         //listStatus列出文件和文件夹的信息,但是不提供自带的递归遍历
169         FileStatus[] listStatus = fs.listStatus(new Path("hdfs://master:9000/"));
170         /*for(int i = 0 ; i<listStatus.length; i++){
171             System.out.println(listStatus[i]);
172         }*/
173         for(FileStatus fileStatus : listStatus){
174             //根据获取的路径获取文件夹的名称
175             Path path = fileStatus.getPath();
176             System.out.println(path.getName());
177         }
178         
179     }
180     
181     public static void main(String[] args) {
182         HdfsTest hdfsTest = new HdfsTest();
183         try {
184             //上传文件的调用
185             //hdfsTest.upload02();
186             
187             //下载文件的调用
188             //hdfsTest.download02();
189             
190             //文件夹的创建
191             //hdfsTest.mkdir02();
192             
193             //删除操作
194             //hdfsTest.remove02();
195             
196             //移动文件的操作
197             //hdfsTest.move();
198             
199             //查看文件信息
200             hdfsTest.listFiles();
201         } catch (Exception e) {
202             e.printStackTrace();
203         }
204     }
205 
206 }

3:NameNode的职责

(1):维护元数据的信息;

(2):维护hdfs的目录树;

(3):响应客户端的请求;

 

展开阅读全文

JAVA调用支付宝接口一脸懵逼!

07-01

本人刚学java4个月的萌新求大佬指教!rn简单的实验通过test.html中的按钮跳转支付宝支付页面test.html代码如下rn```rnrnrnrn rn 测试按钮rn rn rn rn rnrnrn rn 支付宝测试按钮rn rnrnrnrn```rn通过异步点击安酒后跳转支付宝接口controller部分代码如下rn```rn @RequestMapping("/test")rn public String test()rn return "test";rn rnrnrn @RequestMapping(value = "/paytest",method = RequestMethod.GET)rn public void testPay(HttpServletResponse response,String value)rn System.out.println(value);rn if (value!=null) rn try rn response.setContentType("text/html; charset=utf-8");rnrn response.getWriter().write(alipayService.getwayPay(value));rn response.getWriter().close();rn catch (IOException e) rn e.printStackTrace();rn rn rn rn```rn然后通过实现类实现数据的插入代码如下:rnrn```rn@Servicernpublic class AlipayServiceImpl implements AlipayService rn @Resourcern private AlipayClient alipayClient;rnrn @Resourcern private AlipayProperties aliPayProperties;rnrn @Resourcern private OrderDao orderDao;rnrn @Overridern public String getwayPay( String id) rn Order order=new Order();rn Long id1=Long.parseLong(id);rn order=orderDao.findById(id1).get();rn AlipayTradePagePayModel model=new AlipayTradePagePayModel();rn model.setOutTradeNo(order.getOutTradeNo());rn model.setSubject(order.getOrderType());rn model.setTotalAmount(order.getTotalAmount());rn model.setBody(order.getMiaoshu());rn model.setProductCode(order.getProductCode());rnrn AlipayTradePagePayRequest pagePayRequest = new AlipayTradePagePayRequest();rn pagePayRequest.setReturnUrl(aliPayProperties.getReturnUrl());rn pagePayRequest.setNotifyUrl(aliPayProperties.getNotifyUrl());rn pagePayRequest.setBizModel(model);rnrn // 调用SDK生成表单, 并直接将完整的表单html输出到页面rn String retStr = null;rn try rn retStr = alipayClient.pageExecute(pagePayRequest).getBody();rn catch (AlipayApiException e) rn e.printStackTrace();rn rn return retStr;rn rnrn```rn那么问题来了Demo用的是阿里给的 我这边只是插入数据 为什么跳转不了到阿里支付页面呢求大神指教下面贴两张控制器图rnrnrnrn![登录画面](https://img-ask.csdn.net/upload/201907/01/1561961475_262259.png)rnrnrn![点击后画面](https://img-ask.csdn.net/upload/201907/01/1561961494_836262.png)rnrnrnrn控制器代码:rn```rnHibernate: rn selectrn order0_.id as id1_5_0_,rn order0_.out_trade_no as out_trad2_5_0_,rn order0_.total_amount as total_am3_5_0_,rn order0_.miaoshu as miaoshu4_5_0_,rn order0_.order_type as order_ty5_5_0_,rn order0_.product_code as product_6_5_0_,rn order0_.state_pay as state_pa7_5_0_,rn order0_.student_id as student_8_5_0_,rn student1_.sid as sid1_8_1_,rn student1_.address as address2_8_1_,rn student1_.age as age3_8_1_,rn student1_.gender as gender4_8_1_,rn student1_.head as head5_8_1_,rn student1_.id_card as id_card6_8_1_,rn student1_.password as password7_8_1_,rn student1_.sname as sname8_8_1_,rn student1_.tel as tel9_8_1_ rn fromrn wu_order order0_ rn left outer joinrn wu_student student1_ rn on order0_.student_id=student1_.sid rn wherern order0_.id=?rnnullrn```rn说实话最后一个null没看懂 求大神指教! 问答

没有更多推荐了,返回首页