- 打开pycharm创建djangoweb项目
- 在terminal安装最新版本的django pyhdfs 包
- 确保你现在安装的python是系统默认的python环境
- 新建一个upload工程
- 新建一个app01应用程序`
upload/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01',
]
upload/urls.py
"""Fileupload URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import include
from Fileupload import views
urlpatterns = [
path('', views.index),
path('admin/', admin.site.urls),
path('app01/', include('app01.urls')),
]
app01/urls.py
from django.urls import path
from app01 import views
urlpatterns = [
path('upload/', views.upload, name='upload')
]
upload/views.py
from django.shortcuts import render
from pyhdfs import HdfsClient
import os
def upload(request):
if request.method == 'GET':
return render(request, 'upload.html')
elif request.method == 'POST':
obj = request.FILES.get('file-5[]')
# print(obj, type(obj), obj.name)
f = open(os.path.join('File', obj.name), 'wb+')
for line in obj.chunks():
f.write(line)
f.close()
# 获取本地文件路径 文件名 文件路径拼接
local_path = os.path.join('./File/', obj.name)
# 定义hdfs文件路径
hadoop_path = os.path.join('/upload/', obj.name)
print(local_path, hadoop_path)
# 开始上传
fs = HdfsClient(hosts='localhost:50070', user_name='root')
if not fs.exists('/upload/'):
fs.mkdirs('/upload/')
print(fs.listdir('/upload/'))
fs.copy_from_local(local_path, hadoop_path)
# 上传结束
return render(request, 'success.html')
upload/app.py
from django.apps import AppConfig
class UploadConfig(AppConfig):
name = 'app01'
templates/upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spring 4 file upload </title>
<link rel="stylesheet" type="text/css" href="/static/css/normalize.css" />
<link rel="stylesheet" type="text/css" href="/static/css/default.css" />
<link rel="stylesheet" type="text/css" href="/static/css/component.css" />
<!-- remove this if you use Modernizr -->
<script>(function(e){const r=e.querySelectorAll("html")[0];r.className=r.className.replace(/(^|\s)no-js(\s|$)/,"$1js$2")})(document,window,0);</script>
</head>
<body>
<div class="container">
<header class="htmleaf-header">
<h1>Hdfs File Upload<span>Hadoop Hdfs</span></h1>
<div class="htmleaf-links">
<a class="htmleaf-icon icon-htmleaf-home-outline" href="http://www.baidu.com" title="百度一下" target="_blank">
<span>Baidu</span>
</a>
<a class="htmleaf-icon icon-htmleaf-home-outline" href="http://www.sogo.com" title="搜狗搜索" target="_blank">
<span>Sogo</span>
</a>
</div>
</header>
<div class="content">
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="box">
<input type="file" name="file-5[]" id="file-5" class="inputfile inputfile-4" data-multiple-caption="{count} files selected" multiple />
<label for="file-5"><figure><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg></figure> <span>Choose a file</span></label>
</div>
{% csrf_token %}
<div class="box">
<input type="submit" name="file-5[]" id="file-4" class="inputfile inputfile-4" data-multiple-caption="{count} files selected" multiple />
<label for="file-4"><figure><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg></figure> <span>upload a file</span></label>
</div>
</form>
</div>
</div>
<script src="/static/js/custom-file-input.js"></script>
{% comment %}
<input type="file" name="filename"/>
<input type="submit" value="上传"/>
{% endcomment %}
</body>
</html>
templates/success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>上传成功</h2>
</body>
</html>
teplates/index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页</title>
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap-grid.min.css" /><!--CSS RESET-->
<link href="http://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/static/css/htmleaf-demo.css"><!--演示页面样式,使用时可以不引用-->
<style>
.demo{ padding: 20px 0;background: linear-gradient(to right,#7AB6B6 50%, #E5CFAA 50%); }
.form-horizontal{
background-color: #fff;
font-family: 'Arimo', sans-serif;
text-align: center;
padding: 50px 30px 50px;
box-shadow: 12px 12px 0 0 rgba(0,0,0,0.3);
}
.form-horizontal .heading{
color: #555;
font-size: 30px;
font-weight: 600;
letter-spacing: 1px;
text-transform: capitalize;
margin: 0 0 50px 0;
}
.form-horizontal .form-group{
margin: 0 auto 30px;
position: relative;
}
.form-horizontal .form-group:nth-last-child(2){ margin-bottom: 20px; }
.form-horizontal .form-group:last-child{ margin: 0; }
.form-horizontal .form-group>i{
color: #999;
transform: translateY(-50%);
position: absolute;
left: 5px;
top: 50%;
}
.form-horizontal .form-control{
color: #7AB6B6;
background-color: #fff;
font-size: 17px;
letter-spacing: 1px;
height: 40px;
padding: 5px 10px 2px 25px;
box-shadow: 0 0 0 0 transparent;
border: none;
border-bottom: 1px solid rgba(0,0,0,0.1);
border-radius: 0;
display: inline-block;
}
.form-control::placeholder{
color: rgba(0,0,0,0.2);
font-size: 16px;
}
.form-horizontal .form-control:focus{
border-bottom: 1px solid #7AB6B6;
box-shadow: none;
}
.form-horizontal .btn{
color: #7AB6B6;
background-color: #EDF6F5;
font-size: 18px;
font-weight: 700;
letter-spacing: 1px;
border-radius: 5px;
width: 50%;
height: 45px;
padding: 7px 30px;
margin: 0 auto 25px;
border: none;
display: block;
position: relative;
transition: all 0.3s ease;
}
.form-horizontal .btn:focus,
.form-horizontal .btn:hover{
color: #fff;
background-color: #7AB6B6;
}
.form-horizontal .btn:before,
.form-horizontal .btn:after{
content: '';
background-color: #7AB6B6;
height: 50%;
width: 2px;
position: absolute;
left: 0;
bottom: 0;
z-index: 1;
transition: all 0.3s;
}
.form-horizontal .btn:after{
bottom: auto;
top: 0;
left: auto;
right: 0;
}
.form-horizontal .btn:hover:before,
.form-horizontal .btn:hover:after{
height: 100%;
width: 50%;
opacity: 0;
}
.form-horizontal .create_account{
color: #D6BC8B;
font-size: 16px;
font-weight: 600;
display: inline-block;
}
.form-horizontal .create_account:hover{
color: #7AB6B6;
text-decoration: none;
}
</style>
</head>
<body>
<div class="htmleaf-container">
<header class="htmleaf-header">
<h1>welcome to django<span>Django-2.2.2</span></h1>
<div class="htmleaf-links">
<a class="htmleaf-icon icon-htmleaf-home-outline" href="http://www.baidu.com" title="百度一下" target="_blank">
<span>Baidu</span>
</a>
<a class="htmleaf-icon icon-htmleaf-home-outline" href="http://www.sogo.com" title="搜狗搜索" target="_blank">
<span>Sogo</span>
</a>
</div>
</header>
<div class="demo form-bg">
<div class="container">
<div class="row">
<div class="col-md-offset-4 col-md-4 col-sm-offset-3 col-sm-6">
<form class="form-horizontal" action="" method="POST">
<div class="heading">Login form</div>
<div class="form-group">
<i class="fa fa-user"></i><input required name="login[username]" type="email" class="form-control" placeholder="Username" id="exampleInputEmail1">
</div>
<div class="form-group">
<i class="fa fa-lock"></i><input required name="login[password]" type="password" class="form-control" placeholder="Password"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"><i class="fa fa-arrow-right"></i></button>
<span>Don't have an account? <a href="" class="create_account">Sign up</a></span>
</div>
<span style="color: #761c19">{{error_msg}}</span>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
在项目根目录新建一个File文件夹用来接收上传过来的文件,文件先上传到服务端,再按地址上传到hdfs上
hadoop-hdfs在windows下配置在这里就不叙述了,比较麻烦,我用的是hadoop2.7.7在windows10系统下配置的
打开聪明的输入:
start-dfs.cmd
启动hdfs,运行django程序选择文件,上传,在浏览器地址栏输入
localhost:50070
进入hdfs browser 查看上传的文件