BCP 实用程序无法使用 JAVA 在 Linux 中导出数据:

BCP Utility Unable to Export Data in Linux Using JAVA:

我尝试执行以下代码导出数据(myFileName.csv) :

bcp "select * from DataBase.schema.TABLE_NAME" queryout tableData.csv -c -t, , -S [server] -U [user] -P '[password(with special characters)]' > LogFile.txt 

以上代码在终端中工作正常。

相比之下,我使用 java 尝试了同一段代码。

File dir = new File("Mydirectory");
    Path dataPath = Paths.get("tableData.csv");
    List<String> val = new ArrayList();
    val.add("bcp");
    val.add("\"select * from " + [Database] + ".[Schema]." + table_name + "\"");
    val.add("queryout");
    val.add(dataPath.toString());
    val.add("-c");
    val.add("-t");
    val.add(",");
    val.add("-S");
    val.add([server]);// ex: if Server is 10.0.0.1  then val.add("10.0.0.1");
    val.add("-U");
    val.add([user]); // ex: if User_name is TestA then val.add("TestA");
    val.add("-P");
    val.add([password(with special characters)]); // ex: if Password is !@#MyPassword*& then val.add("!@#MyPassword*&");
    ProcessBuilder builder = new ProcessBuilder(val);
    File logFile = new File("LogFile.txt");
    System.out.println("BCP command :" + builder.command());
    builder.redirectlogFile(logFile);
    builder.directory(dir);
    Process exec = builder.start();
    System.out.println("BCP process completed : with errors :" + exec.waitFor());
    System.out.println("BCP logFile :" + org.​apache.​commons.​io.FileUtils.readFileToString(logFile));

我收到以下错误:

BCP command :[bcp, "select * from DataBase.schema.TABLE_NAME", queryout,tableData.csv, -c, -t, ,, -S, 10.0.0.1, -U, TestA, -P, !@#MyPassword*&]

BCP process completed : with errors :1

BCP logFile : Starting copy... SQLState = S1000, NativeError = 0 Error = [Microsoft][SQL Server Native Client 11.0]Unable to resolve column level collations

SQLState = 37000, NativeError = 102 Error = [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'select * from DataBase.schema.TABLE_NAME'.

通过查看错误,我检查了 ServerDatabaseTable 的排序规则,看起来都很相似 SQL_Latin1_General_CP1_CI_AS

系统规格:

Linux :
    uname -mrs
        Linux 3.10.0-327.10.1.el7.x86_64 x86_64
    uname -a
        Linux [domain] 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
    cat /etc/redhat-release
        CentOS Linux release 7.2.1511 (Core)

ODBC Driver for linux:
    isql --version
        unixODBC 2.3.0

    odbcinst -q -d -n "SQL Server Native Client 11.0"
        [SQL Server Native Client 11.0]
        Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
        Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0

任何人都可以帮助我我在这里做错了什么。

使用 freebcp 命令来自 Linux 语法与 BCP 基本相同。

bcp mydatabase.dbo.mytable out mytable.csv /U myusername /P mypassword /S myhost /c  

等同于

freebcp mydatabase.dbo.mytable out mytable.csv -U myusername -P mypassword -S myhost -c

我想和你分享一些建议。你可能有尝试。希望它能解决您的问题。

建议一:

如果我们更改列的排序规则或对 table/view 自己的属性进行任何更改,有时会出现此问题。 您可以做的一件事是检查列 b\w 源和目标 table 的排序规则设置,然后在源脚本中使用 COLLATE 子句更改排序规则以匹配目标的排序规则。 这个问题可以通过重新创建 table/view 来解决。所以我会建议您再次从 "DataBase.schema"

创建 "TABLE_NAME"

建议2:

Recommended solution:

有时 queryout 不工作并给出错误。在那种情况下,如果我们使用 print 那么这个问题就解决了。 您可以浏览此部分:SQL query using bcp not working with queryout as a variable

建议三:

Tough task but useful:

您可以尝试远程连接到服务器和 运行 bcp,而不使用代码中的 -S 选项。 去掉下面的部分试试看:

val.add("-S");
val.add([server]);// ex: if Server is 10.0.0.1  then val.add("10.0.0.1");

如果服务器排序规则不同于数据库或列排序规则,请尝试创建格式文件并在 bcp 中明确指定它。

建议4:

有时nullbulk insert也可能导致这个问题。你可以通过: Bulk Insert Collation Error

建议5:

此问题发生在 SQL Server 2005 Native Client。但是微软不会在以前的版本中修复这个问题。他们已在 SQL Server 2008 Native Client 中修复它。有关更多详细信息,您可以通过: BCP Error - Unable to resolve column level collations - by krishnc

ProcessBuilder 与 CMD 的行为不同,因此您不需要引号:

val.add("select * from " + database_name + ".[Schema]." + table_name);

我已经尝试创建 shell 脚本 它工作正常。

File dir = new File("Mydirectory"); 
Path dataPath = Paths.get("tableData.csv"); 
SStringBuilder strb = new StringBuilder();
strb.append("bcp ");
strb.append("\"select " + column + "  from " + credentials.getSchema() + ".dbo." + table_name + "\" ");
strb.append("queryout ");
strb.append(dataPath.toString());
strb.append(" -c ");
strb.append("-t ");
strb.append(", ");
strb.append("-S ");
strb.append(credentials.getServer());
strb.append(" -U ");
strb.append(credentials.getUser());
strb.append(" -P ");
strb.append(credentials.getPassword());

File shellFile = new File(folderName + File.separator + "exec.sh");

try (FileOutputStream outShell = new FileOutputStream(shellFile)) {
    outShell.write(strb.toString().getBytes());
    outShell.flush();
}
shellFile.setExecutable(true, false);
shellFile.setWritable(true, false);
shellFile.setReadable(true, false);
builder = new ProcessBuilder(shellFile.getAbsolutePath());

File logFile = new File("LogFile.txt");
System.out.println("BCP command :" + builder.command());
builder.redirectlogFile(logFile); builder.directory(dir); 
Process exec = builder.start();
System.out.println("BCP process completed : with errors :" + exec.waitFor());
System.out.println("BCP logFile :" + org.​apache.​commons.​io.FileUtils.readFileToString(logFile));

感谢大家...