把传统数据迁移到现代大数据平台有时是一件很令人畏惧的事,诚然,不是每个人都需要这么去做,但是有时候新的分析方法确实能让人看到数据中的惊喜,Marc Borowczak近日撰文描述了一些方法。
下为译文
PART 1
在这篇简短的指导中,笔者将会简短地回顾一种方法并且用我喜欢的数据集来演示。这不是一个ML库也不是一个Kaggle竞赛的数据集,仅仅是积累了数十年笔者跟踪塑料模型集合产生的数据,如此这般一定会适合传统的标准。
描述的步骤是在笔记本电脑上用VirtualBox运行Ubuntu 16.04.1 LTS Gnome,并且假定你已经保存了一张Excel notebook的CSV格式(或者你已经有了一些现有的CSV文件)。这个例子将使用一个从Excel表中保存的CSV文件,也就是Unbuilt.csv。假定该文件是受保护的,但可以被MySQL 5.7.15访问。这个例子中的CSV位于/var/lib/mysql-files/Unbuilt.csv。以下步骤是必需的:
在MySQL中引入.csv文件,并且可以备份一份压缩的MySQL数据库文件。
连接到在Spark2.0.1上的MySQL数据库然后访问它的的数据:这里将使用jupyter pyspark演示一种交互式的Python方法,并且在下文留下一种基于现有方法的Rstudio Sparkyl访问。
1. 在MySQL表中引入一个.CSV 文件
这一步很容易完成,假设你已经安装完了MySQL。
A. 在MySQL中创建一个新的数据库
首先你需要创建一个新的数据库。简便起见,你以根账户访问MySQL创建一个新的数据库(或者使用任意别的你已经有的用户证书)。对于这个例子来说我们将会创建一个模型数据库,并且挑选它以备使用:
marc@marc-VirtualBox:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.15-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database models;
Query OK, 1 row affected (0.00 sec)
mysql> use models;
Database changed
B. 在你的数据库里创建一个新的表
下一步是创建一个表来引入.cvs数据,同时对于每一个领域宣告一种兼容的格式(在这种情况下正在创建一种被称作unbuilt的表)
mysql> create table unbuilt (
-> itemid int not null auto_increment,
-> iscale int not null,
-> type varchar(63) not null,
-> subtype varchar(63) not null,
-> brand varchar(63) not null,
-> model varchar(31) not null,
-> description varchar(255) not null,
-> msrp decimal(10,2) not null,
-> auction decimal(10,2) not null,
-> maxval decimal(10,2) not null,
-> minval decimal(10,2) not null,
-> decals varchar(31) not null,
-> remarks varchar(255) not null,
-> primary key (itemid)
-> );
Query OK, 0 rows affected (0.04 sec)
C. 引入CSV数据
最后一步是引入.csv数据到表中
mysql> load data infile '/var/lib/mysql-files/Unbuilt.csv'
-> into table unbuilt
-> fields terminated by ','
-> lines terminated by '\r\n'
-> ignore 1 rows;
Query OK, 625 rows affected (0.02 sec)
Records: 625 Deleted: 0 Skipped: 0 Warnings: 0
mysql> exit
Bye
为了完成这次操作,需要在mysql文件夹中采用适当的许可。没有成功从这个位置引入该文件会导致如下错误:
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
对于MySQL安全设置而言,你引用了在线文档。我们接下来描述的以下的可选步骤是为了增强你刚落户于的数据库,并且也在MySQL的文档中描述过。查看示例 htpp://dev.mysql.com/doc/refman/5.7/en/examples.html。
marc@marc-VirtualBox:~$ mysqldump -u root -p --quick models | gzip > models.gz
Enter password:
一个快速且明智的检查方法由阅读数据库,挑选表并运行一次快速的查询组成。
marc@marc-VirtualBox:~$ mysql -u root -p models
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.15-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| metastore_db |
| models |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql> show tables;
+------------------+
| Tables_in_models |
+------------------+
| unbuilt |
+------------------+
1 row in set (0.00 sec)
mysql> describe unbuilt;
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| itemid | int(11) | NO | PRI | NULL | auto_increment |
| iscale | int(11) | NO | | NULL | |
| type | varchar(63) | NO | | NULL | |
| subtype | varchar(63) | NO | | NULL | |
| brand | varchar(63) | NO | | NULL | |
| model | varchar(31) | NO | | NULL | |
| description | varchar(255) | NO | | NULL | |
| msrp | decimal(10,2) | NO | | NULL | |
| auction | decimal(10,2) | NO | | NULL | |
| maxval | decimal(10,2) | NO | | NULL | |
| minval | decimal(10,2) | NO | | NULL | |
| decals | varchar(31) | NO | | NULL | |
| remarks | varchar(255) | NO | | NULL | |
+-------------+---------------+------+-----+---------+----------------+
13 rows in set (0.00 sec)
mysql> select model,description,auction from unbuilt where auction > 250;
+-----------+------------------------------------------------------+---------+
| model | description | auction |
+-----------+------------------------------------------------------+---------+
| HKM01E04 | B-17G Flying Fortress | 299.00 |
| 60311 | Zero Fighter Real Sound Action Set | 487.36 |
| VF2405 | P-47D Thunderbolt Gabreski | 480.00 |
| PA226 | Phantom Huey Chopper | 570.00 |
| 3705 | BB-63 Missouri 'Mighty Mo' with PE and flags | 446.40 |
| DBS1-6000 | Ford Mustang Mach I | 299.99 |
| H1552-598 | Allison Turboprop Jet Engine Motorized Model 501-DI3 | 365.00 |
| 16010 | Harley Davidson FXE1200 Super Glide | 268.40 |
| 16041 | Harley Davidson FLSTSB 'Fat Boy' with PE | 270.79 |
+-----------+------------------------------------------------------+---------+
9 rows in set (0.00 sec)
用现在迁移到MySQL的数据,我们现在走在了用现代的数据科学工具例如Spark2.0.1来访问它的康庄大道上。
2.在Spark2.0.1中用Jupyter PySpark访问MySQL
先前描述了怎样使用Toree去交互地使用Spark 1.6.1.运行Scala 2.10或者PySpark。Toree最近被更新来支持Scala2.11并且后者的创建已经被改编来运行在2016-10-6发行的Spark2.0.1。
假设你有一个本地Hadoop2.7.2操作版的工作站。一个典型的安装有一个hduser发行,然后我们可以很容易地连接进一个HDFS环境并从专用文件夹发布。
marc@marc-VirtualBox:~$ su hduser
Password:
hduser@marc-VirtualBox:/home/marc$ start-all.sh
This script is Deprecated. Instead use start-dfs.sh and start-yarn.sh
Starting namenodes on [localhost]
localhost: starting namenode, logging to /usr/local/hadoop/logs/hadoop-hduser-namenode-marc-VirtualBox.out
localhost: starting datanode, logging to /usr/local/hadoop/logs/hadoop-hduser-datanode-marc-VirtualBox.out
Starting secondary namenodes [0.0.0.0]
0.0.0.0: starting secondarynamenode, logging to /usr/local/hadoop/logs/hadoop-hduser-secondarynamenode-marc-VirtualBox.out
starting yarn daemons
starting resourcemanager, logging to /usr/local/hadoop/logs/yarn-hduser-resourcemanager-marc-VirtualBox.out
localhost: starting nodemanager, logging to /usr/local/hadoop/logs/yarn-hduser-nodemanager-marc-VirtualBox.out
hduser@marc-VirtualBox:/home/marc$ su marc
Password:
marc@marc-VirtualBox:~$ cd spyder
marc@marc-VirtualBox:~/spyder$ jupyter notebook
...
Starting Spark Kernel with SPARK_HOME=/usr/local/spark
[I 12:04:52.486 NotebookApp] Kernel started: 6be7a5b5-3a4e-4eeb-a7ef-de12cbbcf474
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel).
(Scala,org.apache.toree.kernel.interpreter.scala.ScalaInterpreter@295eaa7c)
(PySpark,org.apache.toree.kernel.interpreter.pyspark.PySparkInterpreter@3918c187)
(SparkR,org.apache.toree.kernel.interpreter.sparkr.SparkRInterpreter@2c88b9fc)
(SQL,org.apache.toree.kernel.interpreter.sql.SqlInterpreter@64dafeed)
16/10/05 12:05:01 WARN Main$$anon$1: No external magics provided to PluginManager!
可以看出,命令打开一个熟悉的jupyter Notebooks并且方便地连接到一个pyspark内核。下面将详细介绍一些访问步骤,以证明连接是可用的。

