本文主要介绍如何采用neo4j方式操作neo4j图数据库,当然也可以采用cyber等方式实现。
首先编写一个neo4j数据的连接池:
JdbcConnectionsPool
public class JdbcConnectionsPool implements DataSource {
private static PropertyUtil property = PropertyUtil.getInstance();
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JdbcConnectionsPool.class);
private static String driver;
private static String url;
private static String username;
private static String password;
private static int jdbcConnectionInitSize;//最小连接数量
private static int max = 10; //当前最大连接数量
private static LinkedList list = new LinkedList
static {
try {
url = property.getString("mysql.jdbc.url", "");
username = property.getString("mysql.username", "");
password = property.getString("mysql.pwd", "");
driver = property.getString("mysql.driver", "");
Class.forName(driver);
jdbcConnectionInitSize = property.getInt("jdbcConnectionInitSize", 0);
//创建最小数据库的连接
for (int i = 0; i < jdbcConnectionInitSize; i++) {
final Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("connected to mysql...");
list.add(conn);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public Connection getConnection() throws SQLException {
if (list.size() == 0 && max <= 50) {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
for (int i = 0; i < jdbcConnectionInitSize; i++) {
final Connection conn = DriverManager.getConnection(url, username, password);
list.add(conn);
}
max++;
}
if (list.size() > 0) {
final Connection conn1 = (Connection) list.removeFirst();
return (Connection) Proxy.newProxyInstance(Neo4jConnectionsPool.class.getClass().getClassLoader(),
new Class[]{Connection.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!method.getName().equalsIgnoreCase("close")) {
return method.invoke(conn1, args);
} else {
list.add(conn1);
return null;
}
}
});
} else {
System.out.println("connect database error.");
}
return null;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}
@Override
public T unwrap(Class iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class> iface) throws SQLException {
return false;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
}
@Override
public int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
编写neo4j的数据操作:
public class JdbcConnection {
private static JdbcConnectionsPool pool = new JdbcConnectionsPool();
private static Connection conn = null;
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JdbcConnection.class);
/**
* 获取链接
*
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return pool.getConnection();
}
/**
* 根据hbase 的videoname模糊查询mysql更新hbase节目信息videonum,type
*
* @param rk
*/
public static Boolean queryAndUpateHbase(String rk) {
Boolean b = true;
PreparedStatement stat = null;
ResultSet rs = null;
String table = "ods_videobaseinfo";
String family = "info";
String type = "";
String videonum = "";
String videoscore = "";
try {
conn = getConnection();
String videoname = HBase.getResultByColumn(table, rk, family, "videoname");
String sql = "select type,videonum,videoname from t_filmBasicInfo where videoname like '%" + videoname + "%'";
stat = conn.prepareStatement(sql);
rs = stat.executeQuery();
while (rs.next()) {
String type1 = rs.getString(1);
int videonum1 = rs.getInt(2);
type = HBase.getResultByColumn(table, rk, family, "type");
videonum = HBase.getResultByColumn(table, rk, family, "videonum");
videoscore = HBase.getResultByColumn(table, rk, family, "videoscore");
if (StringUtils.isEmpty(type) || type == null) {
HBase.put(rk, table, family, new String[]{"type"}, new String[]{type1}, false);
}
if (StringUtils.isEmpty(videonum) || videonum == null) {
HBase.put(rk, table, family, new String[]{"videonum"}, new String[]{videonum1 + ""}, false);
}
if (StringUtils.isEmpty(videoscore) || videoscore == null) {
HBase.put(rk, table, family, new String[]{"videoscore"}, new String[]{videoscore}, false);
}
System.out.println("upating the hbase info...");
}
} catch (Exception e) {
logger.error("execute query error:" + e.getMessage());
b = false;
} finally {
try {
release(conn, stat, rs);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("updated hbase info...");
return b;
}
/**
* 查询
*
* @param sql
* @return
*/
public static Boolean executeQuery(String sql) {
Boolean result = true;
PreparedStatement stat = null;
ResultSet rs = null;
try {
conn = getConnection();
stat = conn.prepareStatement(sql);
rs = stat.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
result = false;
logger.error("execute query error:" + e.getMessage());
} finally {
try {
release(conn, stat, rs);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 插入neo4j数据库
*
* @param sql
* @return
*/
public static Boolean executeInsert(String sql) {
Boolean result = true;
PreparedStatement stat = null;
ResultSet rs = null;
try {
conn = getConnection();
stat = conn.prepareStatement(sql);
conn.setAutoCommit(false);//取消手动提交
int num = 0;
stat.setString(1, "");
stat.setString(2, "");
stat.addBatch();
num++;
//每100条提交一次
if (num >= 100) {
stat.executeBatch();
conn.commit();
num = 0;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
release(conn, stat, rs);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 更新neo4j数据库
*
* @param sql
* @return
*/
public static Boolean executeUpdate(String sql) {
Boolean result = true;
PreparedStatement stat = null;
ResultSet rs = null;
try {
conn = getConnection();
stat = conn.prepareStatement(sql);
stat.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
release(conn, stat, rs);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 释放连接
*
* @param conn
* @param st
* @param rs
*/
public static void release(Connection conn, PreparedStatement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if (st != null) {
try {
st.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
配置信息:
db.jdbc.url=jdbc:neo4j:http://192.168.1.11/7474/
username=test
pwd=123456
driver=org.neo4j.jdbc.Driver
jdbcConnectionInitSize=5