HIVE学习笔记:HiveServer2,调用HIVE的JavaAPI
2017-09-05 21:34
477 查看
首先修改配置文件hive-site.xml:
启动hiveserver2:
官方给出的示例代码:
官方给出的代码依赖包:
我是在windows下的eclipse中执行,所以所需依赖包如下图所示:
本地测试文本内容为:
程序输出为:
不知为毛程序无法识别文本内容,原因暂时不明。
<property> <name>hive.server2.thrift.bind.host</name> <value>192.168.200.100</value> <description>Bind host on which to run the HiveServer2 Thrift service.</description> </property> <property>
启动hiveserver2:
hive --service metastore & hiveserver2 &
官方给出的示例代码:
package HIVEDemo; import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveJdbcClient { private static String driverName = "org.apache.hive.jdbc.HiveDriver"; /** * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { try { Class.forName(driverName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(1); } //程序若在远程机器上执行则指定ip,本地执行可使用localhost,用户名与密码为Linux账户 Connection con = DriverManager.getConnection("jdbc:hive2://192.168.200.100:10000/default", "daya", "123456"); Statement stmt = con.createStatement(); //创建表 String tableName = "testHiveDriverTable"; stmt.execute("drop table if exists " + tableName); stmt.execute("create table " + tableName + " (key int, value string)"); // show tables String sql = "show tables '" + tableName + "'"; System.out.println("Running: " + sql); ResultSet res = stmt.executeQuery(sql); if (res.next()) { System.out.println(res.getString(1)); } // describe table sql = "describe " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } // load data into table // NOTE: filepath has to be local to the hive server // NOTE:本地文件每行应有两个条目 String filepath = "/home/daya/test/students.txt"; sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running: " + sql); stmt.execute(sql); // select * query sql = "select * from " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2)); } // regular hive query sql = "select count(1) from " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(res.getString(1)); } } }
官方给出的代码依赖包:
# To run the program using remote hiveserver in non-kerberos mode, we need the following jars in the classpath # from hive/build/dist/lib # hive-jdbc*.jar # hive-service*.jar # libfb303-0.9.0.jar # libthrift-0.9.0.jar # log4j-1.2.16.jar # slf4j-api-1.6.1.jar # slf4j-log4j12-1.6.1.jar # commons-logging-1.0.4.jar # # # To run the program using kerberos secure mode, we need the following jars in the classpath # hive-exec*.jar # commons-configuration-1.6.jar (This is not needed with Hadoop 2.6.x and later). # and from hadoop # hadoop-core*.jar (use hadoop-common*.jar for Hadoop 2.x) # # To run the program in embedded mode, we need the following additional jars in the classpath # from hive/build/dist/lib # hive-exec*.jar # hive-metastore*.jar # antlr-runtime-3.0.1.jar # derby.jar # jdo2-api-2.1.jar # jpox-core-1.2.2.jar # jpox-rdbms-1.2.2.jar # and from hadoop/build # hadoop-core*.jar # as well as hive/build/dist/conf, any HIVE_AUX_JARS_PATH set, # and hadoop jars necessary to run MR jobs (eg lzo codec)
我是在windows下的eclipse中执行,所以所需依赖包如下图所示:
本地测试文本内容为:
程序输出为:
不知为毛程序无法识别文本内容,原因暂时不明。
相关文章推荐
- Java学习笔记 Java调用Win32 API控制鼠标活动范围
- Hadoop学习笔记(十九)---Hive Java Api
- Java-API学习笔记---Object
- ArcGIS API For Flex对ImageServer发布的服务进行渲染及RasterFunction支持函数(学习笔记)
- Sun Directory Server/LDAP学习笔记(二)——API说明及代码样例
- {传智播客} (学习笔记)--Java调用存储过程实现Oracle数据库分页
- Java 学习笔记 (10) - Java 函数的递归调用
- Java学习笔记——子类调用父类属性方法
- Java菜鸟学习笔记--配置篇(三):Java API 文档下载与文档注释的使用
- 黑马程序员之asp.net学习笔记:巧用C#做中间语言 实现java调用.net
- .net调用java webservice基于JBOSS服务器 学习笔记(一)
- Java Sound API 学习笔记
- 云计算学习笔记---异常处理---hadoop问题处理ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: java.lang.NullPoin
- lua学习笔记 3 android调用Lua。Lua脚本使用LoadLib回调Java,并传多个参数
- JAVA Logging API 学习笔记
- JNI学习笔记:(1)开篇(2)本地代码访问Java代码 (3)本地方法取得Java属性/调用java方法 (4)本地代码创建Java对象(包括javaString) (5) 本地方法处理java数组
- java学习笔记---构造器的多态和调用顺序
- JavaServer Faces(JSF)学习笔记
- ant学习笔记(四)ant整合javadoc直接生成java文档api并将其打包之后上传到FTP服务器上面
- Java Sound API 学习笔记[转载]