Phoenix 映射 HBase + Maven

声明

  • 本文基于 Centos6.x + CDH5.x

什么是Phoenix

Phoenix的团队用了一句话概括Phoenix:"We put the SQL back in NoSQL" 意思是:我们把SQL又放回NoSQL去了!这边说的NoSQL专指HBase,意思是可以用SQL语句来查询Hbase,你可能会说:“Hive和Impala也可以啊!”。但是Hive和Impala还可以查询文本文件,Phoenix的特点就是,它只能查Hbase,别的类型都不支持!但是也因为这种专一的态度,让Phoenix在Hbase上查询的性能超过了Hive和Impala!

安装Phoenix

之前的组件都是通过CDH来安装的,但是这回就跟Cloudera完全没关系了。从 Apache Phoenix Download 下载Phoenix包,不过这个镜像有点慢,我把包上传到CSDN了,下载地址见下面的版本对应 

Phoenix跟Hbase的版本对应

这里我用4.2.2,下载好后,解压开,把 phoenix-4.2.2-server.jar 拷贝到所有RegionServer的lib目录下   /usr/lib/hbase/lib
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. cp phoenix-4.2.2-server.jar /usr/lib/hbase/lib  


然后重启所有regionserver
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. service hbase-regionserver restart  


使用Phoenix

把 phoenix-4.2.2-bin.tar.gz 解压出来的 phoenix-4.2.2-bin 文件夹也上传到host1上,然后到bin目录下执行(其实在本机也可以,只是我本机没有Python环境而Centos天生有Python)
如果是windows下下载的,得先在centos上给bin文件夹里面的.py文件赋上执行权限
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@host1 ~]# cd phoenix-4.2.2-bin/  
  2. [root@host1 phoenix-4.2.2-bin]# cd bin  
  3. [root@host1 bin]# chmod +x *.py  

phoenix可以用4种方式调用
  • 批处理方式
  • 命令行方式
  • GUI方式
  • JDBC调用

批处理方式

我们建立sql 名叫 us_population.sql 内容是

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CREATE TABLE IF NOT EXISTS us_population (  state CHAR(2) NOT NULL,  city VARCHAR NOT NULL,  population BIGINT  CONSTRAINT my_pk PRIMARY KEY (state, city));  

建立一个文件 us_population.csv

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. NY,New York,8143197  
  2. CA,Los Angeles,3844829  
  3. IL,Chicago,2842518  
  4. TX,Houston,2016582  
  5. PA,Philadelphia,1463281  
  6. AZ,Phoenix,1461575  
  7. TX,San Antonio,1256509  
  8. CA,San Diego,1255540  
  9. TX,Dallas,1213825  
  10. CA,San Jose,912332  

再创建一个文件 us_population_queries.sql

[sql] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. SELECT state as "State",count(city) as "City Count",sum(population) as "Population Sum" FROM us_population GROUP BY state ORDER BY sum(population) DESC;   

然后一起执行

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. phoenix-4.2.2-bin/bin/psql.py host1,host2:2181 us_population.sql us_population.csv us_population_queries.sql  

这边记得把 host1 和 host2 换成你的zookeeper地址 

这条命令你同时做了 创建一个表,插入数据,查询结果 三件事情

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@host1 ~]# phoenix-4.2.2-bin/bin/psql.py host1,host2:2181 us_population.sql us_population.csv us_population_queries.sql  
  2. 15/03/04 17:14:23 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable  
  3. 15/03/04 17:14:24 WARN impl.MetricsConfig: Cannot locate configuration: tried hadoop-metrics2-phoenix.properties,hadoop-metrics2.properties  
  4. no rows upserted  
  5. Time: 0.726 sec(s)  
  6.   
  7. csv columns from database.  
  8. CSV Upsert complete. 10 rows upserted  
  9. Time: 0.103 sec(s)  
  10.   
  11. St                               City Count                           Population Sum   
  12. -- ---------------------------------------- ----------------------------------------   
  13. NY                                        1                                  8143197   
  14. CA                                        3                                  6012701   
  15. TX                                        3                                  4486916   
  16. IL                                        1                                  2842518   
  17. PA                                        1                                  1463281   
  18. AZ                                        1                                  1461575   
  19. Time: 0.048 sec(s)  

