Java与XQuery在BaseX集成中的实践指南

简介: Java与XQuery在BaseX集成中的实践指南

本文主要介绍了后端开发中比较小众的一种数据库技术:BaseX。本文讲解Java结合Xquery集成BaseX的案例。

1、集成准备工作

首先是环境的搭建,技术的选型。开发工具的使用。Oxygen是一个数据库可视化连接工具,也是比较小众。

开发工具:

  • IDEA
  • Oxygen

技术:

  • Java
  • BaseX
  • Xpath
  • Xquery

BaseX需要阅读的文档:

2、集成案例目录结构

我们首先新建一个如下的项目目录结构。这里要注意的是,我们的 Xquery文件是放在 resource文件下的。我们要完成的是 BaseXClient和Example文件中的代码。

总结:Base X相当于一个工具类,Example是我们写的创建XML数据库的例子。

3、IDEA配置集成BaseX

按照下图的方式进行IDEA的配置:

总结:BaseX配置的端口一定是:1984 这里不能乱填!

4、集成工具类BaseXClient的创建

创建一个BaseXClient.java文件,写入下面的代码:

package com.linghu.util;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
/**
 * Java client for BaseX.
 * Works with BaseX 7.0 and later
 *
 * Documentation: https://docs.basex.org/wiki/Clients
 *
 * (C) BaseX Team 2005-22, BSD License
 */
