Try accessing the dataset from Java using JZOS

Overview

Files in the z / OS environment (IBM mainframe environment) are called data sets. Data sets are slightly different from Linux, Unix, and Windows files (*** 1 **). The features of the dataset and the implementation sample of dataset I / O processing in Java language are shown below.

** * 1 ** In the z / OS environment, there is also a Unix environment called USS in addition to the environment operated by TSO and JCL. Files in the USS environment can be accessed using the standard Java API, just like files in Linux, Unix, and Windows.

Data set features

The dataset has the following characteristics:

  1. For both input and output, you must first allocate (associate the dataset to the process) before opening.
  2. When creating a new dataset, attributes such as record format, logical record length, block size (a collection of several logical records is called a block), and dataset organization must be specified and allocated. ..
  3. In both input and output, it must be free (release the dataset from the process in the reverse of allocation) after closing.
  4. The record delimiter position is identified by the record length, not the newline character.

Data sets with the above characteristics cannot implement I / O processing with the Java standard API (java.io package or java.nio package). Then, what to do is to use ** Java Batch Launcher and Toolkit for z / OS (hereinafter referred to as JZOS) **. JZOS is a toolkit that supports the development of Java applications that run in the z / OS environment and is included in IBM Java for z / OS (see the reference link [JZOS Batch Launcher and Toolkit](https:: for an overview of JZOS). //www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.80.doc/zsecurity-component/jzos.html See "Reference link: JZOS Batch Launcher and Toolkit").

The sample Java application that implements the input processing from the dataset and the sample Java application that implements the output processing to the dataset are shown below (*** 2 **).

** * 2 ** There are various types of datasets, but in the following, samples are shown focusing on the sequential organization dataset, which is similar to Windows files and Unix files and has the simplest structure. ..

Sample program (input processing from dataset)

The following sample program (DSRead.java) reads records from the dataset 'USER01.TEST.DATA' and writes them to standard output. It is assumed that the data is encoded in ʻUTF-8`.

DSRead.java


package sample;

import com.ibm.jzos.RecordReader;
import com.ibm.jzos.ZFile;

public class DSRead {
	public void main(String args[]) throws Exception {
		String ddName = ZFile.allocDummyDDName();
		String dsName = "USER01.TEST.DATA";

		RecordReader reader = null;
		int x = 0;

		try {
			ZFile.bpxwdyn("alloc fi("+ddName+") da("+dsName+") shr reuse");
			reader = RecordReader.newReaderForDD(ddName);
			byte[] recordBuf = new byte[reader.getLrecl()];
			while ((reader.read(recordBuf)) >= 0) {
				String rec = new String(recordBuf, "UTF-8");
				System.out.println(rec);
				x++;
			}
			System.out.println("record count : "+x);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			ZFile.bpxwdyn("free fi("+ddName+")");
		}
	}
}

** This is the point! **

--Dataset allocation and free is done with the ZFile # bpxwdyn method. --This method BPXWDYN For dynamic allocation (dynamically allocating datasets programmatically). Call a command interface called ibm.zos.v2r2.bpxb600/wdyn.htm (reference link: BPXWDYN)). Specify the command string (" alloc ... ", " free ... ") you want to execute as an argument. --Specify the DD name for the alloc command (*** 3 **) and the free command. The DD name is a logical name that identifies the dataset in your program. The ZFile # allocDummyDDName method is generating a unique DD name. --The process of reading a data record from a file is done with RecordReader. --An instance of RecordReader is obtained by calling the factory method RecordReader # newReaderForDD with the DD name mentioned above. For details on the API, refer to the reference link [JZOS API (Javadoc)](http://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm. See .jzos / index.html "Reference link: JZOS API (Javadoc)").

** * 3 ** ʻalloc is an abbreviation for ʻallocate. See the reference link Examples of allocate command for specific examples of the allocate command. See "Reference link: Example of allocate command").

Sample program (output processing to data set)

The following sample program (DSWrite.java) writes records in overwrite mode to the already existing dataset'USER01.TEST.DATA'. The dataset'USER01.TEST.DATA' has a fixed length and a logical record length of 80 bytes, and outputs a 3-line record created by the getTestData method. The data shall be encoded in UTF-8 and output.

DSWrite.java


package sample;

import com.ibm.jzos.RecordWriter;
import com.ibm.jzos.ZFile;

public class DSWrite {
	public void main(String args[]) throws Exception {
		String ddName = ZFile.allocDummyDDName();
		String dsName = "USER01.TEST.DATA";

		RecordWriter writer = null;
		int x = 0;

		try {
			ZFile.bpxwdyn("alloc fi("+ddName+") da("+dsName+") old reuse");
			writer = RecordWriter.newWriterForDD(ddName);
			int lrecl = writer.getLrecl();
			String records[] = getTestData();
			for (String record : records) {
				writer.write(record.getBytes("UTF-8"), 0, lrecl);
				x++;
			}
			System.out.println("record count : "+x);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			ZFile.bpxwdyn("free fi("+ddName+")");
		}
	}
	private String[] getTestData() {
		String[] records = new String[3];
		records[0] = String.format("%-80s", "AAAAAAAAAAAAAAAAAAAA");
		records[1] = String.format("%-80s", "BBBBBBBBBBBBBBBBBBBB");
		records[2] = String.format("%-80s", "CCCCCCCCCCCCCCCCCCCC");
		return records;

	}
}

** This is the point! **

--The data set allocation and free are almost the same as the above sample DSRead.java. --The only difference is that the DISP parameter of the alloc command specifies old instead of shr. You can access the dataset exclusively by specifying old. --The process of writing data records to a file is done with RecordWriter. --An instance of RecordWrite is obtained by calling the factory method RecordWriter # newWriterForDD with the DD name. For details on the API, refer to the reference link [JZOS API (Javadoc)](http://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm. .jzos / index.html See "JZOS API (Javadoc)").

Reference link

JZOS Batch Launcher and Toolkit https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.80.doc/zsecurity-component/jzos.html

JZOS API (Javadoc) http://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm.jzos/index.html

BPXWDYN https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.2.0/com.ibm.zos.v2r2.bpxb600/wdyn.htm

** Example of allocate command ** http://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.ikjc500/ikj2l2_ALLOCATE_command_examples.htm

Recommended Posts

Try accessing the dataset from Java using JZOS
Try using the Stream API in Java
Try using the Emotion API from Android
Try using the Wii remote with Java
Try calling the CORBA service from Spring (Java)
Try using the COTOHA API parsing in Java
Output the maximum value from the array using Java standard output
Try implementing the Eratosthenes sieve using the Java standard library
Try calling IBM Watson Assistant 2018-07-10 from the Java SDK.
Using the database (SQL Server 2014) from a Java program 2018/01/04
Try global hooking in Java using the JNativeHook library
Try using RocksDB in Java
Try scraping using java [Notes]
Using Docker from Java Gradle
[Java] Try editing the elements of the Json string using the library
Java comparison using the compareTo () method
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using the messaging system Pulsar
Try using IBM Java method tracing
The road from JavaScript to Java
Sample code using Minio from Java
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
[Java] Try to solve the Fizz Buzz problem using recursive processing
Try passing values from Java Servlet to iPhone app using JSON
Try using Java framework Nablarch [Web application]
Try using || instead of the ternary operator
Try using the service on Android Oreo
Newcomer training using the Web-Basic programming using Java-
Try using the Rails API (zip code)
Kick ShellScript on the server from Java
Hit the Salesforce REST API from Java
Study Java Try using Scanner or Map
Using JavaScript from Java in Rhino 2021 version
Try using JSON format API in Java
Try calling the CORBA service in Java 11+
Connect from Java to MySQL using Eclipse
Try running a Kubernetes Job from Java
Call Java methods from Nim using jnim
Try using JobScheduler's REST-API --Java RestClient implementation--
Try calling Nim from Java via JNI
[Java] Generate a narrowed list from multiple lists using the Stream API
Try adding text to an image in Scala using the Java standard library
Try calling Watson NLU that seems to support Japanese from the Java SDK
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Try to access the on-premise system from SAP Cloud Platform --Java App Edition
Access Forec.com from Java using Axis2 Enterprise WSDL
Try using Firebase Cloud Functions on Android (Java)
Try using JobScheduler's REST-API --Java RestClient Test class-
Using JUnit from the command line on Ubuntu
[Java] Set the time from the browser with jsoup
Newcomer training using the Web-Basic programming / classes using Java-
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try similar search of Image Search using Java SDK [Search]
Try the free version of Progate [Java II]
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Try communication using gRPC on Android + Java server
[Java] Try to solve the Fizz Buzz problem