用hbase shell 看下会发现多出来一个 US_POPULATION 表,用scan 命令查看一下这个表的数据

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. hbase(main):002:0> scan 'US_POPULATION'  
  2. ROW                                              COLUMN+CELL                                                                                                                                     
  3.  AZPhoenix                                       column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00\x16MG                                                                  
  4.  AZPhoenix                                       column=0:_0, timestamp=1425460467206, value=                                                                                                    
  5.  CALos Angeles                                   column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00:\xAA\xDD                                                               
  6.  CALos Angeles                                   column=0:_0, timestamp=1425460467206, value=                                                                                                    
  7.  CASan Diego                                     column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00\x13(t                                                                  
  8.  CASan Diego                                     column=0:_0, timestamp=1425460467206, value=                                                                                                    
  9.  CASan Jose                                      column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00\x0D\xEB\xCC                                                            
  10.  CASan Jose                                      column=0:_0, timestamp=1425460467206, value=                                                                                                    
  11.  ILChicago                                       column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00+_\x96                                                                  
  12.  ILChicago                                       column=0:_0, timestamp=1425460467206, value=                                                                                                    
  13.  NYNew York                                      column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00|A]                                                                     
  14.  NYNew York                                      column=0:_0, timestamp=1425460467206, value=                                                                                                    
  15.  PAPhiladelphia                                  column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00\x16S\xF1                                                               
  16.  PAPhiladelphia                                  column=0:_0, timestamp=1425460467206, value=                                                                                                    
  17.  TXDallas                                        column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00\x12\x85\x81                                                            
  18.  TXDallas                                        column=0:_0, timestamp=1425460467206, value=                                                                                                    
  19.  TXHouston                                       column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00\x1E\xC5F                                                               
  20.  TXHouston                                       column=0:_0, timestamp=1425460467206, value=                                                                                                    
  21.  TXSan Antonio                                   column=0:POPULATION, timestamp=1425460467206, value=\x80\x00\x00\x00\x00\x13,=                                                                  
  22.  TXSan Antonio                                   column=0:_0, timestamp=1425460467206, value=                                                                                                    
  23. 10 row(s) in 0.2220 seconds  

会发现

  • 之前定义的PRIMARY KEY 为 state, city ,于是Phoenix就把你输入的state 和 city的值拼起来成为rowkey
  • 其他的字段还是按照列名去保存,默认的列簇为 0 
  • 还有一个0:_0 这个列是没有值的,这个是Phoenix处于性能方面考虑增加的一个列,不用管这个列

命令行方式

然后执行sqlline.py
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. $ ./sqlline.py localhost  
可以进入命令行模式
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 0: jdbc:phoenix:localhost>  
退出命令行的方式是执行 !quit
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 0: jdbc:phoenix:localhost>!quit  
命令开头需要一个感叹号,使用help可以打印出所有命令
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 0: jdbc:phoenix:localhost> help  
  2. !all                Execute the specified SQL against all the current connections  
  3. !autocommit         Set autocommit mode on or off  
  4. !batch              Start or execute a batch of statements  
  5. !brief              Set verbose mode off  
  6. !call               Execute a callable statement  
  7. !close              Close the current connection to the database  
  8. !closeall           Close all current open connections  
  9. !columns            List all the columns for the specified table  
  10. !commit             Commit the current transaction (if autocommit is off)  
  11. !connect            Open a new connection to the database.  
  12. !dbinfo             Give metadata information about the database  
  13. !describe           Describe a table  
  14. !dropall            Drop all tables in the current database  
  15. !exportedkeys       List all the exported keys for the specified table  
  16. !go                 Select the current connection  
  17. !help               Print a summary of command usage  
  18. !history            Display the command history  
  19. !importedkeys       List all the imported keys for the specified table  
  20. !indexes            List all the indexes for the specified table  
  21. !isolation          Set the transaction isolation for this connection  
  22. !list               List the current connections  
  23. !manual             Display the SQLLine manual  
  24. !metadata           Obtain metadata information  
  25. !nativesql          Show the native SQL for the specified statement  
  26. !outputformat       Set the output format for displaying results  
  27.                     (table,vertical,csv,tsv,xmlattrs,xmlelements)  
  28. !primarykeys        List all the primary keys for the specified table  
  29. !procedures         List all the procedures  
  30. !properties         Connect to the database specified in the properties file(s)  
  31. !quit               Exits the program  
  32. !reconnect          Reconnect to the database  
  33. !record             Record all output to the specified file  
  34. !rehash             Fetch table and column names for command completion  
  35. !rollback           Roll back the current transaction (if autocommit is off)  
  36. !run                Run a script from the specified file  
  37. !save               Save the current variabes and aliases  
  38. !scan               Scan for installed JDBC drivers  
  39. !script             Start saving a script to a file  
  40. !set                Set a sqlline variable  
  41. !sql                Execute a SQL command  
  42. !tables             List all the tables in the database  
  43. !typeinfo           Display the type map for the current connection  
  44. !verbose            Set verbose mode on  



