本文主要是介绍ODPS JDBC链接方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这里写目录标题
- MAVEN
- 正文
MAVEN
<dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-jdbc</artifactId><version>3.0.1</version></dependency>
正文
特别注意:
url
jdbc:odps:http://service.cn.maxcompute.aliyun.com/api
user ——> access_id;
这里不能再写user了,得写access_id不然系统不认
config.put("access_id", "LTAI*********SPbd");
password——>access_key;
这里不能再写password了,得写access_key不然系统不认
config.put("access_key", "AC5K*******************GdnVk");
database——>project_name
这里不能再写database了,得写project_name不然系统不认
config.put("project_name", "market_***_analyze");
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;/*** Created by zlh_fulin on 2018/5/14.*/
public class OdpsTest {private static String driverName = "com.aliyun.odps.jdbc.OdpsDriver";public static void main(String[] args) throws SQLException {try {Class.forName(driverName);} catch (ClassNotFoundException e) {e.printStackTrace();System.exit(1);}Properties config = new Properties();config.put("access_id", "LTAI*********SPbd");config.put("access_key", "AC5K*******************GdnVk");config.put("project_name", "market_***_analyze");Connection conn = DriverManager.getConnection("jdbc:odps:http://service.cn.maxcompute.aliyun.com/api", config);ResultSet rs;// 建表Statement stmt = conn.createStatement();String tableName = "market_repay_report";
// stmt.execute("drop table if exists " + tableName);
// stmt.execute("create table " + tableName + " (key int, value string)");// metadata
// DatabaseMetaData metaData = conn.getMetaData();
// System.out.println("product = " + metaData.getDatabaseProductName());
// System.out.println("jdbc version = " + metaData.getDriverMajorVersion() + ", "
// + metaData.getDriverMinorVersion());
// rs = metaData.getTables(null, null, tableName, null);
// while (rs.next()) {
// String name = rs.getString(3);
// System.out.println("inspecting table: " + name);
// ResultSet rs2 = metaData.getColumns(null, null, name, null);
// while (rs2.next()) {
// System.out.println(rs2.getString("COLUMN_NAME") + "\t"
// + rs2.getString("TYPE_NAME") + "(" + rs2.getInt("DATA_TYPE") + ")");
// }
// }String sql;// 插
// sql = String.format("insert into table %s select 24 key, ‘hours’ value from (select count(1) from %s) a", tableName, tableName);
// System.out.println("Running: " + sql);
// int count = stmt.executeUpdate(sql);
// System.out.println("updated records: " + count);//查sql = "select * from " + tableName + " where pt=to_char(dateadd(from_unixtime(unix_timestamp()), -1, 'dd'), 'yyyymmdd')";System.out.println("Running: " + sql);rs = stmt.executeQuery(sql);while (rs.next()) {System.out.println(String.valueOf(rs.getInt(1)) + "\t" + rs.getString(2));}if (rs !=null) rs.close();if (stmt != null) stmt.close();if (conn!=null) conn.close();}
}
这篇关于ODPS JDBC链接方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!