zoukankan      html  css  js  c++  java
  • Java 连接 Hive的样例程序及解析

    以后编程基于这个样例可节省查阅API的时间。

    private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

    try{

      Class.forName(driverName);//取得数据驱动,应用不同的数据驱动可以用不同的class路径。

    }catch (ClassNotFoundException e){

      e.printStackTrack();//optional

      System.exit(1);//optional

    }

    Connection con  = DriverManager.getConnection("jdbc:hive://localhost:10000/default","","");

    //DriverManager : The basic service for managing a set of JDBC drivers.

    //所用方法的详细说明如下:

    //在这里,subprotocol = "hive";

    Statement stmt = con.createStatement();

    //A Statement object represents a primitive statement in which a single method is applied to a target and a set of arguments .

    //可用于执行不带参数的简单 SQL 语句

    String tableName = "CustomerHiveTableJava";

    // Table to be created;

    ResultSet res = stmt.executeQuery("create table "+tableName+"(custid int, fname string,lname string) row format delimited fields terminated by ',' stored as textfile");

    //ResultSet A table of data representing a database result set, which is usually generated by executing a statement that queries the database.

    //show tables

    String st = "show tables" + tableName +"";

    System.out.println("Running :" + sql);

    res = stmt.executeQuery(sql);//res记录所有返回的记录

    if(res.next()){

      System.out.println(res.getString(1));

    }

    //Loading data into the table

    String filepath = "/path";

    sql = "load data local inpath"+filepath+"into table " + tableName;

    System.out.println("Running :"+ sql);

  • 相关阅读:
    01.网页学习阶段、整站分析、规划
    书签搬运
    如何判断两个链表相交及找到第一个相交点
    Windows平台使用git bash管理github中的工程
    二级指针的操作
    结构体的内存对齐
    大端和小端
    剑指Offer——面试题26:复杂链表的复制
    使用editcap命令将ERF格式转换为pcap格式
    如何在STL的map中使用结构体作为键值
  • 原文地址:https://www.cnblogs.com/xiamodeqiuqian/p/4887315.html
Copyright © 2011-2022 走看看