1
|
package eu.dnetlib.data.mapreduce.hbase.statsExport.drivers;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
|
5
|
import eu.dnetlib.data.mapreduce.hbase.statsExport.daos.SqlDAO;
|
6
|
import eu.dnetlib.data.mapreduce.hbase.statsExport.daos.SqlStore;
|
7
|
import eu.dnetlib.data.mapreduce.hbase.statsExport.utils.ContextExporter;
|
8
|
|
9
|
public class DBDriver {
|
10
|
|
11
|
private Logger log = Logger.getLogger(this.getClass());
|
12
|
|
13
|
private SqlDAO statsDao;
|
14
|
private SqlStore statsStore;
|
15
|
private ContextExporter contextExporter;
|
16
|
|
17
|
/**
|
18
|
* Prepares shadow schema in the database that will be used to import data
|
19
|
*
|
20
|
* @throws Exception
|
21
|
*/
|
22
|
|
23
|
public DBDriver(String dbUrl, String dbUser, String dbPassword, String dbDriver) {
|
24
|
|
25
|
statsStore = new SqlStore(dbUrl, dbUser, dbPassword, dbDriver);
|
26
|
statsDao = new SqlDAO();
|
27
|
statsDao.setStore(statsStore);
|
28
|
|
29
|
}
|
30
|
|
31
|
public DBDriver() {
|
32
|
}
|
33
|
|
34
|
public void prepareDB(String user) throws Exception {
|
35
|
try {
|
36
|
|
37
|
statsDao.setSearchPathDB(user);
|
38
|
statsDao.createSchema();
|
39
|
|
40
|
} catch (Exception e) {
|
41
|
log.error("Error Preparing Schema for DB import. ", e);
|
42
|
throw new Exception("Error Preparing Schema for DB import.", e);
|
43
|
}
|
44
|
}
|
45
|
|
46
|
public void finalizedDB(String contextMap, String outputPath) throws Exception {
|
47
|
|
48
|
try {
|
49
|
|
50
|
contextExporter = new ContextExporter(outputPath, contextMap, false);
|
51
|
contextExporter.exportToFile( );
|
52
|
statsDao.insertContexts(outputPath);
|
53
|
statsDao.buildViews();
|
54
|
statsDao.executeExtraInserts();
|
55
|
statsDao.buildIndexes();
|
56
|
|
57
|
} catch (Exception e) {
|
58
|
log.error("Error while finalizing SQL DB", e);
|
59
|
throw new Exception("Error while finalizing SQL DB", e);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
public void renameSchema() throws Exception {
|
64
|
|
65
|
try {
|
66
|
statsDao.renameSchema();
|
67
|
|
68
|
} catch (Exception e) {
|
69
|
log.error("Error while renaming database schema", e);
|
70
|
throw new Exception("Error while renaming database schema", e);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
public Logger getLog() {
|
75
|
return log;
|
76
|
}
|
77
|
|
78
|
public void setLog(Logger log) {
|
79
|
this.log = log;
|
80
|
}
|
81
|
|
82
|
public SqlDAO getStatsDao() {
|
83
|
return statsDao;
|
84
|
}
|
85
|
|
86
|
public void setStatsDao(SqlDAO statsDao) {
|
87
|
this.statsDao = statsDao;
|
88
|
}
|
89
|
|
90
|
}
|