原文地址:

https://www.cnblogs.com/instant7/p/4159022.html

---------------------------------------------------------------------------------------------

  本篇随笔记录如何导入google-cluster-data-2011-1-2的

job_events和task_events到MySQL

1. 下载数据

download_job_events:

import urllib2

url = 'https://commondatastorage.googleapis.com/clusterdata-2011-2/'
f = open('C:\\SHA256SUM')
l = f.readlines()
f.close()
for i in l:
if i.count('job_events')>0:
fileAddr = i.split()[1][1:]
fileName = fileAddr.split('/')[1]
print 'downloading', fileName
data = urllib2.urlopen(url+fileAddr).read()
print 'saving', fileName
fileDown = open('C:\\job_events\\'+fileName, 'wb')
fileDown.write(data)
fileDown.close()

(ps:   由于上面的代码为python2.7的,现在一般使用python3的,于是给出python3版本的代码如下:

#encoding:UTF-8

from urllib import request

url = 'https://commondatastorage.googleapis.com/clusterdata-2011-2/'
f = open('C:\\SHA256SUM')
l = f.readlines()
f.close()
for i in l:
if i.count('job_events')>0:
fileAddr = i.split()[1][1:]
fileName = fileAddr.split('/')[1]
print('downloading', fileName)
data = request.urlopen(url+fileAddr).read()
print('saving', fileName)
fileDown = open('C:\\job_events\\'+fileName, 'wb')
fileDown.write(data)
fileDown.close()

download_task_events:

import urllib2

url = 'https://commondatastorage.googleapis.com/clusterdata-2011-2/'
f = open('C:\\SHA256SUM')
l = f.readlines()
f.close()
for i in l:
if i.count('task_events')>0:
fileAddr = i.split()[1][1:]
fileName = fileAddr.split('/')[1]
print 'downloading', fileName
data = urllib2.urlopen(url+fileAddr).read()
print 'saving', fileName
fileDown = open('C:\\task_events\\'+fileName, 'wb')
fileDown.write(data)
fileDown.close()

(ps:   由于上面的代码为python2.7的,现在一般使用python3的,于是给出python3版本的代码如下:

#encoding:UTF-8

from urllib import request

url = 'https://commondatastorage.googleapis.com/clusterdata-2011-2/'
f = open('C:\\SHA256SUM')
l = f.readlines()
f.close()
for i in l:
if i.count('task_events')>0:
fileAddr = i.split()[1][1:]
fileName = fileAddr.split('/')[1]
print('downloading', fileName)
data = request.urlopen(url+fileAddr).read()
print('saving', fileName)
fileDown = open('C:\\task_events\\'+fileName, 'wb')
fileDown.write(data)
fileDown.close()

)

注意:这次用的数据是

clusterdata-2011-2

不同于之前重画GoogleCLusterData中的

clusterdata-2011-1

2. 解压缩

由于不能直接导入压缩包里的数据到mysql,故先将它们解压缩

unzip_job_events:

import gzip
import os fileNames = os.listdir('C:\\task_events') for l in fileNames:
print 'now at: '+ l
f = gzip.open('C:\\job_events\\'+l)
fOut = open('C:\\job_events_unzip\\'+l[:-3], 'w')
content = f.read()
fOut.write(content)
f.close()
fOut.close()
#raw_input()

python3 版本

import gzip
import os fileNames = os.listdir('C:\\job_events') for l in fileNames:
print( 'now at: '+ l )
f = gzip.open('C:\\job_events\\'+l)
fOut = open('C:\\job_events_unzip\\'+l[:-3], 'wb')
content = f.read()
fOut.write(content)
f.close()
fOut.close()
#raw_input()

unzip_task_events:

import gzip
import os fileNames = os.listdir('C:\\task_events') for l in fileNames:
print 'now at: '+ l
f = gzip.open('C:\\task_events\\'+l)
fOut = open('C:\\task_events_unzip\\'+l[:-3], 'w')
content = f.read()
fOut.write(content)
f.close()
fOut.close()

python3 版本:

import gzip
import os fileNames = os.listdir('C:\\task_events') for l in fileNames:
print( 'now at: '+ l )
f = gzip.open('C:\\task_events\\'+l)
fOut = open('C:\\task_events_unzip\\'+l[:-3], 'wb')
content = f.read()
fOut.write(content)
f.close()
fOut.close()
#raw_input()

3. 建数据库

create_job_events:

create table job_events(
time bigint,
missing_info int,
job_id bigint,
event_type int,
user text,
scheduling_class int,
job_name text,
logical_job_name text)
engine = myisam;

create_task_events:

create table task_events(
time bigint,
missing_info int,
job_id bigint,
task_index bigint,
machine_id bigint,
event_type int,
user text,
scheduling_class int,
priority int,
cpu_request float,
memory_request float,
disk_space_request float,
difference_machine_restriction boolean
)engine = myisam;

注意:由于数据量非常大,这里一定要选择myisam作为engine。

4. 导入数据

由于数据中有部分为空的值,需要先设定mysql使其能够导入空值。

具体方法为:

在mysql的控制台输入

SET @@GLOBAL.sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";

之后就可以开始导入数据了。

注意!!以下代码在导入类似2.3e-10的数据会产生严重问题,具体为导入的数据在MySQL中变为负数,而且绝对值不小!!!

loadJobEvents2MySQL.py

import os
import MySQLdb fileNames = os.listdir('C:\\task_events_unzip') conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="googleclusterdata",charset="utf8")
cursor = conn.cursor()
cursor.execute('truncate job_events') for f in fileNames:
print 'now at: '+ f
order = "load data infile 'C:/job_events_unzip/%s' into table job_events fields terminated by ',' lines terminated by '\n'" %f
print order
cursor.execute(order)
conn.commit()