public final class BaseXClient implements Closeable {
    /** UTF-8 charset. */
    private static final Charset UTF8 = Charset.forName("UTF-8");
    /** Output stream. */
    private final OutputStream out;
    /** Input stream (buffered). */
    private final BufferedInputStream in;
    /** Socket. */
    private final Socket socket;
    /** Command info. */
    private String info;
    /**
     * Constructor.
     * @param host server name
     * @param port server port
     * @param username user name
     * @param password password
     * @throws
     */
    public BaseXClient(final String host, final int port, final String username,
                       final String password) throws IOException {
        socket = new Socket();
        socket.setTcpNoDelay(true);
        socket.connect(new InetSocketAddress(host, port), 5000);
        in = new BufferedInputStream(socket.getInputStream());
        out = socket.getOutputStream();
        // receive server response
        final String[] response = receive().split(":");
        final String code, nonce;
        if(response.length > 1) {
            // support for digest authentication
            code = username + ':' + response[0] + ':' + password;
            nonce = response[1];
        } else {
            // support for cram-md5 (Version < 8.0)
            code = password;
            nonce = response[0];
        }
        send(username);
        send(md5(md5(code) + nonce));
        // receive success flag
        if(!ok()) throw new IOException("Access denied.");
    }
    /**
     * Executes a command and serializes the result to an output stream.
     * @param command command
     * @param output output stream
     * @throws IOException Exception
     */
    public void execute(final String command, final OutputStream output) throws IOException {
        // send {Command}0
        send(command);
        receive(in, output);
        info = receive();
        if(!ok()) throw new IOException(info);
    }
    /**
     * Executes a command and returns the result.
     * @param command command
     * @return result
     * @throws IOException Exception
     */
    public String execute(final String command) throws IOException {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        execute(command, os);
        return new String(os.toByteArray(), UTF8);
    }
    /**
     * Creates a query object.
     * @param query query string
     * @return query
     * @throws IOException Exception
     */
    public Query query(final String query) throws IOException {
        return new Query(query);
    }
    /**
     * Creates a database.
     * @param name name of database
     * @param input xml input
     * @throws IOException I/O exception
     */
    public void create(final String name, final InputStream input) throws IOException {
        send(8, name, input);
    }
    /**
     * Adds a document to a database.
     * @param path path to resource
     * @param input xml input
     * @throws IOException I/O exception
     */
    public void add(final String path, final InputStream input) throws IOException {
        send(9, path, input);
    }
    /**
     * Replaces a document in a database.
     * @param path path to resource
     * @param input xml input
     * @throws IOException I/O exception
     */
    public void replace(final String path, final InputStream input) throws IOException {
        send(12, path, input);
    }
    /**
     * Stores a binary resource in a database.
     * @param path path to resource
     * @param input xml input
     * @throws IOException I/O exception
     */
    public void store(final String path, final InputStream input) throws IOException {
        send(13, path, input);
    }
    /**
     * Returns command information.
     * @return string info
     */
    public String info() {
        return info;
    }
    /**
     * Closes the session.
     * @throws IOException Exception
     */
    @Override
    public void close() throws IOException, IOException {
        send("exit");
        out.flush();
        socket.close();
    }
    /**
     * Checks the next success flag.
     * @return value of check
     * @throws IOException Exception
     */
    private boolean ok() throws IOException {
        out.flush();
        return in.read() == 0;
    }
    /**
     * Returns the next received string.
     * @return String result or info
     * @throws IOException I/O exception
     */
    private String receive() throws IOException {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        receive(in, os);
        return new String(os.toByteArray(), UTF8);
    }
    /**
     * Sends a string to the server.
     * @param string string to be sent
     * @throws IOException I/O exception
     */
    private void send(final String string) throws IOException {
        out.write((string + '\0').getBytes(UTF8));
    }
    /**
     * Receives a string and writes it to the specified output stream.
     * @param input input stream
     * @param output output stream
     * @throws IOException I/O exception
     */
    private static void receive(final InputStream input, final OutputStream output)
            throws IOException {
        for(int b; (b = input.read()) > 0;) {
            // read next byte if 0xFF is received
            output.write(b == 0xFF ? input.read() : b);
        }
    }
    /**
     * Sends a command, argument, and input.
     * @param code command code
     * @param path name, or path to resource
     * @param input xml input
     * @throws IOException I/O exception
     */
    private void send(final int code, final String path, final InputStream input) throws IOException {
        out.write(code);
        send(path);
        send(input);
    }
    /**
     * Sends an input stream to the server.
     * @param input xml input
     * @throws IOException I/O exception
     */
    private void send(final InputStream input) throws IOException {
        final BufferedInputStream bis = new BufferedInputStream(input);
        final BufferedOutputStream bos = new BufferedOutputStream(out);
        for(int b; (b = bis.read()) != -1;) {
            // 0x00 and 0xFF will be prefixed by 0xFF
            if(b == 0x00 || b == 0xFF) bos.write(0xFF);
            bos.write(b);
        }
        bos.write(0);
        bos.flush();
        info = receive();
        if(!ok()) throw new IOException(info);
    }
    /**
     * Returns an MD5 hash.
     * @param pw String
     * @return String
     */
    private static String md5(final String pw) {
        final StringBuilder sb = new StringBuilder();
        try {
            final MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(pw.getBytes());
            for(final byte b : md.digest()) {
                final String s = Integer.toHexString(b & 0xFF);
                if(s.length() == 1) sb.append('0');
                sb.append(s);
            }
        } catch(final NoSuchAlgorithmException ex) {
            // should not occur
            ex.printStackTrace();
        }
        return sb.toString();
    }
    /**
     * Inner class for iterative query execution.
     */
    public class Query implements Closeable {
        /** Query id. */
        private final String id;
        /** Cached results. */
        private ArrayList<byte[]> cache;
        /** Cache pointer. */
        private int pos;
        /**
         * Standard constructor.
         * @param query query string
         * @throws IOException I/O exception
         */
        Query(final String query) throws IOException {
            id = exec(0, query);
        }
        /**
         * Binds a value to an external variable.
         * @param name name of variable
         * @param value value
         * @throws IOException I/O exception
         */
        public void bind(final String name, final String value) throws IOException {
            bind(name, value, "");
        }
        /**
         * Binds a value with the specified type to an external variable.
         * @param name name of variable
         * @param value value
         * @param type type (can be an empty string)
         * @throws IOException I/O exception
         */
        public void bind(final String name, final String value, final String type) throws IOException {
            cache = null;
            exec(3, id + '\0' + name + '\0' + value + '\0' + type);
        }
        /**
         * Binds a value to the context item.
         * @param value value
         * @throws IOException I/O exception
         */
        public void context(final String value) throws IOException {
            context(value, "");
        }
        /**
         * Binds a value with the specified type to the context item.
         * @param value value
         * @param type type (can be an empty string)
         * @throws IOException I/O exception
         */
        public void context(final String value, final String type) throws IOException {
            cache = null;
            exec(14, id + '\0' + value + '\0' + type);
        }
        /**
         * Checks for the next item.
         * @return result of check
         * @throws IOException I/O exception
         */
        public boolean more() throws IOException {
            if(cache == null) {
                out.write(4);
                send(id);
                cache = new ArrayList<>();
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                while(in.read() > 0) {
                    receive(in, os);
                    cache.add(os.toByteArray());
                    os.reset();
                }
                if(!ok()) throw new IOException(receive());
                pos = 0;
            }
            if(pos < cache.size()) return true;
            cache = null;
            return false;
        }
        /**
         * Returns the next item.
         * @return item string
         * @throws IOException I/O Exception
         */
        public String next() throws IOException {
            return more() ? new String(cache.set(pos++, null), UTF8) : null;
        }
        /**
         * Returns the whole result of the query.
         * @return query result
         * @throws IOException I/O Exception
         */
        public String execute() throws IOException {
            return exec(5, id);
        }
        /**
         * Returns query info in a string.
         * @return query info
         * @throws IOException I/O exception
         */
        public String info() throws IOException {
            return exec(6, id);
        }
        /**
         * Returns serialization parameters in a string.
         * @return query info
         * @throws IOException I/O exception
         */
        public String options() throws IOException {
            return exec(7, id);
        }
        /**
         * Closes the query.
         * @throws IOException I/O exception
         */
        @Override
        public void close() throws IOException {
            exec(2, id);
        }
        /**
         * Executes the specified command.
         * @param code command code
         * @param arg argument
         * @return resulting string
         * @throws IOException I/O exception
         */
        private String exec(final int code, final String arg) throws IOException {
            out.write(code);
            send(arg);
            final String s = receive();
            if(!ok()) throw new IOException(receive());
            return s;
        }
    }
}

