好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

q: 单机版的hive

在hadoop中自己写map/reduce代码,分析文本文件, 开发效率不高, 于是有了hive,只要定义好表结构,然后就可以直接用sql语句来分析文本文件,效率大大提升。

在单机linux上呢?如果要分析一个文本文件,通常是用awk,或者py/php? 开发速度也不快 于是就有了q,可以用sql语句来分析文本文件,表结构都不用提前定义的。

https://github测试数据/harelba/q

[大硬盘][zhaokunyao@ tpcc-mysql]$ ls -l >exampledatafile [大硬盘][zhaokunyao@ tpcc-mysql]$ cat exampledatafile 总用量 368 -rw-rw-r--. 1 zhaokunyao zhaokunyao 1621 8月 8 16:08 add_fkey_idx.sql -rw-rw-r--. 1 zhaokunyao zhaokunyao 317 8月 8 16:08 count.sql -rw-rw-r--. 1 zhaokunyao zhaokunyao 3105 8月 8 16:08 create_table.sql -rw-rw-r--. 1 zhaokunyao zhaokunyao 763 8月 8 16:08 drop_cons.sql -rw-rw-r--. 1 zhaokunyao zhaokunyao 0 8月 8 20:23 exampledatafile -rwxrwxr-x. 1 zhaokunyao zhaokunyao 477 8月 8 16:08 load.sh -rw-rw-r--. 1 zhaokunyao zhaokunyao 851 8月 8 16:08 README drwxrwxr-x. 2 zhaokunyao zhaokunyao 4096 8月 8 16:08 schema2 drwxrwxr-x. 5 zhaokunyao zhaokunyao 4096 8月 8 16:08 scripts drwxrwxr-x. 2 zhaokunyao zhaokunyao 4096 8月 8 18:23 src -rw-r--r--. 1 root root 359 8月 8 18:14 tpcc-data-binlog -rw-r--r--. 1 root root 364 8月 8 18:14 tpcc-data-nobinlog -rwxr-xr-x. 1 zhaokunyao zhaokunyao 468 8月 8 17:54 tpcc-graph-build.sh -rw-r--r--. 1 root root 723 8月 8 18:14 tpcc-graphic-data.txt -rw-r--r--. 1 root root 65213 8月 8 18:14 tpcc-graph.jpg -rwxrwxr-x. 1 zhaokunyao zhaokunyao 61239 8月 8 16:10 tpcc_load -rwxr-xr-x. 1 root root 366 8月 8 18:14 tpcc-output-analyze.sh -rw-rw-r--. 1 zhaokunyao zhaokunyao 14201 8月 8 17:46 tpcc-output-binlog -rw-rw-r--. 1 zhaokunyao zhaokunyao 14280 8月 8 16:58 tpcc-output-nobinlog -rwxrwxr-x. 1 zhaokunyao zhaokunyao 155950 8月 8 16:10 tpcc_start [大硬盘][zhaokunyao@ tpcc-mysql]$ [大硬盘][zhaokunyao@ tpcc-mysql]$ q -H1 "select count(1) from exampledatafile" 20 -H1表示跳过第一行内容。 还可以这样用: [大硬盘][zhaokunyao@ tpcc-mysql]$ cat exampledatafile | q -H1 "SELECT COUNT(*) FROM -" 20 按用户分组count: [大硬盘][zhaokunyao@ tpcc-mysql]$ q -H1 "SELECT c3,COUNT(1) FROM exampledatafile GROUP BY c3" root 5 zhaokunyao 15 [大硬盘][zhaokunyao@ tpcc-mysql]$ q -H1 "SELECT c3,COUNT(1) as huarong_count FROM exampledatafile GROUP BY c3 having huarong_count >10" zhaokunyao 15 分组统计文件大小总和 [大硬盘][zhaokunyao@ tpcc-mysql]$ q -H1 "SELECT c3, sum(c5)/1024 FROM exampledatafile GROUP BY c3" root 65 zhaokunyao 259 看起来靠谱,只是结果被取整了。 与浮点数操作,结果就会是浮点数: [大硬盘][zhaokunyao@ tpcc-mysql]$ q -H1 "SELECT c3, sum(c5)/1024.0 FROM exampledatafile GROUP BY c3" root 65.4541015625 zhaokunyao 259.3359375 格式化: [大硬盘][zhaokunyao@ tpcc-mysql]$ q -H1 -f "2=%4.2f" "SELECT c3, sum(c5)/1024.0 FROM exampledatafile GROUP BY c3" root 65.45 zhaokunyao 259.34 注: q支持的sql语法是sqlite的。 http://HdhCmsTestsqlite.org/lang.html 所以它是没有concat函数的.... [大硬盘][zhaokunyao@ tpcc-mysql]$ q -H1 "SELECT c3, (sum(c5)/1024 ) || 'kb' FROM exampledatafile GROUP BY c3" root 65kb zhaokunyao 259kb 此外还有一点要注意的: 所有的字段都被当作了TEXT,所以下面这样的操作是不靠谱的: [BIG HD][zhaokunyao@ tpcc-mysql]$ q -H1 "select c5 from exampledatafile where c5>1000" 1621 317 3105 763 477 851 4096 4096 4096 359 364 468 723 65213 61239 366 14201 14280 155950 要自己做个转化: [BIG HD][zhaokunyao@ tpcc-mysql]$ q -H1 "select c5 from exampledatafile where cast(c5 as int) >1000" 1621 3105 4096 4096 4096 65213 61239 14201 14280 155950  

另外还支持join操作…

其实还有一个明显的优点,就是q的操作结果可以使用管道和unix命令结合起来。自己发挥吧。  

查看更多关于q: 单机版的hive的详细内容...

  阅读:39次