TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Hadoop是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上
6 X+ j, u( W8 n6 }" @上传:9 H7 e' j- w. R" A
- /**
. ~2 f7 Y: x5 h+ i' n - *
& S4 A/ g g, I0 }& j& T - * 上传控制类,上传页面跳转到这里; |; j# x2 U6 {2 C3 T* C
- *
! \: P- a8 I5 T- Q& G - * @author 科帮网7 C8 Y3 B, w) u' e5 [
- * @version [版本号, 2012-08-02]
/ _. [! u6 g) C - * @see [相关类/方法] 8 u. {) x6 u, m/ Z( v4 f. m' m
- * @since version 1.0
; H; N/ M4 s+ z x7 ^" n0 x - */4 v G+ U: {9 y$ X+ ^9 u! L" y
- public class UploadHDFSServlet extends HttpServlet {! b3 ^* c9 t( ?; h- F+ Z
; P1 H* F$ y8 T" h- /** *
4 ?& x, t; X, ]! r& X' O - * * 该方法负责上传控制处理页面 *
7 Q1 ^% V9 n7 \6 e7 O: w! q9 Y - * @param request 客户端到服务器的请求消息- h, {5 K4 C+ e2 X; h8 U
- * @param response 服务器返回给页面的消息
3 J. G- J+ S3 o2 ?+ @, [* h p - * @throws ServletException
8 E" ^9 R5 ]; H3 u - * @throws IOException
$ `0 w0 ?; L$ ]. M! k1 i - */; ~9 u* O2 J) [+ K
- public void doPost(HttpServletRequest request, HttpServletResponse response)0 a4 ]4 y9 R7 x6 H& S3 {
- throws ServletException, IOException {( N0 ~" z3 I: j3 B
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);
1 Y" z4 ^; w: }! l - if(!isMultipart){* \' _0 }/ d# w* e. ~( a% l$ g
- }else{
6 x( X3 N$ v W% t$ n7 U - FileItemFactory factory = new DiskFileItemFactory();
, O* r5 G# U, Q. u& u# C - ServletFileUpload upload = new ServletFileUpload(factory);/ |" ]! A! ?9 Z
- try {
) `, m! c/ H+ w0 H* u0 }' j - List items = upload.parseRequest(request);
+ J; y- P6 I3 E) V3 k) @/ ] - Iterator iter = items.iterator();" F2 t# E. v; @6 F
- while(iter.hasNext()){
) F2 k% G. T' A# T O) r4 m! ?) D - FileItem item = (FileItem)iter.next();( |) P6 d6 S1 m; N# E1 `- l$ A
- if(item.isFormField()){
9 m) [* V6 t+ T) L - String name = item.getFieldName();
9 T, R: a' [5 L" x - System.out.println(name);
% M4 f- v! D" ^; Q \ - }else{2 Z# t3 W8 c8 ~. R
- //表单名字
7 ~ ?2 s: a( s1 K* ^/ ^7 K - String fileName = item.getName();# S O1 ^/ g! s1 @4 }) d) _( k
- InputStream uploadedStream = item.getInputStream();
( D- k' a3 f& Y - HDFSOperation hdfsOperation = new HDFSOperation();
- `5 e+ Z/ X% B# z% X, d" j - + }3 f8 s- `) b1 v" r+ J
- String hdfsPath = dfs.HDFSConfig.getHDFSPath() + fileName;
3 R* K$ m4 k5 A( r; X5 n - boolean flag = hdfsOperation.upLoad(uploadedStream, hdfsPath);
/ Y4 k; A' Z8 r h0 K - 8 ]8 l0 ^: _0 y. N* e4 e% i/ \7 u% y
- if(flag){8 i! g9 s' f" B; N, L1 G; b* k
- response.sendRedirect("success.jsp");
& r; ~, }/ ]+ f - }else{% [; a, H7 P4 i+ M. |6 @6 L1 @
- response.sendRedirect("failure.jsp");
# \1 Q6 u) v1 @7 o+ |) h/ H7 b - }! Z) q, H1 K8 ?/ n9 r
- }
. Y* _" j+ o* g - }
% y* x/ \8 S# b) ]9 ~* O - $ p7 `5 M1 D, `/ y; \, E
- } catch (Exception e) { 0 b2 T9 N- ^3 t7 G: a M
- e.printStackTrace();) Z( e5 `0 d- Y- `" _
- }
9 X+ e1 l% q' F1 G' N3 o) Z: F - }
: a3 p( m) h4 |+ d$ q - J- ]4 r9 T9 g+ n* O0 h: }
- }
* b+ J2 }3 O, J- [( \' X5 C. \
. D B/ z/ L" ~$ Y2 K3 n- }
复制代码 配置:
; u3 u8 @; v" ]( \- <configuration>6 ?6 b# h* R9 I
- <property>1 V) ]6 B9 Z3 h8 @# {: B8 ~2 l7 x+ ]
- <name>fs.default.name </name>3 h3 X, V+ ]8 E$ J( c, B9 p. o; x
- <value>hdfs://10.6.12.101:8888</value>
' A. r- L$ r) U8 i2 r9 K - </property>
+ H: W( I# @7 o - <property>
2 ^$ u/ ~: ]2 [5 a - <name>hadoop.tmp.dir</name>$ p6 b- |& S ?2 W9 w! t' V
- <value>/home/hadoopuser/tmp</value>$ j2 _7 ]0 T& z. q: Y
- </property>
& L' x" {, C$ p8 V: w - </configuration>
复制代码 源码下载地址:点击下载5 |9 V; t; \9 b* D( E
; x$ G( e& q5 Z7 t6 ~ s% [6 B+ D" f' X8 { V$ ]) t
, e% N4 l% {% {6 S2 W& \ |
|