📄 miningsqlstream.java
字号:
}
// remarked by joyce
// 2004/01/14
ResultSet result = dataSource.executeQuery(sql);
// At least one row
if (result.next())
{
// Move to last row+1
result.last();
if (result.getRow() >= MiningInputStream.CATEGORICAL_ATTRIBUTE_BOUND)
{
catAtt.setUnboundedCategories(true);
} else
catAtt.setUnboundedCategories(false);
} else
catAtt.setUnboundedCategories(false);
result.close();
// Modify Frank Xu's approach to solve MySQL subquries
// problem.
//String sql = "select count(*) from (select
// distinct(["+name+"]) from " + rsmd.getTableName(1) +")";
// // remarked by joyce 2004/01/14
//String sql = "select count(*) from " +
// rsmd.getTableName(1) ; // added by joyce 2005/01/14
//ResultSet result = dataSource.executeQuery(sql);
//distinctCount = 0;
//result. next();
//distinctCount =
// Integer.parseInt(result.getObject(1).toString());
//if (distinctCount > m_BoundedThreshold)
// catAtt.setUnboundedCategories(true);
//else
// catAtt.setUnboundedCategories(false);
//Franky Chan, 16/02/2005>>
metaData.addMiningAttribute(catAtt);
}
}
this.cursorType = cursorType;
return metaData;
} catch (SQLException sqle)
{
throw new MiningException(
"Failed to create meta data from SQL request result");
}
}
// -----------------------------------------------------------------------
// Methods of cursor positioning
// -----------------------------------------------------------------------
/**
* Places the cursor before first row. This is done by closing and reopening
* the file reader.
*
* @throws MiningException
* cannot reset cursor
*/
public void reset() throws MiningException
{
try
{
if (rs == null || cursorType == ResultSet.TYPE_FORWARD_ONLY)
{
rs = dataSource.executeQuery(request);
if (metaData == null)
metaData = recognize();
} else
{
rs.beforeFirst();
}
cursorPosition = -1;
} catch (SQLException ex)
{
throw new MiningException("Can't reset cursor to the first row.");
}
}
/**
* Advance cursor by one position.
*
* @return true if next vector exists, else false
* @throws MiningException
* cannot advance cursor
*/
public boolean next() throws MiningException
{
boolean b = false;
try
{
b = rs.next();
cursorPosition++;
} catch (SQLException ex)
{
throw new MiningException(
"Can't move cursor to the next cursor position.");
}
return b;
}
/**
* Moves cursor to given position.
*
* @param position
* new position of the cursor
* @return true if cursor could be moved, else false
* @exception MiningException
* free cursor positioning not supported by JDBC driver
*/
public boolean move(int position) throws MiningException
{
boolean b = false;
try
{
b = rs.absolute(position);
cursorPosition = position;
} catch (SQLException ex)
{
throw new MiningException(
"Can't move cursor to this cursor position.");
}
return b;
}
// -----------------------------------------------------------------------
// Methods of reading from the stream
// -----------------------------------------------------------------------
/**
* Reads current data vector.
*
* @return data vector at current cursor position
* @throws MiningException
* cannot read data vector
*/
public MiningVector read() throws MiningException
{
MiningVector vector = null;
try
{
double[] values = new double[metaData.getAttributesNumber()];
for (int i = 0; i < values.length; i++)
{
// Get current value:
Object obj = rs.getObject(i + 1);
// Test for missing values:
boolean missing = false;
if (usedAsMissingValues != null && obj != null)
{
for (int j = 0; j < usedAsMissingValues.length; j++)
if (usedAsMissingValues[j].equals(obj))
{
missing = true;
break;
}
;
}
// Use object:
if (obj != null && !missing)
{
//<< added by Joyce to handle missing value - 2005/03/30
if (obj.toString().trim().length() <= 0){
values[i] = Category.MISSING_VALUE;
} //>>>
else{
MiningAttribute ma = metaData.getMiningAttribute(i);
// Categorical attribute:
if (ma instanceof CategoricalAttribute)
{
Category category = new Category(obj);
values[i] = ((CategoricalAttribute) ma)
.getKey(category);
// New category => add to header:
if (Category.isMissingValue(values[i]))
{
values[i] = ((CategoricalAttribute) ma)
.addCategory(category);
}
}
// Numeric attribute:
else
{
values[i] = Double.parseDouble(obj.toString());
}
;
}
} else
values[i] = Category.MISSING_VALUE;
}
;
// Create mining vector:
vector = new MiningVector(values);
vector.setMetaData(metaData);
} catch (SQLException ex)
{
ex.printStackTrace();
throw new MiningException("Can't read mining vector.");
}
return vector;
}
// -----------------------------------------------------------------------
// Test
// -----------------------------------------------------------------------
/**
* Test method.
*
* @param args
* arguments (ignored)
*/
public static void main(String[] args)
{
try
{
// Drivers:
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
// String driver = "oracle.jdbc.driver.OracleDriver";
// String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
// String driver = "net.avenir.jdbc3.Driver";
// URLs:
String url = "jdbc:microsoft:sqlserver://schostakovitch.prudsys.com:1433;databasename=test";
// String url =
// "jdbc:oracle:thin:system/manager@213.170.91.27:1521:test";
// String url = "jdbc:odbc:SAS Universal";
// String url = "jdbc:AvenirDriver://213.170.91.30:1433/z_form";
// Data sources:
MiningSqlSource dataSource = new MiningSqlSource(url, "test",
"tester", driver);
// MiningSqlSource dataSource = new MiningSqlSource( url, driver );
// Streams:
// MiningSqlStream s = new MiningSqlStream( dataSource, "select
// TRANSACTID, ITEMID, ITEMNAME from BASKET");
// MiningSqlStream s = new MiningSqlStream( dataSource, "SELECT *
// FROM prdsale");
MiningSqlStream s = new MiningSqlStream(dataSource,
"select * from dmc2000_class");
// Get physical model of database catalog:
s.findPhysicalModel();
org.omg.cwm.objectmodel.core.Package pkg = s.getPhysicalModel();
System.out.println("Physical model: " + pkg);
// Get logical model, i.e. the table:
System.out.println("metaData: " + s.getMetaData());
// Get table data:
while (s.next())
{
System.out.println(s.getCursorPosition() + ":" + s.read());
}
s.reset();
while (s.next())
{
System.out.println(s.getCursorPosition() + ":" + s.read());
}
s.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -