YDB系统JDBC接口访问工具

YDB系统JDBC接口访问工具

YDB:实时在线分析(OLAP)系统

YDB:实时在线分析(OLAP)系统:是我公司自主研发的一个大型分布式索引系统。旨在为数据总量为万亿级别、每天千亿级别数据增量的项目提供近似实时的数据导入,并提供近似实时响应的多维查询与统计服务。

JDBC接口

JDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语言中的接口和类,使用这些接口和类,Java客户端程序可以访问各种不同类型的数据库。比如建立数据库连接、执行SQL语句进行数据的存取操作。

使用方法

$ java -jar ysql.jar -c drop table test; create table test(user string, password string); desc test;
$ java -jar ysql.jar -f input
$ cat input
# 删除测试表格
drop table test; 
# 新建测试表格
create table test(user string, password string);
# 查询表格结构
desc test;

源代码

package cn.net.ycloud.ejdazhi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class YSql {

    public static void main(String[] args) {
        YSql ysql = new YSql();
        ysql.start(args);
    }

    public void printUsage() {
        System.out.println("usage:");
        System.out.println("    java -jar ysql.jar -c [;]...");
        System.out.println("    java -jar ysql.jar -f filename");
    }

    static Logger logger = LoggerFactory.getLogger(YSql.class);
    private List mSqls = new ArrayList<>();

    public void setSqls(String[] strings) {
        for (String string : strings) {
            if (string.trim().length() == 0) continue;
            mSqls.add(string);
            logger.info(string);
        }
    }

    public void start(String[] args) {
        try {
            parseParams(args);
            executeSql();
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
        }
    }

    private void parseParams(String[] args) throws IOException {
        String string = null;
        if (args.length >= 2) {
            if ("-c".equals(args[0])) {
                string = combineParams(args);
            } else if ("-f".equals(args[0])) {
                string = parseFile(args[1]);
            }
        } 
        if (null != string) setSqls(string.split(";"));
        else  printUsage();
    }

    private String parseFile(String filename) throws IOException {
        File file = new File(filename);
        BufferedReader br = new BufferedReader(new FileReader(filename));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.indexOf('#') == 0) continue;
            sb.append(line).append(" ");
        }
        br.close();
        return sb.toString();
    }

    // combine from 1 to n and add space
    private String combineParams(String[] strings) {
        StringBuilder sb = new StringBuilder();
        int n = strings.length;
        for (int i = 1; i < n; i++) {
            sb.append(strings[i]).append(" ");
        }
        return sb.toString();
    }

    private void executeSql() throws ClassNotFoundException, SQLException {
        if (mSqls.size() == 0) return;

        Class.forName("org.apache.hive.jdbc.HiveDriver");
        Connection conn = DriverManager.getConnection("jdbc:hive2://192.168.3.54:10000/default", "ycloud", "");
        Statement smst = conn.createStatement();

        for (String sql : mSqls) {
            ResultSet rs = smst.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                System.out.print(md.getColumnName(i));
                System.out.print("\t\t");
            }
            System.out.println();
            while (rs.next()) {
               for (int i = 1; i <= columns; i++) {
                    System.out.print(rs.getString(i));
                    System.out.print("\t\t");
                }
                System.out.println();
            }
            rs.close();
        }

        conn.close();
    }
}

你可能感兴趣的:(YDB系统JDBC接口访问工具)