比较Java中的两个表
我必须比较两个表,并告诉用户两个表之间的区别 表1比较Java中的两个表,java,Java,我必须比较两个表,并告诉用户两个表之间的区别 表1 ------+--------- |Code | Label | ------+--------- |a1 | a1text | ------+--------- |b1 | b1text | ------+--------- |c1 | bartext1| ------+--------- |e1 | foo | -----+--------- 表2 ------+---
------+---------
|Code | Label |
------+---------
|a1 | a1text |
------+---------
|b1 | b1text |
------+---------
|c1 | bartext1|
------+---------
|e1 | foo |
-----+---------
表2
------+---------
|Code | Label |
------+---------
|a1 | a1text |
------+---------
|b1 | b2text |
------+---------
|d1 | bartext2|
------+---------
|f1 | bar |
------+---------
比较信息
如表1所示,代码c1的标签为bartext1,代码d1的标签为bartext2。
除最后一个字符外,它们是相同的。我必须在我的报告中写上它们除了最后一个字符外都是一样的。表中很少有行可能有额外的单词或特殊字符,并且其位置在任意位置。不知何故,我不得不在报告中指出两个标签是相同的,除了缺少单词或其中一个标签中有特殊字符外。代码在报告中并不重要
更多信息
此数据来自第三方。代码始终是唯一的,它们不是重复的代码。
两个代码可能具有相似的值
像
代码|标签
我有一个兄弟
我们有一个兄弟
预期产出应为
两个表中的标签不同。表1标签为:b1text,表2标签为:b2text。
两个表中的标签不同。表1标签为:bartext1,表2标签为:bartext2。
表2中缺少标签foo。
表1中缺少标签栏。
我将使用equals实现为这个表对象创建一个抽象,它将对客户端隐藏所有细节。Java是一种面向对象的语言,因此最好使用对象作为其存在的理由。我将使用equals实现为这个表对象创建一个抽象,它将对客户端隐藏所有细节。Java是一种面向对象的语言,因此最好将对象用于其存在的理由。有一个开源Java框架可以做到这一点:
www.diffkit.org有一个开源Java框架可以做到这一点:
www.diffkit.org这对我很有效,请随意添加盐来调味:
public final class ComparisonTest {
@Test
public void compare() throws Exception {
String url = "your.url";
String user = "your.user";
String password = "your.password";
// I am using Oracle here, but you can use any database
Connection connection = getConnection(url, user, password, OracleDriver.class);
ResultSet sourceResultSet = getResultSet(connection, "first_table");
ResultSet targetResultSet = getResultSet(connection, "second_table");
Map<Long, String> sourceIdHash = new HashMap<Long, String>();
Map<Long, String> targetIdHash = new HashMap<Long, String>();
try {
long rows = 0;
do {
if (sourceResultSet.next()) {
if (targetResultSet.next()) {
// Compare the lines
long sourceHash = hash(getRowValues(sourceResultSet, sourceResultSet.getMetaData()));
long targetHash = hash(getRowValues(targetResultSet, targetResultSet.getMetaData()));
sourceIdHash.put(sourceHash, sourceResultSet.getString(1));
targetIdHash.put(targetHash, targetResultSet.getString(1));
if (targetIdHash.containsKey(sourceHash)) {
targetIdHash.remove(sourceHash);
sourceIdHash.remove(sourceHash);
}
if (sourceIdHash.containsKey(targetHash)) {
sourceIdHash.remove(targetHash);
targetIdHash.remove(targetHash);
}
} else {
// Add the source row
long sourceHash = hash(getRowValues(sourceResultSet, sourceResultSet.getMetaData()));
sourceIdHash.put(sourceHash, sourceResultSet.getString(1));
}
} else {
if (targetResultSet.next()) {
// Add the target row
long targetHash = hash(getRowValues(targetResultSet, targetResultSet.getMetaData()));
targetIdHash.put(targetHash, targetResultSet.getString(1));
} else {
break;
}
}
if (rows++ % 10000 == 0) {
System.out.println("Rows : " + rows);
}
} while (true);
} finally {
closeAll(sourceResultSet);
closeAll(targetResultSet);
}
for (final Map.Entry<Long, String> mapEntry : sourceIdHash.entrySet()) {
if (targetIdHash.containsKey(mapEntry.getKey())) {
targetIdHash.remove(mapEntry.getKey());
continue;
}
System.out.println("Not in target : " + mapEntry.getValue());
}
for (final Map.Entry<Long, String> mapEntry : targetIdHash.entrySet()) {
if (sourceIdHash.containsKey(mapEntry.getKey())) {
sourceIdHash.remove(mapEntry.getKey());
continue;
}
System.out.println("Not in source : " + mapEntry.getValue());
}
System.out.println("In source and not target : " + sourceIdHash.size());
System.out.println("In target and not source : " + targetIdHash.size());
}
private ResultSet getResultSet(final Connection connection, final String tableName) {
String query = "select * from " + tableName + " order by pdb_key, organization_code, service_littera, day, resource_category";
return executeQuery(connection, query);
}
private Object[] getRowValues(final ResultSet resultSet, final ResultSetMetaData resultSetMetaData) throws SQLException {
List<Object> rowValues = new ArrayList<Object>();
for (int i = 2; i < resultSetMetaData.getColumnCount(); i++) {
rowValues.add(resultSet.getObject(i));
}
return rowValues.toArray(new Object[rowValues.size()]);
}
private final Connection getConnection(final String url, final String user, final String password, final Class<? extends Driver> driverClass) {
try {
DriverManager.registerDriver(driverClass.newInstance());
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private final ResultSet executeQuery(final Connection connection, final String query) {
try {
return connection.createStatement().executeQuery(query);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private final Long hash(final Object... objects) {
StringBuilder builder = new StringBuilder();
for (Object object : objects) {
builder.append(object);
}
return hash(builder.toString());
}
public Long hash(final String string) {
// Must be prime of course
long seed = 131; // 31 131 1313 13131 131313 etc..
long hash = 0;
char[] chars = string.toCharArray();
for (int i = 0; i < chars.length; i++) {
hash = (hash * seed) + chars[i];
}
return Long.valueOf(Math.abs(hash));
}
private void closeAll(final ResultSet resultSet) {
Statement statement = null;
Connection connection = null;
try {
if (resultSet != null) {
statement = resultSet.getStatement();
}
if (statement != null) {
connection = statement.getConnection();
}
} catch (Exception e) {
e.printStackTrace();
}
close(resultSet);
close(statement);
close(connection);
}
private void close(final Statement statement) {
if (statement == null) {
return;
}
try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void close(final Connection connection) {
if (connection == null) {
return;
}
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void close(final ResultSet resultSet) {
if (resultSet == null) {
return;
}
try {
resultSet.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}这对我很有效,请随意加盐调味:
public final class ComparisonTest {
@Test
public void compare() throws Exception {
String url = "your.url";
String user = "your.user";
String password = "your.password";
// I am using Oracle here, but you can use any database
Connection connection = getConnection(url, user, password, OracleDriver.class);
ResultSet sourceResultSet = getResultSet(connection, "first_table");
ResultSet targetResultSet = getResultSet(connection, "second_table");
Map<Long, String> sourceIdHash = new HashMap<Long, String>();
Map<Long, String> targetIdHash = new HashMap<Long, String>();
try {
long rows = 0;
do {
if (sourceResultSet.next()) {
if (targetResultSet.next()) {
// Compare the lines
long sourceHash = hash(getRowValues(sourceResultSet, sourceResultSet.getMetaData()));
long targetHash = hash(getRowValues(targetResultSet, targetResultSet.getMetaData()));
sourceIdHash.put(sourceHash, sourceResultSet.getString(1));
targetIdHash.put(targetHash, targetResultSet.getString(1));
if (targetIdHash.containsKey(sourceHash)) {
targetIdHash.remove(sourceHash);
sourceIdHash.remove(sourceHash);
}
if (sourceIdHash.containsKey(targetHash)) {
sourceIdHash.remove(targetHash);
targetIdHash.remove(targetHash);
}
} else {
// Add the source row
long sourceHash = hash(getRowValues(sourceResultSet, sourceResultSet.getMetaData()));
sourceIdHash.put(sourceHash, sourceResultSet.getString(1));
}
} else {
if (targetResultSet.next()) {
// Add the target row
long targetHash = hash(getRowValues(targetResultSet, targetResultSet.getMetaData()));
targetIdHash.put(targetHash, targetResultSet.getString(1));
} else {
break;
}
}
if (rows++ % 10000 == 0) {
System.out.println("Rows : " + rows);
}
} while (true);
} finally {
closeAll(sourceResultSet);
closeAll(targetResultSet);
}
for (final Map.Entry<Long, String> mapEntry : sourceIdHash.entrySet()) {
if (targetIdHash.containsKey(mapEntry.getKey())) {
targetIdHash.remove(mapEntry.getKey());
continue;
}
System.out.println("Not in target : " + mapEntry.getValue());
}
for (final Map.Entry<Long, String> mapEntry : targetIdHash.entrySet()) {
if (sourceIdHash.containsKey(mapEntry.getKey())) {
sourceIdHash.remove(mapEntry.getKey());
continue;
}
System.out.println("Not in source : " + mapEntry.getValue());
}
System.out.println("In source and not target : " + sourceIdHash.size());
System.out.println("In target and not source : " + targetIdHash.size());
}
private ResultSet getResultSet(final Connection connection, final String tableName) {
String query = "select * from " + tableName + " order by pdb_key, organization_code, service_littera, day, resource_category";
return executeQuery(connection, query);
}
private Object[] getRowValues(final ResultSet resultSet, final ResultSetMetaData resultSetMetaData) throws SQLException {
List<Object> rowValues = new ArrayList<Object>();
for (int i = 2; i < resultSetMetaData.getColumnCount(); i++) {
rowValues.add(resultSet.getObject(i));
}
return rowValues.toArray(new Object[rowValues.size()]);
}
private final Connection getConnection(final String url, final String user, final String password, final Class<? extends Driver> driverClass) {
try {
DriverManager.registerDriver(driverClass.newInstance());
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private final ResultSet executeQuery(final Connection connection, final String query) {
try {
return connection.createStatement().executeQuery(query);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private final Long hash(final Object... objects) {
StringBuilder builder = new StringBuilder();
for (Object object : objects) {
builder.append(object);
}
return hash(builder.toString());
}
public Long hash(final String string) {
// Must be prime of course
long seed = 131; // 31 131 1313 13131 131313 etc..
long hash = 0;
char[] chars = string.toCharArray();
for (int i = 0; i < chars.length; i++) {
hash = (hash * seed) + chars[i];
}
return Long.valueOf(Math.abs(hash));
}
private void closeAll(final ResultSet resultSet) {
Statement statement = null;
Connection connection = null;
try {
if (resultSet != null) {
statement = resultSet.getStatement();
}
if (statement != null) {
connection = statement.getConnection();
}
} catch (Exception e) {
e.printStackTrace();
}
close(resultSet);
close(statement);
close(connection);
}
private void close(final Statement statement) {
if (statement == null) {
return;
}
try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void close(final Connection connection) {
if (connection == null) {
return;
}
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void close(final ResultSet resultSet) {
if (resultSet == null) {
return;
}
try {
resultSet.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}您是否希望我们提供完整的单元测试源代码和安装脚本?表是否已经排序?允许重复代码吗?是否允许您使用库例程,如Google Collections、Jakarta Common Collections,或者这是家庭作业?表是否已排序?不允许重复代码?不使用库例程,如Google Collections、Jakarta Common Collections?是的,这是家庭作业吗?在你认为它们太不同而不能配对的情况下,两行之间没有什么区别吗?毕竟,BARTEX2和BAR共享一个共同的前缀,你认为它们相似到可以配对吗?另外,您不想输出这样一个事实,即尽管bartext1和bartext2相似,但它们的代码列c1和d1不同吗?我认为您的第一个任务是使您的规范更加清晰。您是否希望我们提供完整的单元测试源代码和安装脚本?表是否已经排序?允许重复代码吗?是否允许您使用库例程,如Google Collections、Jakarta Common Collections,或者这是家庭作业?表是否已排序?不允许重复代码?不使用库例程,如Google Collections、Jakarta Common Collections?是的,这是家庭作业吗?在你认为它们太不同而不能配对的情况下,两行之间没有什么区别吗?毕竟,BARTEX2和BAR共享一个共同的前缀,你认为它们相似到可以配对吗?另外,您不想输出这样一个事实,即尽管bartext1和bartext2相似,但它们的代码列c1和d1不同吗?我认为您的首要任务是使您的规范更加清晰。@duffymo一般来说很好,但equals应该给出true或false作为输出,而不是上面描述的输出。因此,对于这个特定的任务,无论如何都必须实现不同的比较算法。因此,如果需要返回原因码列表,请编写另一个方法。“从对象的角度思考”这一点仍然存在。@duffymo一般来说很好,但equals应该给出true或false作为输出,而不是上面描述的输出。因此,对于这个特定的任务,无论如何都必须实现不同的比较算法。因此,如果需要返回原因码列表,请编写另一个方法。关于对象的思考点仍然存在。