Hive之Java操作jdbc以及脚本实现

原博客地址:http://blog.csdn.net/evankaka

    摘要:本文主要讲了如何通过java来连接Hive,以及如何执行hive脚本

一、Hive连接

1.1、通过shell

1、Hive 命令行模式,直接输入#/hive/bin/hive的执行程序,或者输入#hive --service cli


2、 hive web界面的 (端口号9999) 启动方式
#hive --service hwi
用于通过浏览器来访问hive
http://hadoop0:9999/hwi/
3、 hive 远程服务 (端口号10000) 启动方式

#hive --service hiveserver

注意:hiveserver不能和hwi服务同时启动使用。

4、使用dbveare工具

需要将presto的jar添加进来并配置连接




1.2 通过java代码

使用Java代码来连接hive时,驱动可以选择使用jdbc,也可以选择使用presto

HiveServer使用thrift服务来为客户端提供远程连接的访问端口,在JDBC连接Hive之前必须先启动HiveServer。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. hive --service hiveserver    

hiveserver默认端口是10000,可以使用hive --service hiveserver -p 10002,更改默认启动端口,此端口也是JDBC连接端口。


1、直接通过jdbc

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.lin.bdp.common.utils;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.PreparedStatement;  
  7. import java.sql.ResultSet;  
  8. import java.sql.ResultSetMetaData;  
  9. import java.sql.SQLException;  
  10. import java.sql.Statement;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13.   
  14. import org.slf4j.Logger;  
  15. import org.slf4j.LoggerFactory;  
  16.   
  17. import com.lin.bdp.common.vo.Visitor;  
  18.   
  19. /** 
  20.  *  
  21.  * 功能概要:hive客户端工具 
  22.  *  
  23.  * @author linbingwen 
  24.  * @since  2016年10月20日 
  25.  */  
  26. public class HiveJdbcClient {  
  27.   
  28.     private static final Logger logger = LoggerFactory.getLogger(HiveJdbcClient.class);  
  29.   
  30.     public static final char UNDERLINE = '_';  
  31.   
  32.     private static String driverName;  
  33.     private static String url;  
  34.     private static String user;  
  35.     private static String password;  
  36.       
  37.   
  38.     private static class LazyHolder {  
  39.         private static final HiveJdbcClient INSTANCE = new HiveJdbcClient();  
  40.     }  
  41.       
  42.   
  43.     public static final HiveJdbcClient getInstance() {  
  44.         return LazyHolder.INSTANCE;  
  45.     }  
  46.   
  47.     /** 
  48.      * 初始化参数 
  49.      * @author linbingwen 
  50.      * @since  2016年10月20日 
  51.      */  
  52.     private void init() {  
  53.         driverName = ConfigLoader.getProperty("hive.jdbc.driverName");  
  54.         url = ConfigLoader.getProperty("hive.jdbc.url");  
  55.         user = ConfigLoader.getProperty("hive.jdbc.user");  
  56.         password = ConfigLoader.getProperty("hive.jdbc.password");  
  57.     }  
  58.       
  59.     private void initPresto() {  
  60.         driverName ="com.facebook.presto.jdbc.PrestoDriver";  
  61.         url = "jdbc:presto://10.78.104.5:8080/hive/ods_uba";  
  62.         user = "presto";  
  63.         password = "Admin@123";  
  64.     }  
  65.   
  66.     private HiveJdbcClient() {  
  67.         init();  
  68.     }  
  69.   
  70.     /** 
  71.      * 获取连接 
  72.      * @author linbingwen 
  73.      * @since  2016年10月20日  
  74.      * @return 
  75.      * @throws ClassNotFoundException 
  76.      * @throws SQLException 
  77.      */  
  78.     private Connection getConnection() throws ClassNotFoundException, SQLException {  
  79.         Class.forName(driverName);  
  80.         Connection conn = DriverManager.getConnection(url, user, password);  
  81.         return conn;  
  82.     }  
  83.   
  84.     /** 
  85.      * 按条件查找 
  86.      * @author linbingwen 
  87.      * @since  2016年10月20日  
  88.      * @param clazz 
  89.      * @param sql 
  90.      * @return 
  91.      * @throws Exception 
  92.      */  
  93.     public <T> List<T> find(Class<T> clazz, String sql) throws Exception {  
  94.         if (sql == null || sql.length() == 0) {  
  95.             logger.warn("查询sql语句不能为空");  
  96.             return new ArrayList<T>();  
  97.         }  
  98.   
  99.         Connection connection = null;  
  100.         PreparedStatement preState = null;  
  101.         ResultSet rs = null;  
  102.   
  103.         try {  
  104.             connection = getConnection();  
  105.               
  106.             Statement stmt = connection.createStatement();    
  107.             rs = stmt.executeQuery(sql);  
  108.             return (List<T>) handler(clazz, rs);  
  109.         } catch (Exception e) {  
  110.             logger.error("sql = {}执行出错,Exception = {}", sql, e.getLocalizedMessage());  
  111.             throw e;  
  112.         } finally {  
  113.             release(connection,preState,rs);  
  114.         }  
  115.     }  
  116.       
  117.     /** 
  118.      * 释放资源 
  119.      *  
  120.      * @author linbingwen 
  121.      * @since 2016年8月31日 
  122.      * @param conn 
  123.      * @param st 
  124.      * @param rs 
  125.      */  
  126.     private void release(Connection conn, Statement st, ResultSet rs) {  
  127.         if (rs != null) {  
  128.             try {  
  129.                 rs.close();  
  130.             } catch (Exception e) {  
  131.                 e.printStackTrace();  
  132.             }  
  133.             rs = null;  
  134.         }  
  135.         if (st != null) {  
  136.             try {  
  137.                 st.close();  
  138.             } catch (Exception e) {  
  139.                 e.printStackTrace();  
  140.             }  
  141.             st = null;  
  142.         }  
  143.         if (conn != null) {  
  144.             try {  
  145.                 conn.close();  
  146.             } catch (Exception e) {  
  147.                 e.printStackTrace();  
  148.             }  
  149.         }  
  150.     }  
  151.   
  152.     /** 
  153.      * 下划线字段转成陀峰字段 
  154.      * @author linbingwen 
  155.      * @since  2016年10月20日  
  156.      * @param param 
  157.      * @return 
  158.      */  
  159.     private String underlineToCamel(String param) {  
  160.         if (param == null || param.isEmpty()) {  
  161.             return null;  
  162.         }  
  163.         int len = param.length();  
  164.         StringBuilder sb = new StringBuilder(len);  
  165.         for (int i = 0; i < len; i++) {  
  166.             char c = param.charAt(i);  
  167.             if (c == UNDERLINE) {  
  168.                 if (++i < len) {  
  169.                     sb.append(Character.toUpperCase(param.charAt(i)));  
  170.                 }  
  171.             } else {  
  172.                 sb.append(c);  
  173.             }  
  174.         }  
  175.         return sb.toString();  
  176.     }  
  177.   
  178.     /** 
  179.      * 组装list对象 
  180.      * @author linbingwen 
  181.      * @since  2016年10月20日  
  182.      * @param clazz 
  183.      * @param rs 
  184.      * @return 
  185.      * @throws Exception 
  186.      */  
  187.     private Object handler(Class clazz, ResultSet rs) throws Exception {  
  188.         List list = new ArrayList();  
  189.         try {  
  190.             while (rs.next()) {  
  191.                 Object bean = clazz.newInstance();  
  192.   
  193.                 ResultSetMetaData meta = rs.getMetaData();  
  194.                 int count = meta.getColumnCount();  
  195.                 for (int i = 0; i < count; i++) {  
  196.                     String columnName = meta.getColumnName(i + 1);  
  197.                     String name = columnName;  
  198.                     if (columnName.contains(".")) {  
  199.                         String[] split = columnName.split("\\.");  
  200.                         if (split.length != 2) {  
  201.                             throw  new Exception("输入的表名不正确!");  
  202.                         }  
  203.                         name =split[1];  
  204.                     }  
  205.                       
  206.                     Object value = rs.getObject(columnName);  
  207.                     name = underlineToCamel(name.toLowerCase());  
  208.                     try {  
  209.                         Field f = bean.getClass().getDeclaredField(name);  
  210.                         if (f != null) {  
  211.                             f.setAccessible(true);  
  212.                             f.set(bean, value);  
  213.                         }  
  214.                     } catch (NoSuchFieldException e) {  
  215.                         logger.error("表中字段:{}在类:{}没有对应的属性", name, clazz);  
  216.                     } catch (IllegalArgumentException e) {  
  217.                         logger.error("表中字段:{}在类:{}对应属性类型不一到", name, clazz);  
  218.                     }  
  219.                 }  
  220.                 list.add(bean);  
  221.             }  
  222.         } catch (Exception e) {  
  223.             throw e;  
  224.         }  
  225.           
  226.         logger.info("hiveHandler successed the total size is:{}",list.size());  
  227.           
  228.         return list;  
  229.     }  
  230.   
  231.     @Override  
  232.     public String toString() {  
  233.         return "HiveJdbcClient driverName:" + driverName + " url:" + url + " user:" + user + " password:" + password;  
  234.     }  
  235.       
  236.     public List<Table> export() throws ClassNotFoundException, SQLException {  
  237.         String showtablesSQL = "show tables";  
  238.         List<Table> tableList = new ArrayList<Table>();  
  239.         List<String> tableNameList = new ArrayList<String>();  
  240.   
  241.         Connection conn = getConnection();  
  242.         Statement stmt = null;  
  243.         ResultSet tableRs = null// 存库元数据  
  244.         ResultSet colRs = null;//存储表元数据  
  245.         try {  
  246.             stmt = conn.createStatement();  
  247.             stmt.executeQuery("use ods_uba");  
  248.   
  249.             //获取表名  
  250.             tableRs = stmt.executeQuery(showtablesSQL);  
  251.             while (tableRs.next()) {  
  252.                 String table = tableRs.getString(1);  
  253.                 tableNameList.add(table);  
  254.             }  
  255.             //获取表结构  
  256.             com.lin.bdp.common.utils.Field field = null;  
  257.             Table table = null;  
  258.             for (int i = 0; i < tableNameList.size(); i++) {  
  259.                 String descTableSQL = "describe ";  
  260.                 List<com.lin.bdp.common.utils.Field> fieldList = new ArrayList<com.lin.bdp.common.utils.Field>();  
  261.                 descTableSQL = descTableSQL + tableNameList.get(i).trim();//拼接sql  
  262.                   
  263.                 colRs = stmt.executeQuery(descTableSQL);  
  264.                 while (colRs.next()) {  
  265.                     field = new com.lin.bdp.common.utils.Field();  
  266.                     field.setColumnName(colRs.getString(1));  
  267.                     field.setTypeName(colRs.getString(2));//测试大小  
  268.                     fieldList.add(field);  
  269.                 }  
  270.                 table = new Table();  
  271.                 table.setTableName(tableNameList.get(i).trim());  
  272.                 table.setField(fieldList);  
  273.                 System.out.println(table);  
  274.                 tableList.add(table);  
  275.             }  
  276.   
  277.         } catch (SQLException ex) {  
  278.         } finally {  
  279.             if (colRs != null) {  
  280.                 try {  
  281.                     colRs.close();  
  282.                 } catch (SQLException ex) {  
  283.                 }  
  284.             }  
  285.             if (tableRs != null) {  
  286.                 try {  
  287.                     tableRs.close();  
  288.                 } catch (SQLException ex) {  
  289.                 }  
  290.             }  
  291.             if (conn != null) {  
  292.                 try {  
  293.                     conn.close();  
  294.                 } catch (SQLException ex) {  
  295.                 }  
  296.             }  
  297.         }  
  298.         return tableList;  
  299.     }  
  300.   
  301.     public static void main(String[] args) {  
  302.         HiveJdbcClient hiveJdbcClient = HiveJdbcClient.getInstance();  
  303.         try {  
  304.             hiveJdbcClient.export();  
  305.               
  306.         } catch (Exception e) {  
  307.             e.printStackTrace();  
  308.         }  
  309.         System.exit(0);  
  310.     }  
  311.   
  312. }  