建立employee的映射表

数据准备

然后我们来建立一个映射表,映射我之前建立过的一个hbase表 employee
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. hbase(main):003:0> describe 'employee'  
  2. DESCRIPTION                                                                                                                 ENABLED                                                              
  3.  'employee', {NAME => 'company', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => true                                                                 
  4.   '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', I                                                                      
  5.  N_MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME => 'family', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLIC                                                                      
  6.  ATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => '                                                                      
  7.  false', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}                                                                                                                       
  8. 1 row(s) in 0.1120 seconds  

可以看出employee有连个列簇 company 和 family

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. hbase(main):016:0> scan 'employee'  
  2. ROW                                              COLUMN+CELL                                                                                                                                     
  3.  row1                                            column=company:name, timestamp=1425537923391, value=ted                                                                                         
  4.  row1                                            column=company:position, timestamp=1425537950471, value=worker                                                                                  
  5.  row1                                            column=family:tel, timestamp=1425537956413, value=13600912345                                                                                            
  6.  row2                                            column=family:tel, timestamp=1425537994087, value=18942245698                                                                                            
  7.  row2                                            column=family:name, timestamp=1425537975610, value=michael                                                                                      
  8.  row2                                            column=family:position, timestamp=1425537985594, value=manager                                                                                  
  9. 2 row(s) in 0.0340 seconds  

有两条数据。如果没有这些数据的同学可以用以下命令创建
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. create 'employee','company','family'  
  2. put 'employee','row1','company:name','ted'  
  3. put 'employee','row1','company:position','worker'  
  4. put 'employee','row1','family:tel','13600912345'  
  5. put 'employee','row2','company:name','michael'  
  6. put 'employee','row2','company:position','manager'  
  7. put 'employee','row2','family:tel','1894225698'  
  8. scan 'employee'  



关于映射表

在建立映射表之前要说明的是,Phoenix是大小写敏感的,并且所有命令都是大写,如果你建的表名没有用双引号括起来,那么无论你输入的是大写还是小写,建立出来的表名都是大写的,如果你需要建立出同时包含大写和小写的表名和字段名,请把表名或者字段名用双引号括起来
你可以建立读写的表或者只读的表,他们的区别如下
  • 读写表:如果你定义的列簇不存在,会被自动建立出来,并且赋以空值
  • 只读表:你定义的列簇必须事先存在

建立映射

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 0: jdbc:phoenix:localhost> CREATE TABLE IF NOT EXISTS "employee" ("no" CHAR(4) NOT NULL PRIMARY KEY, "company"."name" VARCHAR(30),"company"."position" VARCHAR(20), "family"."tel" CHAR(11), "family"."age" INTEGER);  
  2. 2 rows affected (1.745 seconds)  

这行语句有几个注意点
  • IF NOT EXISTS可以保证如果已经有建立过这个表,配置不会被覆盖
  • 作为rowkey的字段用 PRIMARY KEY标定
  • 列簇用 columnFamily.columnName 来表示
  • family.age 是新增的字段,我之前建立测试数据的时候没有建立这个字段的原因是在hbase shell下无法直接写入数字型,等等我用UPSERT 命令插入数据的时候你就可以看到真正的数字型在hbase 下是如何显示的
建立好后,查询一下数据
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 0: jdbc:phoenix:localhost> SELECT * FROM "employee";  
  2. +------+--------------------------------+----------------------+-------------+------------------------------------------+  
  3. |  no  |              name              |       position       |     tel     |                   age                    |  
  4. +------+--------------------------------+----------------------+-------------+------------------------------------------+  
  5. | row1 | ted                            | worker               | 13600912345 | null                                     |  
  6. | row2 | michael                        | manager              | 1894225698  | null                                     |  
  7. +------+--------------------------------+----------------------+-------------+------------------------------------------+  

插入/更改数据

插入或者更改数据在Phoenix里面是一个命令叫 UPSERT 意思是 update + insert
我们插入一条数据试试
[sql] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UPSERT INTO "employee" VALUES ('row3','billy','worker','16974681345',33);  

查询一下数据
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 0: jdbc:phoenix:localhost> SELECT * FROM "employee";  
  2. +------+--------------------------------+----------------------+-------------+------------------------------------------+  
  3. |  no  |              name              |       position       |     tel     |                   age                    |  
  4. +------+--------------------------------+----------------------+-------------+------------------------------------------+  
  5. | row1 | ted                            | worker               | 13600912345 | null                                     |  
  6. | row2 | michael                        | manager              | 1894225698  | null                                     |  
  7. | row3 | billy                          | worker               | 16974681345 | 33                                       |  
  8. +------+--------------------------------+----------------------+-------------+------------------------------------------+  
  9. 3 rows selected (0.195 seconds)  

我们去hbase里面看一下数据
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. hbase(main):054:0> scan 'employee'  
  2. ROW                                              COLUMN+CELL                                                                                                                                     
  3.  row1                                            column=company:_0, timestamp=1425543735420, value=                                                                                              
  4.  row1                                            column=company:name, timestamp=1425543735274, value=ted                                                                                         
  5.  row1                                            column=company:position, timestamp=1425543735323, value=worker                                                                                  
  6.  row1                                            column=family:tel, timestamp=1425543735420, value=13600912345                                                                                   
  7.  row2                                            column=company:_0, timestamp=1425543735767, value=                                                                                              
  8.  row2                                            column=company:name, timestamp=1425543735608, value=michael                                                                                     
  9.  row2                                            column=company:position, timestamp=1425543735720, value=manager                                                                                 
  10.  row2                                            column=family:tel, timestamp=1425543735767, value=1894225698                                                                                    
  11.  row3                                            column=company:_0, timestamp=1425543857594, value=                                                                                              
  12.  row3                                            column=company:name, timestamp=1425543857594, value=billy                                                                                       
  13.  row3                                            column=company:position, timestamp=1425543857594, value=worker                                                                                  
  14.  row3                                            column=family:age, timestamp=1425543857594, value=\x80\x00\x00!                                                                                 
  15.  row3                                            column=family:tel, timestamp=1425543857594, value=16974681345                                                                                   
  16. 3 row(s) in 0.0650 seconds  

最后那个 \x80\x00\x00! 就是数字型在hbase中序列化成了字节的存储形式

TABLE 跟 VIEW 的区别

建立VIEW的语句跟TABLE一样,就是把TABLE关键字换成VIEW而已,但是他们有本质的区别:
  • 如果你有一张Hbase的表,并且以前已经有数据,最好建立VIEW,因为一旦建立了TABLE, Phoenix 会认为这张table完全是属于Phoenix的,就算你只是删除Phoenix里面的表,Hbase里面对应的表也会一起删除掉
  • 如果你之前没有这张Hbase的表,你想建立全新的Phoenix表,就用Table,但是要记住所有的操作都要经过Phoenix,那张同时被建立的表,只是一张附属表,不要试图往里面手动维护数据,忘记它的存在

用GUI方式

GUI方式的安装方法在 http://phoenix.apache.org/installation.html 这边不讲了,因为我自己也没搞起来,而且那个界面实在太丑了,看了不忍心使用。

JDBC调用

打开Eclipse建立一个简单的Maven项目 play-phoenix 

pom.xml的内容是
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>org.crazycake</groupId>  
  6.     <artifactId>play-phoenix</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>play-phoenix</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <repositories>  
  18.         <repository>  
  19.             <id>apache release</id>  
  20.             <url>https://repository.apache.org/content/repositories/releases/</url>  
  21.         </repository>  
  22.     </repositories>  
  23.   
  24.   
  25.     <build>  
  26.         <plugins>  
  27.             <plugin>  
  28.                 <artifactId>maven-compiler-plugin</artifactId>  
  29.                 <version>2.0.2</version>  
  30.                 <configuration>  
  31.                     <source>1.7</source>  
  32.                     <target>1.7</target>  
  33.                     <encoding>UTF-8</encoding>  
  34.                     <optimise>true</optimise>  
  35.                     <compilerArgument>-nowarn</compilerArgument>  
  36.                 </configuration>  
  37.             </plugin>  
  38.             <plugin>  
  39.                 <groupId>org.apache.maven.plugins</groupId>  
  40.                 <artifactId>maven-shade-plugin</artifactId>  
  41.                 <version>2.3</version>  
  42.                 <configuration>  
  43.                     <transformers>  
  44.                         <transformer  
  45.                             implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">  
  46.                         </transformer>  
  47.                     </transformers>  
  48.                 </configuration>  
  49.                 <executions>  
  50.                     <execution>  
  51.                         <phase>package</phase>  
  52.                         <goals>  
  53.                             <goal>shade</goal>  
  54.                         </goals>  
  55.                     </execution>  
  56.                 </executions>  
  57.             </plugin>  
  58.         </plugins>  
  59.     </build>  
  60.     <dependencies>  
  61.         <dependency>  
  62.             <groupId>junit</groupId>  
  63.             <artifactId>junit</artifactId>  
  64.             <version>3.8.1</version>  
  65.             <scope>test</scope>  
  66.         </dependency>  
  67.         <dependency>  
  68.             <groupId>org.apache.phoenix</groupId>  
  69.             <artifactId>phoenix-core</artifactId>  
  70.             <version>4.2.2</version>  
  71.         </dependency>  
  72.     </dependencies>  
  73. </project>  



phoenix 4.2.2 使用jdk1.7编译的,如果你只有1.6就用 4.2.0
[html] view plain copy
  1. <dependency>  
  2.     <groupId>org.apache.phoenix</groupId>  
  3.     <artifactId>phoenix-core</artifactId>  
  4.     <version>4.2.0</version>  
  5. </dependency>  



我们建立一个类 PhoenixManager
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package org.crazycake.play_phoenix;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8.   
  9. public class PhoenixManager {  
  10.     public static void main(String[] args) throws SQLException {  
  11.         Connection conn = null;  
  12.         Statement stat = null;  
  13.         ResultSet rs = null;  
  14.         try {  
  15.             Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");  
  16.             conn = DriverManager.getConnection("jdbc:phoenix:host1,host2:2181");  
  17.             stat = conn.createStatement();  
  18.             rs = stat.executeQuery("select * from \"employee\"");  
  19.             while(rs.next()){  
  20.                 System.out.println("no: " + rs.getString("no"));  
  21.                 System.out.println("name: " + rs.getString("name"));  
  22.                 System.out.println("position: " + rs.getString("position"));  
  23.                 System.out.println("age: " + rs.getInt("age"));  
  24.             }  
  25.         } catch (Throwable e) {  
  26.             e.printStackTrace();  
  27.         } finally{  
  28.             if(rs != null){  
  29.                 rs.close();  
  30.             }  
  31.             if(stat != null){  
  32.                 stat.close();  
  33.             }  
  34.             if(conn != null){  
  35.                 conn.close();  
  36.             }  
  37.         }  
  38.     }  
  39. }  


运行下,结果为
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. no: row1  
  2. name: ted  
  3. position: worker  
  4. age: 0  
  5. no: row2  
  6. name: michael  
  7. position: manager  
  8. age: 0  
  9. no: row3  
  10. name: billy  
  11. position: worker  
  12. age: 33  

搞定!

结语

至此所有Hadoop必学的组件已经完成,菜鸟课程已经完成!后续的非必学组件我就看心情更新了! :-)

