TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Hadoop是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上2 Y/ n) G0 ~" f# i+ G, b2 b
上传:4 h2 w4 e0 b8 K1 }9 [( J& x
- /** ) A8 g5 W; ~! o7 e: l. W
- *
& t3 l) d/ A |/ }# D5 [$ S6 L+ F; _ - * 上传控制类,上传页面跳转到这里
U1 d' e1 C7 j( [7 V1 {3 S- L - *
- P% W: F# \2 d! d' ]3 R0 O$ a - * @author 科帮网$ j' B( Q9 G* I6 D7 p4 ^3 c
- * @version [版本号, 2012-08-02] 2 @# d6 {$ D; }5 Q1 O5 E
- * @see [相关类/方法] . r; i/ O% W. ~1 \7 A, i
- * @since version 1.09 n5 `2 T% Y- u
- *// ?" \: n8 \4 W' ^
- public class UploadHDFSServlet extends HttpServlet {
4 ~; i( @# f' e - $ g8 D! Y. ^3 O
- /** *
( E0 a' }/ p* p5 j8 w5 m - * * 该方法负责上传控制处理页面 * + p1 g. o% }/ X4 ]4 ~ ~
- * @param request 客户端到服务器的请求消息
2 Q5 A$ Z' \4 a8 g! V - * @param response 服务器返回给页面的消息
: |* c8 S2 u- j9 { - * @throws ServletException
; o$ A0 [8 `! z; B; \' O - * @throws IOException 0 w# m! Z0 X5 t9 m v! z. `
- */
4 Y; |5 T' K! B) B" h - public void doPost(HttpServletRequest request, HttpServletResponse response)
( L z; x% P0 e/ v: ^ - throws ServletException, IOException {* I1 a( D1 @% w0 e
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);0 Z3 [( Q4 ~ A$ t$ X/ y
- if(!isMultipart){4 Q) L: M4 t6 v e2 z2 |2 c* j6 }
- }else{
+ n g; {: _$ S: v% j - FileItemFactory factory = new DiskFileItemFactory();
$ f& i' G6 K, V. S - ServletFileUpload upload = new ServletFileUpload(factory);0 W& F1 P9 c2 Q( R' V
- try {
3 |5 M+ D1 l- k - List items = upload.parseRequest(request);
8 B" q4 A( r; T4 @) c5 Y1 Z" ^$ V - Iterator iter = items.iterator(); j4 _- h: M* |7 S& u
- while(iter.hasNext()){
$ K+ I; S6 Y- M - FileItem item = (FileItem)iter.next();# n) r2 C$ x3 Z, l. }# E
- if(item.isFormField()){5 H/ k; p3 j- P1 j$ `( P
- String name = item.getFieldName();
/ [4 @% e* I: r" f - System.out.println(name);6 G/ l7 Z7 w7 c2 |
- }else{- p V, c9 E, f2 @7 V
- //表单名字$ o: G0 a# j' L( Y
- String fileName = item.getName();5 y6 H: o: a/ _
- InputStream uploadedStream = item.getInputStream();4 @ v8 d( W) q7 w3 r6 s
- HDFSOperation hdfsOperation = new HDFSOperation();/ n2 X4 u% Z, L8 Z& O
- 6 g/ P! {/ v, y# t) a
- String hdfsPath = dfs.HDFSConfig.getHDFSPath() + fileName;
; d, p" }' {5 ? - boolean flag = hdfsOperation.upLoad(uploadedStream, hdfsPath);
' J" ~+ I: ?! P0 h& Q- z* \ -
; R8 m; d% ^- n2 f; c - if(flag){
( j) A2 {! I6 O8 S* h* V - response.sendRedirect("success.jsp");& d) @ ]/ p! R! y( k
- }else{8 A& `$ c6 H+ R$ z6 d2 \/ @# N
- response.sendRedirect("failure.jsp");
n& G; c8 q( O |% G1 Z - }6 ]% o9 p1 @' j* e/ [
- }3 k" ^/ Q+ c: g8 A: E& t9 P
- }
* A0 u' q& V! x5 u8 ]* f" | -
# t- A: I3 `* e/ B0 L - } catch (Exception e) {
7 z8 B x) |3 R) ?! y7 s - e.printStackTrace();
2 F$ [4 V1 q- A - }
, s2 @% [- {# m2 O& @ - }
/ d' v( v( p2 ^" L4 i* [' x$ u -
& M8 e* z; N' Y9 \ - }4 G' R: X" {/ i1 S* h7 V4 |! X
) W) q6 n8 n- a% A/ L4 R- }
复制代码 配置:
) ?3 Z$ M) T7 H) ?# A4 n- <configuration>: [ J: z: o% _ E( N( w5 e- f2 }
- <property>0 x. v0 }$ _. J5 Z
- <name>fs.default.name </name>
8 V& n' W x3 ~; \( f - <value>hdfs://10.6.12.101:8888</value>
% J$ P% n# j# x/ R - </property>
% j# B s; C2 C: X9 g5 m - <property>
^1 x* G3 y3 `4 S. i d - <name>hadoop.tmp.dir</name>
* s( }: y* y/ L2 D$ f# {& E; m - <value>/home/hadoopuser/tmp</value>
2 s: p g$ O; f* i6 C - </property>
+ {3 \8 _: |( f - </configuration>
复制代码 源码下载地址:点击下载! q3 g) [) A1 J( H2 m: q9 t$ b% U
- G1 F5 o/ M3 f2 I5 _; p
0 k2 ~' Q0 b2 v, k+ w; N0 [2 G7 l/ R2 S. x; p
|
|