5、Example创建并连接Basex数据库

接下来开始创建数据库:

package com.linghu.util;
import java.io.IOException;
import java.io.OutputStream;
/**
 * This example shows how commands can be executed on a server.
 *
 * This example requires a running database server instance.
 * Documentation: https://docs.basex.org/wiki/Clients
 *
 * @author BaseX Team 2005-22, BSD License
 */
public final class Example {
    /**
     * Main method.
     * @param args command-line arguments
     * @throws IOException I/O exception
     */
    public static void main(final String... args) throws IOException {
        // create session
        try(BaseXClient session = new BaseXClient("localhost", 1984, "admin", "admin")) {
        
            session.query("db:create('Zhang')").execute();
        }
    }
}

总结:执行如上代码即可生成上图的数据库、

6、关于BaseX数据库

BaseX是一种用于处理和存储XML数据的数据库管理系统。它是一个开源的XML数据库,具有快速、高效和灵活的特点。

BaseX支持多种查询语言,包括XQuery和XPath。它允许用户通过这些语言对XML数据进行查询、更新和删除操作。BaseX还提供了强大的索引和优化功能,以提高查询和访问性能。

BaseX可以在多个平台上运行,包括Windows、Mac和Linux。它可以作为一个独立服务器运行,也可以嵌入到其他应用程序中使用。

