XQuery Overview and its uses in Java

  • Definition: XQuery is a query language designed for querying collections of XML data. It is a standardized language developed by the W3C (World Wide Web Consortium) for querying and extracting information from XML documents.
  • Syntax: XQuery syntax resembles SQL (Structured Query Language) but is specifically tailored for XML data. It allows users to retrieve, manipulate, and transform XML data using a set of expressive query constructs.
  • Use Cases:
  • XML Data Retrieval: XQuery is primarily used for extracting specific information from XML documents. This can include retrieving elements, attributes, or entire subtrees based on certain criteria.
  • Data Transformation: XQuery supports transformations of XML data into different formats. It enables the conversion of XML documents into other XML formats, HTML, JSON, or plain text.
  • XML Data Manipulation: XQuery facilitates the modification of XML data. It allows for the insertion, deletion, and updating of elements and attributes within XML documents.
  • Integration with XML Databases: XQuery is often used with XML databases for efficient querying and management of XML data.
  • Web Services: XQuery can be utilized in web services to query XML data and transform it according to the requirements of clients.
  • XML Publishing: XQuery can be employed to extract data from XML sources and publish it in various formats for consumption by different applications.
  • Benefits:
  • Declarative Language: XQuery follows a declarative approach, where users specify what data they want to retrieve or manipulate without needing to specify how to achieve it. This abstraction simplifies query development.
  • Expressiveness: XQuery provides powerful constructs for querying and transforming XML data, enabling users to express complex operations concisely.
  • Standardization: Being a W3C standard, XQuery ensures interoperability across different systems and platforms that support XML.
  • Efficiency: XQuery queries are optimized for processing XML data efficiently, providing good performance even with large datasets.
  • Integration: XQuery can be seamlessly integrated with other XML technologies such as XPath, XSLT, and XML Schema, allowing for comprehensive XML data processing solutions.

Java APIs for XQuery:

Java provides several APIs for working with XQuery:

  1. XQJ (XQuery API for Java): This is a standard API for executing XQuery from Java programs. It provides interfaces and classes for compiling, executing, and processing XQuery expressions within Java applications. Some popular implementations of XQJ include:
  • Saxon XQJ
  • BaseX XQJ
  • Oracle XQuery for Java (XQJ)
  1. JAXP (Java API for XML Processing): While not specifically designed for XQuery, JAXP provides facilities for parsing and manipulating XML documents in Java. It includes interfaces for XPath evaluation, which can be utilized for executing XPath expressions within XQuery.
  2. Java XQuery Engines: Some Java-based XQuery engines offer APIs for integrating XQuery processing directly into Java applications. Examples include:
  • Saxon XQuery
  • BaseX Java API
  • eXist-db Java API
  1. Third-party Libraries: There are various third-party libraries available for XQuery processing in Java, offering additional functionalities and features. These libraries may provide their own APIs for executing XQuery. Examples include:
  • Apache Jena
  • XMLBeans

Using these Java APIs, developers can seamlessly integrate XQuery processing capabilities into their Java applications, enabling efficient querying and manipulation of XML data.

In our blog we will first install BaseX tool that provide app that we can use for querying the xml directly.
You can download the BaseX tool from below locations.

https://basex.org/download

Now lets create one simple xls which we will use in our exercise.

1- course.xml

<?xml version="1.0" encoding="UTF-8"?>  
<courses>     
   <course category="JAVA">  
      <title lang="en">Learn Java in 3 Months.</title>  
      <trainer>Sonoo Jaiswal</trainer>  
      <year>2008</year>  
      <fees>10000.00</fees>  
   </course>    
    <course category="Dot Net">  
      <title lang="en">Learn Dot Net in 3 Months.</title>  
      <trainer>Vicky Kaushal</trainer>  
      <year>2008</year>  
      <fees>10000.00</fees>  
   </course>  
    <course category="C">  
      <title lang="en">Learn C in 2 Months.</title>  
      <trainer>Ramesh Kumar</trainer>  
      <year>2014</year>  
      <fees>3000.00</fees>  
   </course>  
    <course category="XML">  
      <title lang="en">Learn XML in 2 Months.</title>  
      <trainer>Ajeet Kumar</trainer>  
      <year>2015</year>  
      <fees>4000.00</fees>  
   </course>    
</courses>  

Lets try to play with Xquery syntex with this newly created course.xml and check if we are getting the desire output.

Now lets create a xquery yml as given below

2- courses.xqy

let $courses := (doc("course.xml")/courses/course)  
return <results>  
{  
   for $x in $courses  
   where $x/fees>2000  
   order by $x/fees  
   return $x/title  
}  
</results>  

Now lets try to execute our courses.xqy by using our maven project.

create a maven project using below command

mvn archetype:generate -DgroupId=com.mycompany -DartifactId=siddhuxqueryapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Lets open that project in STS IDE and add below given package to consume Xquery file

and following dependencies init.

3- Maven entry

<dependencies>
    <!-- Saxon HE -->
    <dependency>
        <groupId>net.sf.saxon</groupId>
        <artifactId>Saxon-HE</artifactId>
        <version>10.6</version>
    </dependency>
</dependencies>

4- Lets keep the course.xml and our courses.xqy in our resource folder where our java files is present

5- XQueryConsumer.java

package com.mycompany;
import java.io.File;

import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.XQueryCompiler;
import net.sf.saxon.s9api.XQueryEvaluator;
import net.sf.saxon.s9api.XQueryExecutable;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XdmValue;

public class XQueryConsumer {

