TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Hadoop是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上+ J9 [6 T; I( I
上传:+ m5 ~: Z6 L) U& }( y
- /**
4 X o$ K4 V( f" q - *
3 P2 V3 P+ ^% t z; B8 } - * 上传控制类,上传页面跳转到这里 h& t8 B6 {# H! R$ q4 n
- * O! G( f2 s. i6 S4 Y+ b6 \
- * @author 科帮网
2 g, L* X6 b+ U" K; s$ O& H, F0 D - * @version [版本号, 2012-08-02]
/ V: p0 w( O, S7 c8 o! S: B - * @see [相关类/方法] : E I# C- d |- A
- * @since version 1.0. E) r+ r4 B* u `* w- C
- */0 M$ e- X0 q' u+ G4 w0 B' A
- public class UploadHDFSServlet extends HttpServlet {
7 e' U4 N8 A: V+ m8 w - 8 x$ j4 r: P& P
- /** *
: j8 }7 E, C# W/ ` - * * 该方法负责上传控制处理页面 *
2 @, r& K* L- V+ z8 w+ Q( g) a5 o - * @param request 客户端到服务器的请求消息 m2 l# l$ W5 C5 y' B
- * @param response 服务器返回给页面的消息
$ U( v* _* A; I4 q5 U. \ - * @throws ServletException
' V) {% Y: Q2 Y' W - * @throws IOException ' C" D6 J1 b k: Z) V
- */
! }1 K, Q: S( n5 w0 N9 E - public void doPost(HttpServletRequest request, HttpServletResponse response), ^4 U/ ^7 [, L- O; O9 s
- throws ServletException, IOException {
8 ]3 ^5 X& O$ M! Q - boolean isMultipart = ServletFileUpload.isMultipartContent(request);; j1 J9 K" o- ?
- if(!isMultipart){* h6 f- ^) S* X0 O( S. X
- }else{
( E( I$ y" N3 D! w: Q+ `9 k7 R - FileItemFactory factory = new DiskFileItemFactory();( V) w& I. q/ U- g5 Q5 p: x
- ServletFileUpload upload = new ServletFileUpload(factory);
0 I' D/ C2 S# p: J4 Y) ^ - try {
( Y }4 y3 e6 U - List items = upload.parseRequest(request);
/ y0 |% B/ l0 p1 t, F4 s - Iterator iter = items.iterator();: ^. p* x! f- L+ X" D# ?. O2 W+ ]
- while(iter.hasNext()){9 e9 i/ R' s3 B6 O
- FileItem item = (FileItem)iter.next();! C2 }5 N; w8 R, ~9 z% N# f
- if(item.isFormField()){& m" P( o/ l7 J: U, i$ r! [
- String name = item.getFieldName();
% g, P& R& ~! G& M% ~5 d! v* b) f - System.out.println(name);
S$ x: T7 }" O: h4 l- O$ R- b) g - }else{5 C- y' b& R! ?; T6 h
- //表单名字
( ^% R8 ^' D) q& m3 l1 f - String fileName = item.getName();& v/ r- V, f i0 b* e# t4 h# w
- InputStream uploadedStream = item.getInputStream(); ] Z- ` [7 v
- HDFSOperation hdfsOperation = new HDFSOperation();3 q/ \, ~* b5 B# a5 h# n5 K
-
3 u. A' h! A! y- y: C' t' ~ - String hdfsPath = dfs.HDFSConfig.getHDFSPath() + fileName;
% S# M; @2 a" ]6 O - boolean flag = hdfsOperation.upLoad(uploadedStream, hdfsPath); 6 Q, G. e8 p- Y' P) h6 r$ @
- 8 `- m0 k2 ^# j5 [# A
- if(flag){
4 j/ `* |' E/ G2 J- g& O3 g5 |4 K2 b - response.sendRedirect("success.jsp");' E. S% k6 }# n$ t' o: [
- }else{: j* |1 ^* U# `2 O$ x1 X( Y& U: _
- response.sendRedirect("failure.jsp");: a _4 N6 x$ L. w) L
- }
7 g, l8 N* W4 i3 n; E2 \9 } - }
" C( Z5 E! b9 ^ - }
L3 F5 y! n' U7 C0 u0 ? - 2 k" P( q4 x% V I
- } catch (Exception e) {
2 B; G. p: u8 `7 X7 Q - e.printStackTrace();
' E/ R) Q6 m. F) ` - }" |( o0 A! S N* e |4 u* w
- } ) s+ g( M- l3 ~ q5 Y
- 0 f8 ]& F; j- a6 c% @" ^7 y
- }4 p( o: o, V5 g. c; G: I
" V& E" f: K ]- }
复制代码 配置:
2 ~: t( G- J6 |% R5 M. a- <configuration>3 s, l; w+ `* t8 {- G( H2 B
- <property>
6 B" }# k! D0 p% Z _/ s# L/ | - <name>fs.default.name </name>/ ]; Q" e/ J) t) I
- <value>hdfs://10.6.12.101:8888</value>
- P9 L P" S3 ] - </property>
5 V$ b" T) c+ D j - <property>
( j; x# g2 _( v0 Z/ [! I$ q3 S - <name>hadoop.tmp.dir</name>
4 @5 F7 ]% j8 C- f# P3 f - <value>/home/hadoopuser/tmp</value>8 f- i3 \* d0 {2 i* M2 T: m! N, _5 F
- </property>+ R# h- M* k& G/ t( Z, R; N4 \& H
- </configuration>
复制代码 源码下载地址:点击下载6 t& z7 [7 L) {- N
& w3 \' T" \2 I" |1 f/ @' \" p
: j: S7 ` W0 g. ]6 Y" V f% w
|
|