loadTaskEvents2MySQL.py

import os
import MySQLdb fileNames = os.listdir('C:\\task_events_unzip') conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="googleclusterdata",charset="utf8")
cursor = conn.cursor()
cursor.execute('truncate task_events') for f in fileNames:
print 'now at: '+ f
order = "load data infile 'C:/task_events_unzip/%s' into table task_events fields terminated by ',' lines terminated by '\n'" %f
print order
cursor.execute(order)
conn.commit()

注意:这里需要相应的修改密码和使用的数据库名(db)

 
 

---------------------------------------------------------------------------------------------

【转载】 导入GoogleClusterData到MySQL的更多相关文章

  1. 导入GoogleClusterData到MySQL

    本篇随笔记录如何导入google-cluster-data-2011-1-2的 job_events和task_events到MySQL 1. 下载数据 download_job_events: im ...

  2. linux下导入、导出mysql数据库命令 下载文件到本地

    一.下载到本地 yum install lrzsz sz filename  下载 rz filename  上传   linux下导入.导出mysql数据库命令 一.导出数据库用mysqldump命 ...

  3. linux、windows下导入、导出mysql数据库命令

    一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径): 1.导出数据和表结构:[不是mysql里的命令]mysqldump -u用户名 -p密码 数据库名 > 数据 ...

  4. 完美转换MySQL的字符集 Mysql 数据的导入导出,Mysql 4.1导入到4.0

    MySQL从4.1版本开始才提出字符集的概念,所以对于MySQL4.0及其以下的版本,他们的字符集都是Latin1的,所以有时候需要对mysql的字符集进行一下转换,MySQL版本的升级.降级,特别是 ...

  5. cpanel导入大数据库(mysql)的方法

    phpmyadmin是一件很方便的在线管理MySQL数据库的工具,但对于较大的数据库的导出和导入却很容易出错.特别是导入工作,通常5M已经是它的极限了.这里,主要介绍一下如何通过cPanel导入大型的 ...

  6. mysql 数据库导入数据报错MySQL server has gone away解决办法

    mysql 数据库导入数据报错MySQL server has gone away解决办法: 进入数据库执行以下命令即可: set global wait_timeout = 2880000; set ...

  7. 导入数据到mysql的一种简单的方法

    由于ubuntu默认自带的mysql版本号为5.5,并不能使用load data infile这样的高级的功能,因此我们写了一个通用的脚本来上传文件 shell脚本 cat ./employee.cs ...

  8. 随笔编号-09 批量导入数据(Mysql)报MySQL server has gone away 问题的解决方法

    问题场景: 使用*.sql 脚本,批量导入数据到mysql实例中,使用DOS 界面导入的,期间,到最后一步 source D:\aaa.sql  回车后,系统提示 MySQL server has g ...

  9. Sqoop导入数据到mysql数据库报错:ERROR tool.ExportTool: Error during export: Export job failed!(已解决)

    问题描述: Container killed by the ApplicationMaster. Container killed on request. Exit code is 143 Conta ...

随机推荐

  1. 51nod 2488 矩形并的面积

    在二维平面上,给定两个矩形,满足矩形的每条边分别和坐标轴平行,求这个两个矩形的并的面积.即它们重叠在一起的总的面积. 收起   输入 8个数,分别表示第一个矩形左下角坐标为(A,B),右上角坐标为(C ...

  2. 使用Restful风格中的post使用过遇到前端数据传送不到后端

    问题描述:使用postman进行请求时候,前端的数据,一直在后端接收不到,找了好多资料,竟然是因为一个注解的原因. 后端接收设置:@RequestParam@ReqeustBody设置的原因 因为sp ...

  3. java.lang.RuntimeException: org.springframework.dao.DuplicateKeyException:

    java.lang.RuntimeException: org.springframework.dao.DuplicateKeyException: ### Error updating databa ...

  4. LG5325 【模板】Min_25筛

    P5325 [模板]Min_25筛 题目背景 模板题,无背景. 题目描述 定义积性函数$f(x)$,且$f(p^k)=p^k(p^k-1)$($p$是一个质数),求 $$\sum_{i=1}^n f( ...

  5. [Apio2010]patrol 巡逻

    1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 2541  Solved: 1288[Submit][S ...

  6. Spring源码窥探之:@Value

    1. 首先定义实体 /** * @author 70KG * @Title: Apple * @Description: 苹果实体 * @date 2018/10/22下午9:26 * @From w ...

  7. ORM框架三种映射在Springboot上的使用

    ORM(对象/关系映射)是数据库层非常重要的一部分,有三种常用的映射关系 1.多对一 tbl_clazz clazz{ id name description grade_id charge_id } ...

  8. C#调用一下CMD

    C#程序调用CMD执行命令   在windows环境下,命令行程序为cmd.exe,是一个32位的命令行程序,微软Windows系统基于Windows上的命令解释程序,类似于微软的DOS操作系统.输入 ...

  9. NOI.ac模拟赛20181021 ball sequence color

    T1 ball 可以发现每次推动球时,是将每个球的位置 −1-1−1 ,然后把最左边的球放到 P−1P-1P−1 处. 记个 −1-1−1 次数,再用set维护就好了. #include <bi ...

  10. (转载) 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)

    这一篇是从0开始搭建SQL Server AlwaysOn 的第四篇,这一篇开始搭建异地机房节点 注意点1 注意异地节点最好至少有2个AG节点,否则在本地节点进行手动故障转移的时候会出现仲裁警告,提示 ...