hadoop 》》 django 简单操作hdfs 语句

》》

from django.shortcuts import render
# Create your views here.

from hdfs.client import Client
from django.views import View
from hdfs.client import Client
import os

#
# # 关于python操作hdfs的API可以查看官网:
# # https://hdfscli.readthedocs.io/en/latest/api.html
#
# # 读取hdfs文件内容,将每行存入数组返回
# def read_hdfs_file(client, filename):
#     lines = []
#     with client.read(filename, encoding=‘utf-8‘, delimiter=‘\n‘) as reader:
#         for line in reader:
#             # pass
#             # print line.strip()
#             lines.append(line.strip())
#     return lines
#
#
# # 创建目录
# def mkdirs(client, hdfs_path):
#     client.makedirs(hdfs_path)
#
#
# # 删除hdfs文件
# def delete_hdfs_file(client, hdfs_path):
#     client.delete(hdfs_path)
#
#
# # 上传文件到hdfs
def put_to_hdfs(client, local_path, hdfs_path):
    client.upload(hdfs_path, local_path, cleanup=True)

#
# # 从hdfs获取文件到本地
# def get_from_hdfs(client, hdfs_path, local_path):
#     download(hdfs_path, local_path, overwrite=False)
#
#
# # 追加数据到hdfs文件
# def append_to_hdfs(client, hdfs_path, data):
#     client.write(hdfs_path, data, overwrite=False, append=True)
#
#
# # 覆盖数据写到hdfs文件
def write_to_hdfs(client, hdfs_path, data):
    client.write(hdfs_path, data, overwrite=True, append=False)

#
# # 移动或者修改文件
def move_or_rename(client, hdfs_src_path, hdfs_dst_path):
    client.rename(hdfs_src_path, hdfs_dst_path)
#
#
# # 返回目录下的文件
# def list(client, hdfs_path):
#     return client.list(hdfs_path, status=False)
#
#
# # root:连接的跟目录
# client = Client("http://192.168.88.129:50070",
#                 root="/", timeout=5 * 1000, session=False)
# # put_to_hdfs(client,‘a.csv‘,‘/user/root/a.csv‘)
# # append_to_hdfs(client,‘/b.txt‘,‘111111111111111‘+‘\n‘)
# # write_to_hdfs(client,‘/b.txt‘,‘222222222222‘+‘\n‘)
# # move_or_rename(client,‘/b.txt‘, ‘/user/b.txt‘)
# mkdirs(client, ‘/input1/python1‘)
# print(list(client,‘/input‘))
# read_hdfs_file(client,‘/‘)
# client.list("/")
def mkdirs(client, hdfs_path):
    client.makedirs(hdfs_path)

def get_IS_File(client,hdfs_paht):
    return client.status(hdfs_paht)[‘type‘] == ‘FILE‘
from hadoop_hdfs.settings import UPLOAD_ROOT
import os

def put_to_hdfs(client, local_path, hdfs_path):
        client.upload(hdfs_path, local_path, cleanup=True)
client = Client("http://192.168.88.129:50070",
                root="/", timeout=5 * 1000, session=False)
class Index(View):
    def get(self,request):
        return render(request,"index.html")
    def post(self,request):
        def uploadfile(img):
            f = open(os.path.join(UPLOAD_ROOT, ‘‘, img.name), ‘wb‘)
            for chunk in img.chunks():
                f.write(chunk)
            f.close()

        def read_hdfs_file(client, filename):
            lines = []
            with client.read(filename, encoding=‘utf-8‘, delimiter=‘\n‘) as reader:
                for line in reader:
                    # pass
                    # print line.strip()
                    lines.append(line.strip())
            return lines

        file = request.FILES.get("file")
        uploadfile(file)
        all_file = client.list("/")
        for i in all_file:
            file_true =get_IS_File(client,"/{}".format(i)),i
            print(file_true)
            # if file_true == "True":
            return render(request,"index.html",locals())
            # else:
            #     pass
            #     # data = {"file_true":file_true}
            #     return render(request,"index.html",locals())
            # else:
            #     pass
        # get_IS_File(all_file,"/")
        # lujin = "/upload/"+file.name
        mkdirs(client,file.name)
        # move_or_rename(client,file.name, ‘/c.txt‘)
        # put_to_hdfs(client, "C:/Users/Lenovo/Desktop/hadoop_hdfs/upload/"+file.name, ‘/‘)
        # write_to_hdfs(client,"upload"+file.name,‘222222222222‘+‘\n‘)
        return render(request,"index.html",locals())

Views.py

 # 导入必要模块
import pandas as pd
from sqlalchemy import create_engine
from  matplotlib import pylab as plt
from django.views import View
from django.shortcuts import render
import os
from hadoop_hdfs.settings import UPLOAD_ROOT
from web.models import *
def uploadfile(img):
 f = open(os.path.join(UPLOAD_ROOT, ‘‘, img.name), ‘wb‘)
 for chunk in img.chunks():
     f.write(chunk)
 f.close()
