drill apache
当今的数据是动态的,并由应用程序驱动。 由诸如Web /社交/移动/ IOT等行业趋势驱动的新业务应用时代的增长正在生成具有新数据类型和新数据模型的数据集。 这些应用程序是迭代的,并且关联的数据模型通常是半结构化的,无模式的且不断发展的。 半结构化的元素可以是复杂的/嵌套的,并且无模式,它具有允许在每一行中更改字段的能力,并且可以不断发展以不断添加和删除字段以满足业务需求。 换句话说,现代数据集不仅涉及体积和速度,还涉及多样性和可变性。
Apache Drill是业界第一个针对Hadoop和NoSQL的无模式SQL引擎 ,允许业务用户使用熟悉SQL技能和BI工具以自助服务的方式本地查询动态数据集,例如JSON。 使用Apache Drill,只需几分钟就可以从任何类型的数据中获取见解,而传统方法则需要数周甚至数月的时间延迟。
让我用一个简单的例子演示一下。 该示例中使用的数据集来自Yelp签入数据集 。
Yelp签入数据集具有以下结构(此描述来自上述Yelp站点供参考):
check-in
{
'type': 'checkin',
'business_id': (encrypted business id),
'checkin_info': {
'0-0': (number of checkins from 00:00 to 01:00 on all Sundays),
'1-0': (number of checkins from 01:00 to 02:00 on all Sundays),
...
'14-4': (number of checkins from 14:00 to 15:00 on all Thursdays),
...
'23-6': (number of checkins from 23:00 to 00:00 on all Saturdays)
}, # if there was no checkin for a hour-day block it will not be in the dataset
}
值得重复以上# if there was no checkin for a hour-day block it will not be in the dataset
注释, # if there was no checkin for a hour-day block it will not be in the dataset
。 这意味着您在“checkin_info”
中看到的元素名称是预先未知的,并且对于每一行都可能有所不同。 这是高度动态数据的简单但非常强大的示例。 如果我们需要使用任何其他基于HadoopSQL进行分析,则需要首先以扁平的关系结构表示此数据集,这可能既昂贵又耗时。
但是使用Drill,这非常简单。 不需要ETL /展平。 让我们看看这是如何完成的。
第1步:首先将Drill下载到您的计算机上,并在几分钟内开始
- http://drill.apache.org/download/
- 焦油-xvf apache-drill-0.7.0.tar
请注意,Drill可以本地安装在桌面上(称为嵌入式模式)。 您不需要Hadoop 。
步骤2:使用Drill启动SQLLine,这是一个基于JDBC的预打包应用程序
- bin / sqlline -u jdbc:drill:zk = local
步骤3:开始使用SQL分析数据
首先,让我们看一下数据集:
0: jdbc:drill:zk=local> select * from dfs.`/users/nrentachintala/Downloads/yelp/yelp_academic_dataset_checkin.json` limit 2;
+--------------+------------+-------------+
| checkin_info | type | business_id |
+--------------+------------+-------------+
| {"3-4":1,"13-5":1,"6-6":1,"14-5":1,"14-6":1,"14-2":1,"14-3":1,"19-0":1,"11-5":1,"13-2":1,"11-6":2,"11-3":1,"12-6":1,"6-5":1,"5-5":1,"9-2":1,"9-5":1,"9-6":1,"5-2":1,"7-6":1,"7-5":1,"7-4":1,"17-5":1,"8-5":1,"10-2":1,"10-5":1,"10-6":1} | checkin | JwUE5GmEO-sH1FuwJgKBlQ |
| {"6-6":2,"6-5":1,"7-6":1,"7-5":1,"8-5":2,"10-5":1,"9-3":1,"12-5":1,"15-3":1,"15-5":1,"15-6":1,"16-3":1,"10-0":1,"15-4":1,"10-4":1,"8-2":1} | checkin | uGykseHzyS5xAMWoN6YUqA |
+--------------+------------+-------------+
请注意,Drill使您可以直接查询JSON文件中的数据。 您无需在Hive商店中定义任何前期架构定义。 另外,请注意,第一行和第二行的checkin_info列中的元素名称不同。
Drill提供了一个名为“ KVGen”(键值生成器)的功能,该功能在处理包含包含由动态和未知元素名称组成的任意映射的复杂数据(例如checkin_info
。 KVGen将动态映射转换为键值对数组,其中键代表动态元素名称。
让我们在checkin_info
元素上应用KVGen来生成键值对。
0: jdbc:drill:zk=local> select kvgen(checkin_info) checkins from dfs.`/users/nrentachintala/Downloads/yelp/yelp_academic_dataset_checkin.json` limit 2;
+------------+
| checkins |
+------------+
| [{"key":"3-4","value":1},{"key":"13-5","value":1},{"key":"6-6","value":1},{"key":"14-5","value":1},{"key":"14-6","value":1},{"key":"14-2","value":1},{"key":"14-3","value":1},{"key":"19-0","value":1},{"key":"11-5","value":1},{"key":"13-2","value":1},{"key":"11-6","value":2},{"key":"11-3","value":1},{"key":"12-6","value":1},{"key":"6-5","value":1},{"key":"5-5","value":1},{"key":"9-2","value":1},{"key":"9-5","value":1},{"key":"9-6","value":1},{"key":"5-2","value":1},{"key":"7-6","value":1},{"key":"7-5","value":1},{"key":"7-4","value":1},{"key":"17-5","value":1},{"key":"8-5","value":1},{"key":"10-2","value":1},{"key":"10-5","value":1},{"key":"10-6","value":1}] |
| [{"key":"6-6","value":2},{"key":"6-5","value":1},{"key":"7-6","value":1},{"key":"7-5","value":1},{"key":"8-5","value":2},{"key":"10-5","value":1},{"key":"9-3","value":1},{"key":"12-5","value":1},{"key":"15-3","value":1},{"key":"15-5","value":1},{"key":"15-6","value":1},{"key":"16-3","value":1},{"key":"10-0","value":1},{"key":"15-4","value":1},{"key":"10-4","value":1},{"key":"8-2","value":1}] |
+------------+
Drill提供了另一个功能,可对称为“ Flatten”的复杂数据进行操作,以将“ KVGen”产生的键-值对列表分成单独的行,以进一步在其上应用分析功能。
0: jdbc:drill:zk=local> select flatten(kvgen(checkin_info)) checkins from dfs.`/users/nrentachintala/Downloads/yelp/yelp_academic_dataset_checkin.json` limit 20;
+------------+
| checkins |
+------------+
| {"key":"3-4","value":1} |
| {"key":"13-5","value":1} |
| {"key":"6-6","value":1} |
| {"key":"14-5","value":1} |
| {"key":"14-6","value":1} |
| {"key":"14-2","value":1} |
| {"key":"14-3","value":1} |
| {"key":"19-0","value":1} |
| {"key":"11-5","value":1} |
| {"key":"13-2","value":1} |
| {"key":"11-6","value":2} |
| {"key":"11-3","value":1} |
| {"key":"12-6","value":1} |
| {"key":"6-5","value":1} |
| {"key":"5-5","value":1} |
| {"key":"9-2","value":1} |
| {"key":"9-5","value":1} |
| {"key":"9-6","value":1} |
| {"key":"5-2","value":1} |
| {"key":"7-6","value":1} |
+------------+
请注意,Drill使我们可以在数据集上即时应用KVGen和Flatten函数,而无需预先定义架构,也不需要持久化为中间格式。 它功能强大,可以节省数周甚至数月的延迟,然后才能从数据中获取价值。
现在,在扁平化数据的输出上,我们可以使用所有标准SQL功能,例如过滤器,聚合和排序。 让我们看一些例子。
获取记录在Yelp数据集中的签入总数
0: jdbc:drill:zk=local> select sum(checkintbl.checkins.`value`) as TotalCheckins from (
. . . . . . . . . . . > select flatten(kvgen(checkin_info)) checkins from dfs.`/users/nrentachintala/Downloads/yelp/yelp_academic_dataset_checkin.json` ) checkintbl
. . . . . . . . . . . > ;
+---------------+
| TotalCheckins |
+---------------+
| 4713811 |
+---------------+
获取专门针对周日午夜的入住次数
0: jdbc:drill:zk=local> select sum(checkintbl.checkins.`value`) as SundayMidnightCheckins from (
. . . . . . . . . . . > select flatten(kvgen(checkin_info)) checkins from dfs.`/users/nrentachintala/Downloads/yelp/yelp_academic_dataset_checkin.json` ) checkintbl where checkintbl.checkins.key='23-0';
+------------------------+
| SundayMidnightCheckins |
+------------------------+
| 8575 |
+------------------------+
获取一周中每天的签到次数
0: jdbc:drill:zk=local> select `right`(checkintbl.checkins.key,1) WeekDay,sum(checkintbl.checkins.`value`) TotalCheckins from (
. . . . . . . . . . . > select flatten(kvgen(checkin_info)) checkins from dfs.`/users/nrentachintala/Downloads/yelp/yelp_academic_dataset_checkin.json` ) checkintbl group by `right`(checkintbl.checkins.key,1) order by TotalCheckins;
+------------+---------------+
| WeekDay | TotalCheckins |
+------------+---------------+
| 1 | 545626 |
| 0 | 555038 |
| 2 | 555747 |
| 3 | 596296 |
| 6 | 735830 |
| 4 | 788073 |
| 5 | 937201 |
+------------+---------------+
获取一天中每小时的签入次数
0: jdbc:drill:zk=local> select substr(checkintbl.checkins.key,1,strpos(checkintbl.checkins.key,'-')-1) as HourOfTheDay ,sum(checkintbl.checkins.`value`) TotalCheckins from (
. . . . . . . . . . . > select flatten(kvgen(checkin_info)) checkins from dfs.`/users/nrentachintala/Downloads/yelp/yelp_academic_dataset_checkin.json` ) checkintbl group by substr(checkintbl.checkins.key,1,strpos(checkintbl.checkins.key,'-')-1) order by TotalCheckins;
+--------------+---------------+
| HourOfTheDay | TotalCheckins |
+--------------+---------------+
| 3 | 20357 |
| 4 | 21076 |
| 2 | 28116 |
| 5 | 33842 |
| 1 | 45467 |
| 6 | 54174 |
| 0 | 74127 |
| 7 | 96329 |
| 23 | 102009 |
| 8 | 130091 |
| 22 | 140338 |
| 9 | 162913 |
| 21 | 211949 |
| 10 | 220687 |
| 15 | 261384 |
| 14 | 276188 |
| 16 | 292547 |
| 20 | 293783 |
| 13 | 328373 |
| 11 | 338675 |
| 17 | 374186 |
| 19 | 385381 |
| 12 | 399797 |
| 18 | 422022 |
+--------------+---------------+
从上面可以看到,Drill使得浏览结构化和半结构化数据变得很容易,而无需任何前期模式管理或ETL。
有关Drill如何与JSON和其他复杂数据格式一起使用的更详细的教程,请阅读使用Apache Drill分析Yelp JSON数据: https : //cwiki.apache.org/confluence/display/DRILL/Analyzing+Yelp+JSON+Data+使用+ Apache + Drill
要了解有关Drill的更多信息,请参考以下资源:
- 在此处下载Drill: http : //drill.apache.org/download/
- 我们认为Drill很棒的10个理由 : 使用Apache Drill的10大理由–现在已成为包括Hadoop在内的MapR发行版的一部分
- 一个简单的10分钟教程: https : //cwiki.apache.org/confluence/display/DRILL/Apache+Drill+in+10+Minutes
- 更全面的Hadoop教程: https : //cwiki.apache.org/confluence/display/DRILL/Apache+Drill+Tutorial
翻译自: https://www.javacodegeeks.com/2015/02/analyze-highly-dynamic-datasets-apache-drill.html
drill apache