目录
相关文章
|
1月前
|
Java Maven Windows
使用Java创建集成JACOB的HTTP服务
本文介绍了如何在Java中创建一个集成JACOB的HTTP服务,使Java应用能够调用Windows的COM组件。文章详细讲解了环境配置、动态加载JACOB DLL、创建HTTP服务器、实现IP白名单及处理HTTP请求的具体步骤,帮助读者实现Java应用与Windows系统的交互。作者拥有23年编程经验,文章来源于稀土掘金。著作权归作者所有,商业转载需授权。
使用Java创建集成JACOB的HTTP服务
|
3月前
|
安全 Java 数据安全/隐私保护
在Java项目中集成单点登录(SSO)方案
在Java项目中集成单点登录(SSO)方案
|
9天前
|
安全 算法 Java
数据库信息/密码加盐加密 —— Java代码手写+集成两种方式,手把手教学!保证能用!
本文提供了在数据库中对密码等敏感信息进行加盐加密的详细教程,包括手写MD5加密算法和使用Spring Security的BCryptPasswordEncoder进行加密,并强调了使用BCryptPasswordEncoder时需要注意的Spring Security配置问题。
44 0
数据库信息/密码加盐加密 —— Java代码手写+集成两种方式,手把手教学!保证能用!
|
10天前
|
JSON Java 开发工具
Java服务端集成Google FCM推送的注意事项和实际经验
公司的app要上海外,涉及到推送功能,经过综合考虑,选择Google FCM进行消息推送。 查看一些集成博客和官方文档,看的似懂非懂,迷迷惑惑。本篇文章除了将我实际集成的经验分享出来,也会对看到的博客及其中产生的疑惑、注意事项一一评论。 从官方文档和众多博客中,你会发现Java集成FCM推送有多种实现方式,会让生产生文档很乱,不知作何选择的困惑。
34 0
|
2月前
|
前端开发 Java Maven
【前端学java】全网最详细的maven安装与IDEA集成教程!
【8月更文挑战第12天】全网最详细的maven安装与IDEA集成教程!
79 2
【前端学java】全网最详细的maven安装与IDEA集成教程!
|
2月前
|
Java Devops 持续交付
探索Java中的Lambda表达式:简化代码,提升效率DevOps实践:持续集成与部署的自动化之路
【8月更文挑战第30天】本文深入探讨了Java 8中引入的Lambda表达式如何改变了我们编写和管理代码的方式。通过简化代码结构,提高开发效率,Lambda表达式已成为现代Java开发不可或缺的一部分。文章将通过实际例子展示Lambda表达式的强大功能和优雅用法。
|
2月前
|
缓存 安全 Java
Java服务器端技术:Servlet与JSP的集成与扩展
Java服务器端技术:Servlet与JSP的集成与扩展
25 3
|
2月前
|
Java API 数据中心
百炼平台Java 集成API上传文档到数据中心并添加索引
本文主要演示阿里云百炼产品,如何通过API实现数据中心文档的上传和索引的添加。
|
2月前
|
jenkins 持续交付 开发工具
"引爆效率革命!Docker+Jenkins+GIT+Tomcat:解锁持续集成魔法,一键部署Java Web应用的梦幻之旅!"
【8月更文挑战第9天】随着软件开发复杂度的增加,自动化变得至关重要。本文通过实例展示如何结合Docker、Jenkins、Git与Tomcat建立高效的持续集成(CI)流程。Docker确保应用环境一致性;Jenkins自动化处理构建、测试和部署;Git管理源代码版本;Tomcat部署Web应用。在Jenkins中配置Git插件并设置项目,集成Docker构建Tomcat应用镜像并运行容器。此外,通过自动化测试、代码质量检查、环境隔离和日志监控确保CI流程顺畅,从而显著提高开发效率和软件质量。
67 3
|
2月前
|
jenkins Java 持续交付
【一键搞定!】Jenkins 自动发布 Java 代码的神奇之旅 —— 从零到英雄的持续集成/部署实战秘籍!
【8月更文挑战第9天】随着软件开发自动化的发展,持续集成(CI)与持续部署(CD)已成为现代流程的核心。Jenkins 作为一款灵活且功能丰富的开源 CI/CD 工具,在业界应用广泛。以一家电商公司的 Java 后端服务为例,通过搭建 Jenkins 自动化发布流程,包括创建 Jenkins 项目、配置 Git 仓库、设置构建触发器以及编写构建脚本等步骤,可以实现代码的快速可靠部署。
76 2