TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Hadoop是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上6 W3 ?% g5 S& G9 W: P
上传:# w9 x2 `( ~# |2 ?# M( s
- /**
. L: d0 c: P+ H' Y0 q: U2 Y - *
- f2 {; T$ c: t! O - * 上传控制类,上传页面跳转到这里
+ b( c! t& G5 Y$ h - * 5 b6 s {; j# V- L
- * @author 科帮网% I! c$ v) v8 E$ \8 }) P' p& |! K
- * @version [版本号, 2012-08-02] ( c, U9 q4 j* J- l
- * @see [相关类/方法] , v; w& a# ^6 F8 G% G2 s
- * @since version 1.07 S; V/ I+ P, G* A0 X6 ~! ]/ ]4 E# P. w
- */% O" U+ H2 D* i8 n# \/ ~4 R9 C
- public class UploadHDFSServlet extends HttpServlet {
% u% z W% e* S4 E - 7 K$ W- b! ^; Y2 _* _( f
- /** *) p, p! p4 V0 g( v$ W3 T f
- * * 该方法负责上传控制处理页面 *
4 W% F7 |+ ~4 ]; a) m' ? - * @param request 客户端到服务器的请求消息- B% g' G. t. G; ^. S1 X4 ]! S
- * @param response 服务器返回给页面的消息; ^0 c5 H4 U& N0 t' G
- * @throws ServletException
}6 D) q9 I- `7 H - * @throws IOException ( e, h% V1 R0 Y; ~
- */+ }6 H! m, L0 w* H5 o
- public void doPost(HttpServletRequest request, HttpServletResponse response)! H0 v" k8 p. C3 X' ?3 ?. u
- throws ServletException, IOException {" q) E/ D+ x% c
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);& o& x, Q, t7 J. F3 g# o
- if(!isMultipart){% `/ {7 ~! Q* e, x
- }else{
. R. V8 y+ G- T% Q1 C - FileItemFactory factory = new DiskFileItemFactory();1 H! E2 G7 Z+ l. i
- ServletFileUpload upload = new ServletFileUpload(factory);3 z9 n' W% U) r2 C# C( M
- try {
" i' z+ W9 D! B. N& n - List items = upload.parseRequest(request);
# q; ^' b- s9 d8 b) ` - Iterator iter = items.iterator();* Z8 g! |: [5 T5 K1 \6 ~8 [
- while(iter.hasNext()){
4 ~: n# l# Q: D$ C - FileItem item = (FileItem)iter.next();
% S+ B# f) u" ?4 b4 z# o) N: G - if(item.isFormField()){
3 R! u4 @( d& V4 m* p4 q# v+ Q - String name = item.getFieldName();
( {: t( y; K ^ - System.out.println(name);+ t- l) U n5 U% I# |
- }else{+ h0 S4 T* B9 t3 l/ } u$ K" P1 q
- //表单名字4 `* |% a3 n3 s
- String fileName = item.getName();8 ]; N3 p) \% k% l! W$ C
- InputStream uploadedStream = item.getInputStream();
/ ~; p% `2 O3 |& ~' ], w - HDFSOperation hdfsOperation = new HDFSOperation();
5 q% `* x/ S O2 r# ] z9 @ -
X0 v+ X w& k3 Y7 P/ d" F - String hdfsPath = dfs.HDFSConfig.getHDFSPath() + fileName;; m( M4 ]( d h7 {: X
- boolean flag = hdfsOperation.upLoad(uploadedStream, hdfsPath);
8 |% X4 _$ K/ r# T - , l) E( e9 R- j- F3 _/ R( t
- if(flag){
4 l P) B' y Y7 V - response.sendRedirect("success.jsp");) r( x! A w2 P$ [9 Y6 w6 G( g
- }else{
2 u9 A' o+ b6 e" P+ ^ - response.sendRedirect("failure.jsp");! r# \* W2 `* R
- }
, {$ J8 s& [4 t% s5 w. E5 _# q - }& f7 H: @ V& x5 q" @
- }* T, k9 v L w0 c) @2 l R
-
% P6 t: x& _7 X/ c' v8 ? - } catch (Exception e) {
1 ^/ N8 h. p" n - e.printStackTrace();: J/ w$ I% D o) L: y* J
- }
; E, B4 f+ {1 ]" Q. ]* h. \ - }
& \* S/ ^: F1 f& z9 u - ! W _9 H$ h% \; i1 P5 z% R
- }
1 h$ C/ B7 u& @/ p1 j& J5 e L - / w: S+ I. a: N! y w& A
- }
复制代码 配置:) S- n& K ?3 k% j W
- <configuration>, q: o) U1 O" y9 H
- <property>
7 [" Y; f# T: M& B, H1 R - <name>fs.default.name </name>- l5 ^ Q- B7 v/ F' K, x6 G
- <value>hdfs://10.6.12.101:8888</value>3 ~3 `9 D( V+ F3 a
- </property>
* }% U! A, L. Q( n3 ^( N1 M$ L - <property>
1 o/ Y: ]" A4 m6 H1 I4 |* R - <name>hadoop.tmp.dir</name>& B+ \0 Q2 U, g/ A* k/ @; m
- <value>/home/hadoopuser/tmp</value>. d# F1 B8 ]/ e
- </property>6 ]: ~) _' Z4 N, @% S, n% f: E
- </configuration>
复制代码 源码下载地址:点击下载
2 Z3 g; f: ]1 r% V! H
* n4 [3 P, p. i8 }) g4 f5 ^/ z N6 P; {! x4 Z
2 X. C( t( t. ^+ h$ q: H5 V
|
|