结论
这应该有助于展示将遗留数据迁移和连接到MySQL的目的。进一步我们回顾了如何使用jupyter notebook访问pyspark和利用Toree/Spark2.0.1提供的交互界面。
真的没有必要放弃遗留数据:将数据迁移到新的平台将使企业能够在更广泛的时间尺度上提取和分析数据,并打开新的方法来利用ML技术,分析结果和对结果采取行动。导入mysql数据就像在R-Studio上操作一样容易。在即将到来的文章中我们将会讨论那条路。
引用
1. MySQL 5.7在线文档
2. 拓展机器学习的方法(第二部分)
3. Apache Spark 2.0.1 在线文档
4. Apache Toree Github
5. Matplotlib 在线文档
PART 2
这里演示了相同的结果可以以更直接的方式实现。我们将要在我们上次使用的相同的平台(Ubuntu
16.04.1 LTS 运行于一个 windows VirtualBox Hadoop 2.7.2 和 Spark 2.0.1)和相同的数据集(我的传统的模型集合Unbuilt.CSV)上阐明这个。我们的目标是展示怎样把数据迁移到Hadoop HDFS并且直接地分析它和使用最新的在一个 Jupyter 笔记本中带有PySpark 2.0.1的 ML工具。
许多有趣的事实可以通过子集的结合被推断出来,过滤并且聚合这些信息,并且也在文档中被记录下来。比如,通过一个折线图我们就能够排出最受欢迎的规模,数量最多的模型和最有价值的项目,通过类别来对模型计数,……并且用现代的ML工具操作这些以前的数据。对大规模数据而言,聚合很容易实现,正如在这里阐明的那样。

这,又一次,服务于展示传统数据的直接迁移的目的。回顾了从Jupyter notebook使用PySpark如何访问并且利用Toree/Spark2.0.1提供的交互式的接口。一个Jupyter notebook也被提供出来帮助你迁移数据。这里可以重申真的没有必要抛弃过去的数据。直接或间接地把数据迁移到新的平台上将会使在更广的时间范围内抽取和分析数据的生意成为可能,并且开启一种新的方法来利用ML技术,分析结果和对发现采取行动。