TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Hadoop是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上4 w9 }! V8 z9 n; E4 B4 ]. N
上传:
' n! G# ?+ B; [/ z( E* u- /**
5 d& Q7 V" q# M4 | - *
' ]$ u, c( B# O% d+ g - * 上传控制类,上传页面跳转到这里
0 i8 W, ? o, ^9 d8 C9 ]) Z6 G - * / e* R+ _9 z2 U, \- h
- * @author 科帮网# z) D. L5 t8 h" d
- * @version [版本号, 2012-08-02] ! k4 D0 k: J* V: g: J
- * @see [相关类/方法] / s- b. h% |* D1 u4 J" A
- * @since version 1.04 d9 i# j* m& M, ~# O' t' o
- */6 Z- Y* z/ {7 q0 l- g- S9 ~4 U
- public class UploadHDFSServlet extends HttpServlet {
" d8 i% `* W: G% Q
9 |, J% k- s) o6 |- /** */ d0 h8 @6 ~$ N# ]; v
- * * 该方法负责上传控制处理页面 * 9 ]7 k0 ^6 x. s) X( \9 l1 Q5 ~1 n4 a
- * @param request 客户端到服务器的请求消息- `- q- X; w% P# z7 s% E: \ M
- * @param response 服务器返回给页面的消息
! O! ~0 G% g1 {4 O% [ - * @throws ServletException
I* j% Q+ Z8 A# X7 l1 i1 G7 p - * @throws IOException 4 B6 h, C5 u( h( ^/ O5 M+ K
- */" Y+ F8 H; H+ \2 I
- public void doPost(HttpServletRequest request, HttpServletResponse response)
% _6 G. p/ F" U - throws ServletException, IOException {6 J" r: r2 A! N5 Y
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);+ {8 W/ h4 s# y8 H/ \, l
- if(!isMultipart){( m" |: {9 x; a! W
- }else{
8 S3 ?) a$ e" _, {# Y: H* e, A - FileItemFactory factory = new DiskFileItemFactory();
7 N: ^7 [, `; p2 w6 f& X- o - ServletFileUpload upload = new ServletFileUpload(factory);% x* Z* q9 t3 G4 T2 @
- try {/ Q& r) I& X" k& I% E
- List items = upload.parseRequest(request); 3 ~: c8 ^# ~$ t. @1 m" z
- Iterator iter = items.iterator();
# J( p {; D( _# P* W# [ - while(iter.hasNext()){3 D7 f8 p$ E3 q4 \
- FileItem item = (FileItem)iter.next();
3 ?- h! E. u8 M- I, E+ \0 F( {7 t/ M - if(item.isFormField()){. v( ^; q2 P; Q/ s
- String name = item.getFieldName();3 C! a. ~4 g5 d) \. k ?1 k1 e1 V
- System.out.println(name);& Z0 M/ V- P& m+ a$ E
- }else{
" `, n5 C8 D6 R/ H7 P& I - //表单名字
) g e m) e" | - String fileName = item.getName();+ c/ @' `- d5 r8 }8 o
- InputStream uploadedStream = item.getInputStream();
9 I# y& L" A8 k& r# e- v - HDFSOperation hdfsOperation = new HDFSOperation();
3 V: Z9 f [7 m+ _( E: t -
; H8 R* t" U. ~! y- X5 Z% G - String hdfsPath = dfs.HDFSConfig.getHDFSPath() + fileName;
+ j9 ^ P1 L( |3 @1 F. s - boolean flag = hdfsOperation.upLoad(uploadedStream, hdfsPath); $ u/ b. i; C) ^" G
- - d, [3 B( ~* i
- if(flag){
4 N3 q* X, m6 o$ S - response.sendRedirect("success.jsp");: l8 s" q) c0 F2 U0 `/ g, ^
- }else{
( H! A7 Q4 t0 j t4 f" W - response.sendRedirect("failure.jsp");
; ]% d# h' j: g - }
5 } ^0 `7 B% ^9 f - }5 `8 w" V# T3 X0 o8 {% Y. U' q
- }2 A$ k/ u4 D3 f; h
- * Q& H a+ G Y0 _
- } catch (Exception e) { ( C2 m( G ^# b; P
- e.printStackTrace();
7 ^/ ?, w7 g# F4 W4 G, {5 a& J0 G - }, l+ w+ ]! X" Y/ C3 a' a4 C5 f
- } u; H, c% ^: h5 w F, B r7 u
- 4 B2 ?2 R$ g. |& |
- }0 ~+ f: [3 H3 Z1 N& v0 H
- Y6 P. E* k4 K9 e( R) g
- }
复制代码 配置:
& A$ x# Q9 c1 r( E# p- <configuration>
7 j0 l$ u1 Z6 x/ w7 W - <property>
4 l5 r( | Q7 Y; u& ]+ H - <name>fs.default.name </name>) m1 K5 T$ {+ z! D0 N7 U
- <value>hdfs://10.6.12.101:8888</value>$ n7 k, t$ @! c. X2 {: V
- </property>
: v" h8 n1 ]4 X$ v. ^+ Y" N- P - <property>9 }) V1 Y7 I! A& e% x6 Y
- <name>hadoop.tmp.dir</name>
- m( c' L A& d - <value>/home/hadoopuser/tmp</value>
% @6 t2 R* i; q4 ^* J' ^$ z3 r - </property>
8 D" L) A( s6 ]& w( U y: S' z - </configuration>
复制代码 源码下载地址:点击下载
$ l$ O( W! O' V9 V# s1 l& W$ U9 ~, |0 ^0 w5 A, i: g
, B$ Q$ a/ u+ Z5 p' U1 ?) {/ V# f: K! t/ j8 o
|
|