[工具类] Hive SQL
2015-11-24 16:52
369 查看
package com.claw.util.hive; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Hql { public static String exec(String sql) throws Exception{ System.out.println("start sql ....."); String result = "0"; List<String> command = new ArrayList<String>(); command.add("hive"); command.add("-e"); command.add(sql); System.out.println(sql); ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command); Process hiveProcess = hiveProcessBuilder.start(); BufferedReader br = new BufferedReader(new InputStreamReader(hiveProcess.getInputStream())); System.out.println("hsql run ......"); String data = null; if((data = br.readLine()) != null){ result = data; } return result; } public static List<String> sqlList(String sql) throws Exception{ System.out.println("start sql list ....."); List<String> list = new ArrayList<String>(); List<String> command = new ArrayList<String>(); command.add("hive"); command.add("-e"); command.add(sql); System.out.println(sql); ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command); Process hiveProcess = hiveProcessBuilder.start(); BufferedReader br = new BufferedReader(new InputStreamReader(hiveProcess.getInputStream())); System.out.println("hsql run ......"); String data = null; while((data = br.readLine()) != null){ list.add(data); } return list; } public static void t() throws Exception{ String sql="select count(distinct(cookie)) from logs where eventdate=20150724"; List<String> command = new ArrayList<String>(); command.add("hive"); command.add("-e"); command.add(sql); ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command); Process hiveProcess = hiveProcessBuilder.start(); BufferedReader br = new BufferedReader(new InputStreamReader(hiveProcess.getInputStream())); String data = null; System.out.println("--------------------"); if ((data = br.readLine()) != null) { System.out.println(data); } } public static void main(String[] args) { try { t(); } catch (Exception e) { // TODO: handle exception } } }
相关文章推荐
- Oracle中的锁概述
- SQL语言分类DDL/DML/DCL
- oracle 添加表分区
- oracle blog
- memcached安装
- oracle数据字典详解
- oracle启动和关闭数据库
- 数据库 笔记
- 解决误删datafile导致数据库无法启动的问题
- Oracle 统计信息(备份/导入)表分析-[注意要刷缓存]
- mysql 5.7 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executi
- 快速导入数据库命令
- Win10手记-为应用集成SQLite(一)
- oracle 10g中的几个概念(表空间,数据库名,实例名,操作系统环境变量SID)
- logstash无法读取redis数据
- 关于weka连接mysql的一些问题
- SQL Server常用查询
- centos 安装 redis
- Oracle位图索引与B-Tree索引的优缺点
- Mysql存储引擎