其中对应的配置文件:



对应的field.java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class Field {  
  2.   
  3.     private String columnName;  
  4.     private String typeName;  
  5.     private int columnSize;  
  6.     private int decimal_digits;  
  7.     private int nullable;  
  8.   
  9.     public Field() {  
  10.     }  
  11.   
  12.     public Field(String columnName, String typeName, int columnSize, int decimal_digits, int nullable) {  
  13.         this.columnName = columnName;  
  14.         this.typeName = typeName;  
  15.         this.columnSize = columnSize;  
  16.         this.decimal_digits = decimal_digits;  
  17.         this.nullable = nullable;  
  18.     }  
  19.   
  20.     public String getColumnName() {  
  21.         return columnName;  
  22.     }  
  23.   
  24.     public void setColumnName(String columnName) {  
  25.         this.columnName = columnName;  
  26.     }  
  27.   
  28.     public String getTypeName() {  
  29.         return typeName;  
  30.     }  
  31.   
  32.     public void setTypeName(String typeName) {  
  33.         this.typeName = typeName;  
  34.     }  
  35.   
  36.     public int getColumnSize() {  
  37.         return columnSize;  
  38.     }  
  39.   
  40.     public void setColumnSize(int columnSize) {  
  41.         this.columnSize = columnSize;  
  42.     }  
  43.   
  44.     public int getDecimal_digits() {  
  45.         return decimal_digits;  
  46.     }  
  47.   
  48.     public void setDecimal_digits(int decimal_digits) {  
  49.         this.decimal_digits = decimal_digits;  
  50.     }  
  51.   
  52.     public int getNullable() {  
  53.         return nullable;  
  54.     }  
  55.   
  56.     public void setNullable(int nullable) {  
  57.         this.nullable = nullable;  
  58.     }  
  59.   
  60.     @Override  
  61.     public String toString() {  
  62.         return "Field{" + "columnName=" + columnName + ", typeName=" + typeName + ", columnSize=" + columnSize + ", decimal_digits=" + decimal_digits + ", nullable=" + nullable + '}';  
  63.     }  
  64.   
  65. }  

