oec数据挖掘用到的hbase
2016-03-29 10:09
204 查看
初始化数据配置参数接口;
public static DataSource createComboPooledDataSource(
HadJdbcConfig hadJdbcConfig) throws PropertyVetoException {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
log.info("JdbcDriverClass:" + hadJdbcConfig.getDriverClass());
log.info("JdbcUrl:" + hadJdbcConfig.getJdbcUrl());
// 配置数据源
cpds.setDriverClass(hadJdbcConfig.getDriverClass());
cpds.setJdbcUrl(hadJdbcConfig.getJdbcUrl());
cpds.setUser(hadJdbcConfig.getUsername());
cpds.setPassword(hadJdbcConfig.getPassword());
cpds.setAcquireRetryAttempts(hadJdbcConfig.getAcquireRetryAttempts());
cpds.setAcquireIncrement(hadJdbcConfig.getAcquireIncrement());
cpds.setAcquireRetryDelay(hadJdbcConfig.getAcquireRetryDelay());
cpds.setAutoCommitOnClose(hadJdbcConfig.getAutoCommitOnClose());
cpds.setMinPoolSize(hadJdbcConfig.getMiniPoolSize());
cpds.setMaxPoolSize(hadJdbcConfig.getMaxPoolSize());
cpds.setInitialPoolSize(hadJdbcConfig.getInitialPoolSize());
cpds.setIdleConnectionTestPeriod(hadJdbcConfig.getIdleConnectionTestPeriod());
cpds.setTestConnectionOnCheckin(hadJdbcConfig.getTestConnectionOnCheckin());
cpds.setBreakAfterAcquireFailure(hadJdbcConfig.getBreakAfterAcquireFailure());
cpds.setMaxIdleTime(hadJdbcConfig.getMaxIdleTime());
log.info("Initialize dbcp initialization success ....");
} catch (PropertyVetoException e) {
e.printStackTrace();
log.fatal("[-1000][数据库连接故障][" + ExceptionUtil.getTrace(e) + "]");
throw e;
}
return (DataSource) cpds;
}
hbase查询和插入
/**
* 全表扫描(方案二)
*
* @param tablename
* @return
*/
public String scaner(String tablename) {
String rowKey; // 行健
String qualifier; // 列
String value; // 值
StringBuffer sbf = new StringBuffer();
boolean flag = true;
SqlDao sqlDao = new SqlDao();
try {
HTable table = new HTable(conf, tablename);
Scan s = new Scan();
s.setMaxVersions(1);
ResultScanner rs = table.getScanner(s);
//遍历所有行
for (Result r : rs) {
// 取出属性名
if (flag) {
int counter=0;
for (Cell cell : r.rawCells()) {
String rowkey = new String(CellUtil.cloneRow(cell))+"\t";
String family = new String(CellUtil.cloneFamily(cell));
String ruleId = new String(CellUtil.cloneQualifier(cell));
if(counter++<1){
sbf.append("rowkey"+"\t");
}
if(!ruleId.equals("data")){
//测试,不对ruleId进行转换
//qualifier = family+":"+ruleId+"\t";
//弃用
//qualifier = family+":"+sqlDao.queryByRuleId(ruleId) + "\t";
qualifier = sqlDao.queryByRuleId(ruleId) + "\t";
}else{
//弃用
//qualifier = family+":"+ruleId+"\t";
qualifier = sqlDao.queryByColumnId(family)+"\t";
}
sbf.append(qualifier);
}
sbf.append("\r\n");
}
// 取出一行的所有列值
int counter = 0;
for (Cell cell : r.rawCells()) {
if(counter++<1){
sbf.append(new String(CellUtil.cloneRow(cell))+"\t");
}
sbf.append(new String(CellUtil.cloneValue(cell)) + "\t");
}
sbf.append("\r\n");
flag = false;
}
} catch (IOException e) {
e.printStackTrace();
}
return new String(sbf);
}
/**
* 插入一行数据到HBase,返回true表示插入成功,返回false表示插入失败
* @param tableName
* @param rowkey
* @param columnFamily
* @param column
* @param value
* @return
*/
public static boolean insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value){
HTable table;
try {
table = new HTable(conf, tableName);
Put put=new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);//放入表
table.close();//释放资源
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static DataSource createComboPooledDataSource(
HadJdbcConfig hadJdbcConfig) throws PropertyVetoException {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
log.info("JdbcDriverClass:" + hadJdbcConfig.getDriverClass());
log.info("JdbcUrl:" + hadJdbcConfig.getJdbcUrl());
// 配置数据源
cpds.setDriverClass(hadJdbcConfig.getDriverClass());
cpds.setJdbcUrl(hadJdbcConfig.getJdbcUrl());
cpds.setUser(hadJdbcConfig.getUsername());
cpds.setPassword(hadJdbcConfig.getPassword());
cpds.setAcquireRetryAttempts(hadJdbcConfig.getAcquireRetryAttempts());
cpds.setAcquireIncrement(hadJdbcConfig.getAcquireIncrement());
cpds.setAcquireRetryDelay(hadJdbcConfig.getAcquireRetryDelay());
cpds.setAutoCommitOnClose(hadJdbcConfig.getAutoCommitOnClose());
cpds.setMinPoolSize(hadJdbcConfig.getMiniPoolSize());
cpds.setMaxPoolSize(hadJdbcConfig.getMaxPoolSize());
cpds.setInitialPoolSize(hadJdbcConfig.getInitialPoolSize());
cpds.setIdleConnectionTestPeriod(hadJdbcConfig.getIdleConnectionTestPeriod());
cpds.setTestConnectionOnCheckin(hadJdbcConfig.getTestConnectionOnCheckin());
cpds.setBreakAfterAcquireFailure(hadJdbcConfig.getBreakAfterAcquireFailure());
cpds.setMaxIdleTime(hadJdbcConfig.getMaxIdleTime());
log.info("Initialize dbcp initialization success ....");
} catch (PropertyVetoException e) {
e.printStackTrace();
log.fatal("[-1000][数据库连接故障][" + ExceptionUtil.getTrace(e) + "]");
throw e;
}
return (DataSource) cpds;
}
hbase查询和插入
/**
* 全表扫描(方案二)
*
* @param tablename
* @return
*/
public String scaner(String tablename) {
String rowKey; // 行健
String qualifier; // 列
String value; // 值
StringBuffer sbf = new StringBuffer();
boolean flag = true;
SqlDao sqlDao = new SqlDao();
try {
HTable table = new HTable(conf, tablename);
Scan s = new Scan();
s.setMaxVersions(1);
ResultScanner rs = table.getScanner(s);
//遍历所有行
for (Result r : rs) {
// 取出属性名
if (flag) {
int counter=0;
for (Cell cell : r.rawCells()) {
String rowkey = new String(CellUtil.cloneRow(cell))+"\t";
String family = new String(CellUtil.cloneFamily(cell));
String ruleId = new String(CellUtil.cloneQualifier(cell));
if(counter++<1){
sbf.append("rowkey"+"\t");
}
if(!ruleId.equals("data")){
//测试,不对ruleId进行转换
//qualifier = family+":"+ruleId+"\t";
//弃用
//qualifier = family+":"+sqlDao.queryByRuleId(ruleId) + "\t";
qualifier = sqlDao.queryByRuleId(ruleId) + "\t";
}else{
//弃用
//qualifier = family+":"+ruleId+"\t";
qualifier = sqlDao.queryByColumnId(family)+"\t";
}
sbf.append(qualifier);
}
sbf.append("\r\n");
}
// 取出一行的所有列值
int counter = 0;
for (Cell cell : r.rawCells()) {
if(counter++<1){
sbf.append(new String(CellUtil.cloneRow(cell))+"\t");
}
sbf.append(new String(CellUtil.cloneValue(cell)) + "\t");
}
sbf.append("\r\n");
flag = false;
}
} catch (IOException e) {
e.printStackTrace();
}
return new String(sbf);
}
/**
* 插入一行数据到HBase,返回true表示插入成功,返回false表示插入失败
* @param tableName
* @param rowkey
* @param columnFamily
* @param column
* @param value
* @return
*/
public static boolean insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value){
HTable table;
try {
table = new HTable(conf, tableName);
Put put=new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);//放入表
table.close();//释放资源
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
相关文章推荐
- Maven实战 -- Architype模版
- MQ、JMS以及ActiveMQ 关系的理解
- 计算应用占用CPU的百分比及CPU的利用率
- Yii连接多个数据库的方法
- HashMap实现原理分析
- 天阴 零度 2.21日
- log4j日志系统的配置
- datepicker日期组件的添加
- 小雨 15℃ 3.3日
- struts2头像上传练习及其注意事项
- 测试POI的Excel导出导入
- 用户列表Excel导出实例(POI)
- 利用Excel导入用户列表(POI)
- 多云 8℃ 3.6日
- 小雨 0℃ 3.10日 (八级大风)
- 多云 9℃ 3.11日
- Filter简单过滤器的实现
- android初次学习(android studio安装及注意事项 1.5版本)
- 解决子框架嵌套的问题
- 阴天 6℃ 3.12日