    public static void main(String[] args) throws Exception {
    	Processor processor = new Processor(false); // Use false for Saxon-HE
    	DocumentBuilder documentBuilder = processor.newDocumentBuilder();
    	XdmNode sourceDocument = documentBuilder.build(new File("C:\\STS-Workspace\\siddhuxqueryapp\\src\\main\\resources\\course.xml"));
    	XQueryCompiler compiler = processor.newXQueryCompiler();
    	XQueryExecutable executable = compiler.compile(new File("C:\\STS-Workspace\\siddhuxqueryapp\\src\\main\\resources\\courses.xqy"));
    	XQueryEvaluator evaluator = executable.load();
    	evaluator.setContextItem(sourceDocument); // Set the source document as the context item
    	// Set any other execution options as needed
    	XdmValue result = evaluator.evaluate(); // Execute the query
    	for (XdmItem item : result) {
    	    System.out.println(item.getStringValue());
    	}

    	
    }
}

You can download the project from given below git location.

https://github.com/shdhumale/siddhuxqueryapp.git

Summary:-

  • 💡 XQuery is a query language for XML data developed by W3C.
  • 💡 Its syntax resembles SQL but is tailored for XML data manipulation.
  • 💡 Use cases include XML data retrieval, transformation, manipulation, integration with databases, web services, and publishing.
  • 💡 Benefits include declarative approach, expressiveness, standardization, efficiency, and integration with other XML technologies.
  • 💡 Java provides APIs like XQJ and JAXP for XQuery processing.
  • 💡 Java XQuery engines and third-party libraries offer additional functionalities.
  • 💡 Developers can integrate XQuery processing into Java applications efficiently.
  • 💡 The blog provides examples and resources for practicing XQuery in Java development.
Posted in Uncategorized | Tagged , , , , | Leave a comment

Low code No Code drag drop Flowise AI

Flowise AI is a low-code/no-code platform that utilizes a drag-and-drop interface to allow users to build applications powered by Large Language Models (LLMs). This means:

Low-code/no-code: Requires minimal to no coding knowledge, making it accessible to a wider range of users.
Drag-and-drop: Users can visually build their applications by dragging and dropping pre-built components onto a canvas.
Large Language Models (LLMs): Flowise leverages the power of LLMs, like ChatGPT, to enable functionalities like chatbots, text summarization, and data analysis.
Here’s a breakdown of Flowise AI’s key aspects:

Purpose: Simplifies LLM development, making it accessible to non-programmers.
Functionality: Drag-and-drop interface for building LLM applications.
Applications: Chatbots, Q&A systems, data analysis tools, and more.
Benefits: Democratizes LLM technology, reduces development time, and empowers non-technical users.
Overall, Flowise AI aims to bridge the gap between complex LLM technology and user-friendly application development.

In our last blogitem we had used langchain, Open AI LLM and Astra DB to upload the pdf file and chat with PDF file using Springboot application.

Now lets say if same can be done without any code 🙂
Yes you heard it right.. here comes no code AI tool Flowise.

Lets start the same

Step 1:- Down load the Apache 2 license source code of Flowise from below git hub

https://github.com/FlowiseAI/Flowise

Flowise has 3 different modules in a single mono repository.

server: Node backend to serve API logics

ui: React frontend

components: Integrations components

There are three ways to run the code

1- Quick Start

1- Install Flowise
npm install -g flowise
2- Start Flowise
3- npx flowise start
Open http://localhost:3000

2- Using Docker

Docker Compose
1- Go to docker folder at the root of the project
2- Copy the .env.example file and paste it as another file named .env
3- docker-compose up -d
4- Open http://localhost:3000
5- You can bring the containers down by docker-compose stop

Docker Image

1- Build the image locally:
docker build –no-cache -t flowise .

2- Run image:
docker run -d –name flowise -p 3000:3000 flowise

3- Stop image:
docker stop flowise

For Developer

1- Install Yarn
npm i -g yarn

Setup

1- Clone the repository
git clone https://github.com/FlowiseAI/Flowise.git

2- Go into repository folder
cd Flowise

3- Install all dependencies of all modules:
yarn install

4- Build all the code:
yarn build

5- Start the app:
yarn start

6- You can now access the app on http://localhost:3000

7- For development build: if you want to run in different port number.
Create .env file and specify the PORT (refer to .env.example) in packages/ui
Create .env file and specify the PORT (refer to .env.example) in packages/server
yarn dev

Once start the server on local you will get this first Flowise screen.

Now lets recreate below scenario with no code plateform

user uplaod the pdf file -> Convert the pdf file to vector and store in Astra DB -> user ask question -> related data will be found from Astra DB -> user question and data from astra DB will be send to Open AI LLM-> Finally we get reply from OpenAI LLM.

For that Flow wise has given ready made flows in market place as shown below

Now lets use conversational retrieval QA chain option

Now save this flow and change the txt component to pdf as shown below.

Also remove Pinecone componet with Astra

Now your flow will look like this.

Now lets add the required items

1- Open API key at the belwo location

2- Change the Astra component data as shown below

From where we will get Astra configuration data

1- Create a new DB

DB Name:- flowisedb
Name space:- flowise4chat
db id: c7c1d19b-4d10-4337-81f3-b92081d9c53e
Token Details :- AstraCS:DOMS56e95 API end point :- https://***.datastax.com

Now lets save our flow and upload a pdf file

and upload the pdf to Astra by clicking this icon on the screen.

Once the data is uploaded check it is uploaded on the Astra DB

Now lets execute our chat by cliking this icon

Posted in Uncategorized | Tagged , , , , | Leave a comment