SciDB一个典型的Array Database,官网地址:http://www.paradigm4.com/,提供了C++,Java,Python和R的接口。
下面说说如何使用Java的接口(JDBC)读取SciDB数据: 我使用的SciDB版本是14.12,JDK版本为1.7。 依赖的JDBC的jar包可从https://downloads.paradigm4.com/client/14.12/jdbc/下载,主要用到的jar包括scidb4j.jar和iquery.jar。 此外,还需要Google的protolib库,版本为2.4.1。
代码如下:
package cn.tzy.scidb;
import org.scidb.jdbc.IResultSetWrapper;
import java.sql.*;
public class Main {
public static void main(String[] args) {
try {
Class.forName("org.scidb.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.out.println("Driver is not in the CLASSPATH -> " + ex);
}
String iqueryHost = "127.0.0.1";
String iqueryPort = "1239";
String arrayName = "landsat_wuhan";
try {
String url = "jdbc:scidb://" + iqueryHost + ":" + iqueryPort + "/";
Connection conn = DriverManager.getConnection(url);
ResultSet res;
try (Statement st = conn.createStatement()) {
res = st.executeQuery("select * from " + arrayName);
}
ResultSetMetaData meta = res.getMetaData();
System.out.println("Source array name: " + meta.getTableName(0));
System.out.println(meta.getColumnCount() + " columns:");
IResultSetWrapper resWrapper = res.unwrap(IResultSetWrapper.class);
// 输出Array的元数据
for(int i = 1; i <= meta.getColumnCount(); ++i) {
System.out.println(meta.getColumnName(i) + " - " + meta.getColumnTypeName(i) + " - is attribute:" + resWrapper.isColumnAttribute(i));
}
int count = 0;
// 输出Array的前10行数据
while(!res.isAfterLast() && count < 10) {
System.out.println(res.getLong(1) + " " + res.getLong(2) + " " + res.getShort(3));
res.next();
count++;
}
} catch (SQLException ex) {
System.out.println(ex);
}
}
}
输出结果如下:
Source array name: landsat_wuhan@1
3 columns:
y - int64 - is attribute:false
x - int64 - is attribute:false
band1 - uint8 - is attribute:true
0 0 0
0 1 0
0 2 0
0 3 0
0 4 0
0 5 0
0 6 0
0 7 0
0 8 0
0 9 0