文本挖掘 sql 个架构文件

Text Mining sql schema files

我收集了一大堆 sql 文件。在这些文件中,我只想保留 "CREATE TABLE" 和 "Alter table add constraint Foreign Key" 语句。我可以用它来挖掘这两个正则表达式吗?我知道我可以使用 grep 但我没有 linux

下面的代码将 return 你整行你可以根据你的要求做子字符串或其他事情。

 import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Test extends BufferedReader {
  String pattern;

  public Test(Reader in, String pattern) {
    super(in);
    this.pattern = pattern;
  }

  public final String readLine() throws IOException {
    String line;
    do {
      line = super.readLine();
    } while ((line != null) && line.indexOf(pattern) == -1);
    return line;
  }

  public static void main(String args[]) {
    try {
      Test in = new Test(new FileReader("test.txt"), "emement2");
      String line;
      while ((line = in.readLine()) != null)
        System.out.println(line);
      in.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}

您可以构建一个小的 Java 程序来只获取这样的句子。例如:

String input = new String(Files.readAllBytes(Paths.get("file.sql")), "UTF-8");
String regex = "(?i)((create table|alter table add constraint foreign key)[^;]+;)"
        .replace(" ", "\s+");
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println(matcher.group());
}