java - 使用Mysql ClusterJ jar文件连接到NDB集群

标签 java mysql scala mysql-cluster

我按照 ClusterJ 教程 here 中的说明进行操作使用 NDB api 连接到 MySQL NDB 集群。下面是我现在正在做的示例代码:

Main.java

import com.mysql.clusterj.ClusterJHelper;
import com.mysql.clusterj.SessionFactory;
import com.mysql.clusterj.Session;
import com.mysql.clusterj.Query;
import com.mysql.clusterj.query.QueryBuilder;
import com.mysql.clusterj.query.QueryDomainType;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.*;
import java.util.Properties;
import java.util.List;

public class Main {
    public static void main (String[] args) throws
    java.io.FileNotFoundException,java.io.IOException {
     // Load the properties from the clusterj.properties file
     File propsFile = new File("clusterj.properties");
     InputStream inStream = new FileInputStream(propsFile);
     Properties props = new Properties();
     props.load(inStream);
     //Used later to get user input
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

     // Create a session (connection to the database)
     SessionFactory factory = ClusterJHelper.getSessionFactory(props);
     Session session = factory.getSession();

    // Create and initialize an Employee
     Employee newEmployee = session.newInstance(Employee.class);
     newEmployee.setId(988);
     newEmployee.setFirst("John");
     newEmployee.setLast("Jones");
     newEmployee.setStarted("1 February 2009");
     newEmployee.setDepartment(666);

     // Write the Employee to the database
     session.persist(newEmployee);
}
}

当我尝试运行此程序时遇到以下错误:

java.lang.UnsatisfiedLinkError: no ndbclient in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at com.mysql.clusterj.tie.ClusterConnectionServiceImpl.loadSystemLibrary(ClusterConnectionServiceImpl.java:68)
    at com.mysql.clusterj.tie.ClusterConnectionServiceImpl.create(ClusterConnectionServiceImpl.java:87)
    at com.mysql.clusterj.core.SessionFactoryImpl.createClusterConnection(SessionFactoryImpl.java:335)
    at com.mysql.clusterj.core.SessionFactoryImpl.createClusterConnectionPool(SessionFactoryImpl.java:288)
    at com.mysql.clusterj.core.SessionFactoryImpl.<init>(SessionFactoryImpl.java:211)
    at com.mysql.clusterj.core.SessionFactoryImpl.getSessionFactory(SessionFactoryImpl.java:146)
    at com.mysql.clusterj.core.SessionFactoryServiceImpl.getSessionFactory(SessionFactoryServiceImpl.java:36)
    at com.mysql.clusterj.core.SessionFactoryServiceImpl.getSessionFactory(SessionFactoryServiceImpl.java:27)
    at com.mysql.clusterj.ClusterJHelper.getSessionFactory(ClusterJHelper.java:72)
    at com.mysql.clusterj.ClusterJHelper.getSessionFactory(ClusterJHelper.java:57)
    at com.pkg.mysql.Main.main(Main.java:27)

最佳答案

发生错误是因为 ClusterJ 需要 java 类路径中的 ndb_engine.so 文件。

引用链接:http://ftp.nchu.edu.tw/MySQL/doc/ndbapi/en/mccj-getting.html

一旦指定路径,我就可以运行该程序。

# steps to compile and run the ndb java program on linux
javac -classpath /home/user1/clusterj-7.6.8.jar:. Main.java Employee.java
java -classpath /home/user1/clusterj-7.6.8.jar:. -Djava.library.path=/usr/lib64/mysql Main

我仍然找不到在Windows环境下解决它的方法。

关于java - 使用Mysql ClusterJ jar文件连接到NDB集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082407/

相关文章:

Scala:复制具有特征的案例类

java - JAXB 解码排除特定元素

java - 如何在同一个文件中设置项目名称/组/版本,以及 {source,target} 兼容性?

java - 需要在for循环中声明变量

php - CodeIgniter 全局设置数据库

arrays - Scala:将 csv 字符串转换为数组

java - 将字符串从 AS400 转换为 Java

mysql - mysql : nodejs 上的语法无效

mysql - 使用不同的日期格式将 CSV 导入 MySQL

regex - 使用 Applicative Functor 匹配至少 3 个 Scala 正则表达式中的 1 个