In Java, executing Hive commands or HiveQL does not mean that Hive Client connects HiveServer (or HiveServer2) through JDBC to execute queries, instead, execute the Hive command on the server where HiveServer is deployed. Of course, this is a simple task. When we do simple data analysis experiments through Hive, we all directly go to Hive to execute HiveQL. Why should we execute HiveQL in the program?
This involves a problem. By entering Hive to execute HiveQL, we can only print the analysis results to the screen or save them to a temporary table. If we want to write the analysis results to a file, or what should we do if we want to further analyze the analysis results and use programs for analysis? This is why the Hive command is executed in Java.
After Java 1.5, ProcessBuilder is provided to start a command or application (Runtime before 1.5) in the Runtime environment based on the Runtime environment. For details about ProcessBuilder, see relevant Java documents. The call code is as follows:
String sql="show tables; select * from test_tb limit 10";List<String> command = new ArrayList<String>();command.add("hive");command.add("-e");command.add(sql);List<String> results = new ArrayList<String>();ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command);hiveProcess = hiveProcessBuilder.start();BufferedReader br = new BufferedReader(new InputStreamReader(hiveProcess.getInputStream()));String data = null;while ((data = br.readLine()) != null) {results.add(data);}
Command can be other Hive commands, not necessarily HiveQL.
This article is produced in personal work and study notes, reprinted please indicate the source http://blog.csdn.net/horace20