class Upload(View):
    def get(self,request):
        # show = Image.objects.all()
        return render(request,"haha.html",locals())
    def post(self,request):
        imgs = request.FILES.get(‘img‘)
        uploadfile(imgs)
        all = Image(img="/upload/"+imgs.name)
        all.save()
        return render(request,"haha.html")
    # def post(self,request):
    #     # 初始化数据库连接,使用pymysql模块
    #     # MySQL的用户:root, 密码:147369, 端口:3306,数据库:mydb
    #     engine = create_engine(‘mysql+pymysql://root:@127.0.0.1/dj‘)
    #
    #     # 查询语句,选出employee表中的所有数据
    #     sql = ‘‘‘select * from haha;‘‘‘
    #
    #     # read_sql_query的两个参数: sql语句, 数据库连接
    #     df = pd.read_sql_query(sql, engine)
    #
    #     # 输出employee表的查询结果
    #     print(df)
    #
    #     # 新建pandas中的DataFrame, 只有id,num两列
    #     # df = pd.DataFrame({‘id‘:[1,2],‘name‘: [‘111‘,‘222‘],‘image_url‘:[‘http://111‘,‘http://222‘]})
    #     # df = pd.DataFrame({"id":df[‘sentiment‘],"text":df[‘text‘],})
    #     df.groupby(by=‘sentiment‘).count()[‘text‘].plot.pie(autopct="%0.4f%%", subplots=True)
    #     plt.savefig("../upload/1.jpg")
    #     plt.show()
    #     # 将新建的DataFrame储存为MySQL中的数据表,不储存index列
    #     # df.to_sql(name=‘lallala‘, con=engine, index=False)
    #
    #     print(‘Read from and write to Mysql table successfully!‘)
    #     return render(request,"haha.html",locals())

class Uploads(View):
    def get(self,request):
        show = Image.objects.all()
        return render(request,"haha.html",locals())

haha.py

原文地址:https://www.cnblogs.com/liuxiaomo/p/12036745.html

时间: 2024-09-29 08:09:49

hadoop 》》 django 简单操作hdfs 语句的相关文章

hadoop的Hive操作hdfs文件第一天

hive的具体练习以下4个目标1. 第一普通的hdfs文件能导入到hive中以供我们查询. create table dept(deptID int,deptName string,address string); load data local inpath '/home/dyq/Documents/dept' overwrite into table dept; select * from dept; hive> select * from dept; OK NULL NULL NULL N

使用hadoop的类操作HDFS

1. 创建文件夹 private static final String PATH = "hdfs://hadoop:9000/"; private static final String DIR = "/d2"; public static void main(String[] args) throws Exception { FileSystem fileSystem = FileSystem.get(new URI(PATH), new Configurati

简单操作sql语句-提升我们工作效率(网站关键词替换sql语句)

有时候要批量修改一些内容真不容易 一个一个改一天过去了,但当你学会了使用sql 语句操作 一秒就可以搞定.所以减少我们工作量我们必需要学会提高工作效率. 可批量修改就这么轻松可以实现. update 表名 set article =(REPLACE(列名,'需要替换的内容','替之后的内容'))

hadoop实践02---eclipse操作hdfs的api上传文件

1.eclipse中编写代码后双击main方法--->Run as ---> java application ,然后指定的文件 就会提交到hdfs中. 2.查看文件:http://192.168.108.128:50070/dfshealth.html#tab-overview package hdfs24; import java.net.URI; import java.net.URISyntaxException; import org.apache.hadoop.conf.Confi

python django简单操作

准备: pip3 install  django==1.10.3 cmd django-admin startproject  guest  创建一个guest的项目 cd guest manage.py 查看各种manage的命令 manage.py startapp sign 创建一个应用 manage.py runserver启动这个应用,默认127.0.0.1:8000 1.将sign 加入到guest/settings.py文件 INSTALLED_APPS中,配置/guest/url

Hadoop读书笔记(三)Java API操作HDFS

Hadoop读书笔记(一)Hadoop介绍:http://blog.csdn.net/caicongyang/article/details/39898629 Hadoop读书笔记(二)HDFS的shell操作:http://blog.csdn.net/caicongyang/article/details/41253927 JAVA URL 操作HDFS OperateByURL.java package hdfs; import java.io.InputStream; import jav

Django简单的数据库操作

当然,本篇的前提是你已经配置好了相关的环境,这里就不详细介绍. 一. 在settings.py文件中设置数据库属性. 如下: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', #MySQL数据库 'NAME': 'test1', #库名 'USER': 'root', #用户名 'PASSWORD': 'a8115080', #密码 'HOST':'localhost', 'PORT':'3306', } } 二.

Hadoop大象之旅009-通过Shell操作hdfs

Hadoop大象之旅009-通过Shell操作hdfs 老帅 调用Hadoop分布式文件系统(HDFS)Shell命令应使用bin/hadoop fs <args>的形式. 1.查看hdfs都有哪些Shell命令 参照前面章节中所述方法,使用SecureCRTPortable.exe登录CentOS; 使用命令"hadoopfs"查看hdfs的命令列表,如下图所示: hdfs的所有命令的格式,都为:hadoop fs –xx,具体使用方法,可参考官方文档: http://h

hadoop集群配置和在windows系统上运用java操作hdfs

安装 配置 概念 hadoop常用shell命令 使用java操作hadoop 本文介绍hadoop集群配置和在windows系统上运用java操作hdfs 安装 http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-3.1.1/ sudo apt-get install ssh sudo apt-get install rsync mkdir /usr/local/hadoop tar -zxvf hadoop-3.1.1.tar.gz -C