参考资料

  • http://phoenix.apache.org/Phoenix-in-15-minutes-or-less.html



  • Phoenix中的语法

    Phoenix中的数据类型

    Phoenix中的方法

    我自己使用过程中一些简单语句,如下:

    1. select * from shuju;
    2. select count(1) from shuju;
    3. select cmtid,count(1) as num from shuju group by cmtid order by num desc;
    4. select avg(TO_NUMBER(avgt)) from shuju;
    5. select cmtid,count(1) as num,avg(TO_NUMBER(avgt)) as avgt,avg(TO_NUMBER(loss)) as loss from shuju group by cmtid order by num desc;
    6. select acm,dtype,cmtid,count(1) as num,avg(TO_NUMBER(avgt)) as avgt,avg(TO_NUMBER(loss)) as loss
    7. from shuju
    8. group by acm,dtype,cmtid
    9. order by num desc;
    10. select acm,dtype,porgcode,orgid,cmtid,count(1) as num,avg(TO_NUMBER(avgt)) as avgt,avg(TO_NUMBER(loss)) as loss
    11. from shuju
    12. group by acm,dtype,porgcode,orgid,cmtid
    13. order by num desc;
    14. where TO_DATE(ttime,'yyyyMMddHHmmss')=TO_DATE('20141125','yyyyMMdd')
    15. select ttime from shuju order by ttime desc;
    16.  
    17. where TO_DATE(ttime,'yyyyMMddHHmmss')=TO_DATE('20141125','yyyyMMdd')
    18.  
    19. select TO_DATE(ttime,'yyyyMMddHHmmss') from shuju;
    20. select TO_DATE('20141125','yyyyMMdd') from shuju;
    21. select (TO_DATE(ttime,'yyyyMMddHHmmss')=TO_DATE('20141125','yyyyMMdd')) as aaa from shuju order by aaa asc;










posted @ 2018-08-21 19:28  Jerry_Jin  阅读(1253)  评论(0编辑  收藏  举报