对应的table.java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.lin.bdp.common.utils;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Table {  
  6.   
  7.     private String tableName;  
  8.     private List<Field> field;  
  9.   
  10.     public Table() {  
  11.     }  
  12.   
  13.     public Table(String tableName, List<Field> field) {  
  14.         this.tableName = tableName;  
  15.         this.field = field;  
  16.     }  
  17.   
  18.     public String getTableName() {  
  19.         return tableName;  
  20.     }  
  21.   
  22.     public void setTableName(String tableName) {  
  23.         this.tableName = tableName;  
  24.     }  
  25.   
  26.     public List<Field> getField() {  
  27.         return field;  
  28.     }  
  29.   
  30.     public void setField(List<Field> field) {  
  31.         this.field = field;  
  32.     }  
  33.   
  34.     @Override  
  35.     public String toString() {  
  36.         return "Table{" + "tableName=" + tableName + ", field=" + field + '}';  
  37.     }  
  38.   
  39. }  


2、直接通过presto

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.lin.bdp.common.utils;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.ResultSetMetaData;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9.   
  10. public class HivePrestoClient {  
  11.       
  12.     public static void main(String[] args) throws SQLException, ClassNotFoundException{    
  13.         //jdbc:presto://cdh1:8080/hive/sales  
  14.            Class.forName("com.facebook.presto.jdbc.PrestoDriver");  
  15.             Connection connection = DriverManager.getConnection("jdbc:presto://10.78.104.5:8080/hive/ods_uba","presto","Admin@123");    
  16.          //  connection.setCatalog("hive");    
  17.             Statement stmt = connection.createStatement();    
  18.            ResultSet rs = stmt.executeQuery("select * from ods_uba.visitor_lin limit 10");    
  19.           // ResultSet rs = stmt.executeQuery("select count(*) uuid,lst_visit_chnl lst_visit_chnl from ods_uba.visitor_lin group by lst_visit_chnl");    
  20.            while (rs.next()) {  
  21.                ResultSetMetaData meta = rs.getMetaData();  
  22.                 int count = meta.getColumnCount();  
  23.                 for (int i = 0; i < count; i++) {  
  24.                     String name = meta.getColumnName(i + 1);  
  25.                     Object value = rs.getObject(name);  
  26.                      System.out.println(name + ":" + value);  
  27.                 }  
  28.            }   
  29.             rs.close();    
  30.             connection.close();    
  31.             System.out.println("exit");  
  32.            // System.exit(0);  
  33.     }  
  34.   
  35. }  

