windows下把資料從oracle匯入hbase
隨著資料量的增大,oracle的查詢速度就有點捉襟見肘,這個時候我們需要把資料從oracle資料庫匯入到hbase,然後使用hbase進行查詢資料。
廢話不多說,直接上程式碼.
public class Getdata {
private static Connection conn;
private static Statement st;
private ResultSet rs;
static {
conn = Getconn.getConntection();
try {
st = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Cfmx> getCfmxInfo(String tableName, int pagebegin, int pageEnd) {
String sql = "select id, ghdjid, xmmc, xmdm, je, sl, gg, cfrq, jx from (select rownum rn, id, ghdjid, xmmc, xmdm, je, sl, gg, cfrq, jx from "
+ tableName
+ " where rownum<"
+ pageEnd
+ ") s where s.rn >"
+ pagebegin;
System.out.println(sql);
List<Cfmx> list = new ArrayList<Cfmx>();
try {
rs = st.executeQuery(sql);
int count = 0;
while (rs.next()) {
Cfmx c = new Cfmx();
c.setId(String.valueOf(rs.getDouble(1)));
c.setGhdjid(rs.getString(2));
c.setXmmc(rs.getString(3));
c.setXmdm(rs.getString(4));
c.setJe(rs.getString(5));
c.setSl(rs.getString(6));
c.setGg(rs.getString(7));
c.setCfrq(rs.getString(8));
c.setJx(rs.getString(9));
// c.setJgdj(rs.getString(10));
// c.setJgid(rs.getString(11));
list.add(c);
count++;
}
System.out.println("共匯出" + count + "條資料");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
public class Getconn {
static String driver = "oracle.jdbc.driver.OracleDriver";
static String url = "jdbc:oracle:thin:@192.168.1.234:1521:orcl";
static String user = "LS";
static String password = "LS";
static Connection conn;
public static Connection getConntection()
{
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
System.out.println("connect database success");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
public class Cfmx {
private String id;
private String ghdjid;
private String xmmc;
private String xmdm;
private String je;
private String sl;
private String gg;
private String cfrq;
private String jx;
private String jgdj;
private String jgid;
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the xmmc
*/
public String getXmmc() {
return xmmc;
}
/**
* @param xmmc the xmmc to set
*/
public void setXmmc(String xmmc) {
this.xmmc = xmmc;
}
/**
* @return the xmdm
*/
public String getXmdm() {
return xmdm;
}
/**
* @param xmdm the xmdm to set
*/
public void setXmdm(String xmdm) {
this.xmdm = xmdm;
}
/**
* @return the je
*/
public String getJe() {
return je;
}
/**
* @param je the je to set
*/
public void setJe(String je) {
this.je = je;
}
/**
* @return the sl
*/
public String getSl() {
return sl;
}
/**
* @param sl the sl to set
*/
public void setSl(String sl) {
this.sl = sl;
}
/**
* @return the gg
*/
public String getGg() {
return gg;
}
/**
* @param gg the gg to set
*/
public void setGg(String gg) {
this.gg = gg;
}
/**
* @return the cfrq
*/
public String getCfrq() {
return cfrq;
}
/**
* @param cfrq the cfrq to set
*/
public void setCfrq(String cfrq) {
this.cfrq = cfrq;
}
/**
* @return the jx
*/
public String getJx() {
return jx;
}
/**
* @param jx the jx to set
*/
public void setJx(String jx) {
this.jx = jx;
}
/**
* @return the jgdj
*/
public String getJgdj() {
return jgdj;
}
/**
* @param jgdj the jgdj to set
*/
public void setJgdj(String jgdj) {
this.jgdj = jgdj;
}
/**
* @return the jgid
*/
public String getJgid() {
return jgid;
}
/**
* @param jgid the jgid to set
*/
public void setJgid(String jgid) {
this.jgid = jgid;
}
/**
* @return the ghdjid
*/
public String getGhdjid() {
return ghdjid;
}
/**
* @param ghdjid the ghdjid to set
*/
public void setGhdjid(String ghdjid) {
this.ghdjid = ghdjid;
}
}
上面是實體以及連線oracle和獲取資料的程式碼,由於考慮到outofmemory,這裡我們就可以一次只讀取20000行記錄來解決。
下面附上連線hbase的程式碼
public class HbaseConfigNew {
private static Configuration config;
final private static String SOCKET_TIMEOUT = "ipc.socket.timeout";
final static int DEFAULT_SOCKET_TIMEOUT = 100000; // 100 seconds
public static Configuration getHBaseConfig(){
config=HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum","192.168.13.129");
config.set("hbase.zookeeper.property.clientPort", "2181");
config.set("hbase.master", "192.168.13.129:16010");
config.getInt(SOCKET_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);//設定超時時間
System.setProperty("hadoop.home.dir", "D:\\linux\\hadoop-2.6.4\\hadoop-2.6.4");
return config;
}
public static HTable getTable( Connection conn,String tableName,String familyName){
HTable hTable=null;
try {
conn = ConnectionFactory.createConnection(getHBaseConfig());
HBaseAdmin hBaseAdmin=(HBaseAdmin)conn.getAdmin();
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
desc.addFamily(new HColumnDescriptor(familyName));
if(!hBaseAdmin.tableExists(tableName))
{
System.out.println("已經存在表");
hBaseAdmin.createTable(desc);
}
hTable = (HTable) conn.getTable(TableName.valueOf(tableName));
System.out.println("獲取表成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hTable;
}
下面是匯入資料的程式碼
public static void importCfmx(int count, List<Cfmx> list, String tableName,
String familyname) {
HTable hTable = null;
Connection conn = null;
// int count = 0;
List<Put> puts = new ArrayList<Put>();
try {
hTable = HbaseConfigNew.getTable(conn, tableName, familyname);
for (Cfmx c : list) {
String rowKey = "row" + count;
Put putRow = new Put(rowKey.getBytes());
// Put putRow = new
// Put((String.valueOf(System.currentTimeMillis())).getBytes());
putRow.addColumn(familyname.getBytes(), "id".getBytes(),
StringUtils.isBlank(c.getId()) ? " ".getBytes() : c
.getId().getBytes());
putRow.addColumn(familyname.getBytes(), "ghdjid".getBytes(),
StringUtils.isBlank(c.getGhdjid()) ? " ".getBytes() : c
.getGhdjid().getBytes());
putRow.addColumn(familyname.getBytes(), "xmmc".getBytes(),
StringUtils.isBlank(c.getXmmc()) ? " ".getBytes() : c
.getXmmc().getBytes());
putRow.addColumn(familyname.getBytes(), "xmdm".getBytes(),
StringUtils.isBlank(c.getXmdm()) ? " ".getBytes() : c
.getXmdm().getBytes());
putRow.addColumn(familyname.getBytes(), "je".getBytes(),
StringUtils.isBlank(c.getJe()) ? " ".getBytes() : c
.getJe().getBytes());
putRow.addColumn(familyname.getBytes(), "sl".getBytes(),
StringUtils.isBlank(c.getSl()) ? " ".getBytes() : c
.getSl().getBytes());
putRow.addColumn(familyname.getBytes(), "gg".getBytes(),
StringUtils.isBlank(c.getGg()) ? " ".getBytes() : c
.getGg().getBytes());
putRow.addColumn(familyname.getBytes(), "cfrq".getBytes(),
StringUtils.isBlank(c.getCfrq()) ? " ".getBytes() : c
.getCfrq().getBytes());
putRow.addColumn(familyname.getBytes(), "jx".getBytes(),
StringUtils.isBlank(c.getJx()) ? " ".getBytes() : c
.getJx().getBytes());
// putRow.addColumn(familyname.getBytes(), "jgdj".getBytes(),
// StringUtils.isBlank(c.getJgdj()) ? " ".getBytes() : c
// .getJgdj().getBytes());
// putRow.addColumn(familyname.getBytes(), "jgid".getBytes(),
// StringUtils.isBlank(c.getJgid()) ? " ".getBytes() : c
// .getJgid().getBytes());
puts.add(putRow);
count++;
}
hTable.put(puts);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
HbaseConfigNew.closeAll(hTable, conn);
}
}
資料就這樣從oracle資料庫匯入到hbase了。
相關文章
- Windows 下 MySQL 資料匯入 RedisWindowsMySqlRedis
- 資料匯入終章:如何將HBase的資料匯入HDFS?
- Oracle 資料匯入匯出Oracle
- Oracle資料匯入匯出Oracle
- Oracle 資料匯入ExcelOracleExcel
- oracle資料匯出匯入(exp/imp)Oracle
- Access 匯入 oracle 資料庫Oracle資料庫
- 用Navicat把SQLServer資料匯入MySQLServerMySql
- Oracle資料泵匯出匯入(expdp/impdp)Oracle
- Oracle資料泵的匯入和匯出Oracle
- 【oracle 資料匯入匯出字元問題】Oracle字元
- Hive資料匯入HBase引起資料膨脹引發的思考Hive
- Oracle資料庫匯入匯出。imp匯入命令和exp匯出命令Oracle資料庫
- Oracle使用資料泵expdp,impdp進行資料匯出匯入Oracle
- 使用Oracle SQL Developer匯入Excel資料OracleSQLDeveloperExcel
- java 從EXCEL匯入到資料庫JavaExcel資料庫
- windows下Oracle資料庫完全刪除WindowsOracle資料庫
- Oracle 19.3資料庫impdp匯入view時hang住Oracle資料庫View
- 用php把資料匯出excelPHPExcel
- 把TXT文字匯入SQLServer 出錯:資料轉換失敗SQLServer
- 資料庫 MySQL 資料匯入匯出資料庫MySql
- GeoRapter工具將shapefile資料匯入到Oracle空間資料庫中APTOracle資料庫
- sqoop資料匯入匯出OOP
- 資料泵匯出匯入
- phpMyAdmin匯入/匯出資料PHP
- 從 Neo4j 匯入 Nebula Graph 實踐見 SPark 資料匯入原理Spark
- Oracle 11g impdp 先資料後索引匯入方法Oracle索引
- Oracle 28.6資料庫impdp匯入view時hang@11Oracle資料庫View
- Windows環境下,.lib匯入庫 詳解Windows
- 大文字資料,匯入匯出到資料庫資料庫
- MySQL入門--匯出和匯入資料MySql
- MATLAB匯入資料Matlab
- Solr8 從 MySQL8.0.20中 匯入資料SolrMySql
- 將資料匯入kudu表(建立臨時hive表,從hive匯入kudu)步驟Hive
- 大量表格資料(>10萬條)使用PLSQL快速匯入OracleSQLOracle
- 一條資料HBase之旅,簡明HBase入門教程開篇
- HBase BulkLoad批量寫入資料實戰
- 【STATS】Oracle匯入匯出優化器統計資訊Oracle優化
- Oracle用資料泵匯入資料包12899的錯誤碼解決方法Oracle