在presto的安装目录下,etc/config.properties 包含 Presto Server 相关的配置,每一个 Presto Server 可以通时作为 coordinator 和 worker 使用。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. http-server.http.port=8080  
这个参数为端口的配置

3、用到的相关jar包

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.     <groupId>com.facebook.presto</groupId>  
  3.     <artifactId>presto-jdbc</artifactId>  
  4.     <version>0.75</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>org.apache.hadoop.hive</groupId>  
  8.     <artifactId>hive-jdbc</artifactId>  
  9.     <version>0.7.1-cdh3u6</version>  
  10. </dependency>  
  11. <dependency>  
  12.     <groupId>org.apache.hadoop.hive</groupId>  
  13.     <artifactId>hive-exec</artifactId>  
  14.     <version>0.7.1-cdh3u6</version>  
  15. </dependency>  
  16. <dependency>  
  17.     <groupId>org.apache.hadoop.hive</groupId>  
  18.     <artifactId>hive-metastore</artifactId>  
  19.     <version>0.7.1-cdh3u6</version>  
  20. </dependency>  
  21. <dependency>  
  22.     <groupId>org.apache.hadoop.hive</groupId>  
  23.     <artifactId>hive-service</artifactId>  
  24.     <version>0.7.1-cdh3u6</version>  
  25. </dependency>  

二、Hive脚本执行

Hive也可以直接执行sql脚本,或者通过shell脚本来调用sql脚本

ive脚本的执行方式大致有三种: 

1. hive控制台执行

比较简单,不再多说;

2. hive -e "SQL"执行

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. hive -e "use ods_uba;ALTER TABLE lin DROP PARTITION (opdt='2016-02-04');  

当然也可以将这一行代码放在一个shell脚本中来执行:

如下是删除表里一天分区的记录(分区以opdt = 'yyyy-mm-dd')形式存在

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/bin/sh  
  2. source ~/.bashrc  
  3.   
  4. #清数据  
  5. doClean(){  
  6.     partition=$(echo "$path/opdt=$statisDate")  
  7.     echo "hdfs dfs -rm -r -f $partition"  
  8.     hive -e "use ods_uba;ALTER TABLE $tableName DROP PARTITION (opdt='$statisDate');"  
  9.     hdfs dfs -rm -r -f $partition  
  10. }  
  11.   
  12. #统计日期默认取昨天  
  13. diffday=15  
  14. statisDate=`date +"%Y-%m-%d" -d "-"${diffday}"day"`  
  15. path=/hive/warehouse/ods_uba.db/$1  
  16. #首先判断是否有输入日期,以及输入日期是否合法,如果合法,取输入的日期。不合法报错,输入日期为空,取昨天  
  17. if [ $# -eq 0 ]; then  
  18.   echo "您没有输入参数,请至少输入表名kafka_appchnl_source_log/kafka_web_source_log"   
  19. elif [ $# -eq 1 ]; then  
  20.   echo "您输入的第一个参数为: $1 "  
  21.   tableName=$1  
  22.   if [[ "$tableName" == "kafka_appchnl_source_log" ]] || [[ "$tableName" == "kafka_web_source_log" ]]; then  
  23.      echo "要清除的表名是:$tableName,路径:$path,清除分区的日期:$statisDate"  
  24.      doClean $tableName $path $statisDate  
  25.   else   
  26.      echo "您输入的表名有错,请输入表名kafka_appchnl_source_log/kafka_web_source_log"  
  27.   fi  
  28. elif [ $# -eq 2 ]; then  
  29.   echo "您输入的第一个参数为: $1,第二个参数 :$2"   
  30.   tableName=$1  
  31.   statisDate=$2  
  32.   if [[ "$tableName" == "kafka_appchnl_source_log" ]] || [[ "$tableName" == "kafka_web_source_log" ]]; then  
  33.      echo "要清除的表名是:$tableName,路径:$path"  
  34.      if [ ${#statisDate} -eq 10 ];then  
  35.          echo "清除分区的日期:$statisDate"  
  36.          doClean $tableName $path $statisDate  
  37.      else  
  38.         echo "您输入日期不合法,请输入yyyy-mm-dd类型参数"  
  39.      fi  
  40.   else   
  41.     echo "您输入的表名有错,请输入表名kafka_appchnl_source_log/kafka_web_source_log"  
  42.   fi  
  43. else  
  44.   echo "您输入参数多于2个,请重新输入"  
  45. fi  

执行脚本:



3. hive -f SQL文件执行

如果sql都放在.sql文件中呢?那就用hive -f 

新建一个clean.sql内容如下:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. use ods_uba;  
  2. ALTER TABLE ${hiveconf:tableName} DROP PARTITION (partition_time='${hiveconf:partitionTime}');  


执行如下语句就可以删除一个分区

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. hive -hiveconf  tableName=lin  -hiveconf partitionTime='2016-01-23' -f clean.sql  

这里hiveconf可以用来传递参数

最后,也可以将shell脚本和sql脚本结合一起用。

新建一个如下doClean.sh

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/bin/sh  
  2. . ~/.bashrc  
  3.   
  4. #清数据,注意要传入一个yyyy-mm-dd类型参数  
  5. cleanData(){  
  6.   echo "您要清理数据的日期为:$statisDate"  
  7.   #生成每小时的查询条件  
  8.   for i in `seq 24`  
  9.   do  
  10.     num=$(echo $i)  
  11.     doClean $tableName $statisDate $num  
  12.   done  
  13. }  
  14.   
  15. #清数据  
  16. doClean(){  
  17.     echo "输入日期:$statisDate,输入序列号:$num"  
  18.     timeZone=$(printf "%02d\n" $(expr "$num" - "1"))   
  19.     partitionTime=$(echo "$statisDate-$timeZone")  
  20.     echo "清数据的表名:$tableName 分区字段:$partitionTime"  
  21.     hive -hiveconf tableName=$tableName  -hiveconf partitionTime=$partitionTime -f clean.sql  
  22. }  
  23.   
  24. #统计日期默认取昨天  
  25. diffday=1  
  26. statisDate=`date +"%Y-%m-%d" -d "-"${diffday}"day"`  
  27. #首先判断是否有输入日期,以及输入日期是否合法,如果合法,取输入的日期。不合法报错,输入日期为空,取昨天  
  28. if [ $# -eq 0 ]; then  
  29.   echo "您没有输入参数,请至少输入表名visit_log/evt_log/chnl_req"   
  30. elif [ $# -eq 1 ]; then  
  31.   echo "您输入的第一个参数为: $1 "  
  32.   tableName=$1  
  33.   if [[ "$tableName" == "visit_log" ]] || [[ "$tableName" == "evt_log" ]] || [[ "$tableName" == "chnl_req" ]]; then  
  34.      echo "要清除的表名是:$tableName,清除分区的日期:$statisDate"  
  35.      cleanData $tableName $statisDate   
  36.   else   
  37.     echo "您输入的表名有错,请输入表名visit_log/evt_log/chnl_req"  
  38.   fi  
  39. elif [ $# -eq 2 ]; then  
  40.   echo "您输入的第一个参数为: $1,第二个参数 :$2"   
  41.   tableName=$1  
  42.   statisDate=$2  
  43.   if [[ "$tableName" == "visit_log" ]] || [[ "$tableName" == "evt_log" ]] || [[ "$tableName" == "chnl_req" ]]; then  
  44.      echo "要清除的表名是:$tableName"  
  45.      if [ ${#statisDate} -eq 10 ];then  
  46.          echo "清除分区的日期:$statisDate"  
  47.          cleanData $tableName $statisDate   
  48.      else  
  49.         echo "您输入日期不合法,请输入yyyy-mm-dd类型参数"  
  50.      fi  
  51.   else   
  52.     echo "您输入的表名有错,请输入表名visit_log/evt_log/chnl_req"  
  53.   fi  
  54. else  
  55.   echo "您输入参数多于2个,请重新输入"  
  56. fi  

这里将一天分成了24个区,所以要删除一天的数据需要删除24个分区,分区以(yyyy-mm-dd-ss)

运行时使用:


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值