670 likes | 956 Views
大型主机平台系统应用开发基础. 教育部- IBM 精品课程. 单位:大连理工大学 作者:陆坤,李凤岐,姜厚云. 第八章处理 VSAM 文件. 8.1 VSAM 的概念和分类 8.2 VSAM 文件的操作 8.3 COBOL 对 VSAM 的处理. 第一节 VSAM 的概念和分类. 什么是 VSAM VSAM 的文件结构 VSAM 的几种类型 VSAM 中的相关概念. 1. 什么是 VSAM.
E N D
大型主机平台系统应用开发基础 教育部-IBM精品课程 单位:大连理工大学 作者:陆坤,李凤岐,姜厚云
第八章处理VSAM文件 • 8.1 VSAM的概念和分类 • 8.2 VSAM文件的操作 • 8.3 COBOL对VSAM的处理
第一节VSAM的概念和分类 • 什么是VSAM • VSAM的文件结构 • VSAM的几种类型 • VSAM中的相关概念
1.什么是VSAM • VSAM(Virtual Storage Access Method)文件是在虚拟存储器和树型数据结构的基础上,为了满足数据量大、存取速度快和维护方便的要求而发展起来的一种文件组织形式。 VSAM以索引键或相对字节地址来安排记录的存放位置。VSAM用于直接或顺序处理固定或可变长度的记录。
2.VSAM的文件结构 VSAM数据集中的数据除线性数据集外,都被组成记录(逻辑记录)。逻辑记录是用户访问数据集的单位。VSAM的逻辑记录与非VSAM数据集的存储方式不同。VSAM将记录存于CI(Control Interval)中。一个CI是直接访问存储设备的一片连续区域,用于存储数据记录及其控制信息。当从直接存储设备上读取一个记录时,包含要读的记录的整个CI都被读到虚存的I/O缓冲区中,然后用户要读的记录才从VSAM缓冲传输到用户定义的工作区。
2.VSAM的文件结构 每一个CI含有如下信息: 1. 逻辑记录:每一个CI中可能包含多个逻辑记录。 2. 自由空间:每一个CI中可能含有一定的自由空间,用于插人新的记录。 3. 控制信息:主要为 RDF和CIDF, RDF描述每一个记录的信息,而CIDF则描述整个CI的信息。 注意:在线性数据集中,CI全部是数据,无控制信息。 CI是定长的,多个邻接的CI构成一个定长的区域,称之为CA(Control Area). 每个CA至少由两个CI组成。 注:R : VSAM数据记录 CF: CI的控制信息
3.VSAM文件的几种组织形式 • VSAM supports the following data set types: • Entry-Sequenced Data Set (ESDS) • Key-Sequenced Data Set (KSDS) • Relative Record Data Set (RRDS) • Linear Data Set (LDS)
3.VSAM文件的几种组织形式 (l)键顺序文件 KSDS: 这种文件与索引文件相似,由索引部分和数据部分组成。 索引部分包含树型结构的多级索引。数据部分内的记录按键值顺序排列。 因此,这种文件既可以按键值进行顺序存取,也可利用索引,根据键值进行直接存取。 (2)输人顺序文件 ESDS: 文件内的记录按输人顺序排列,可以按照排列顺序进行存取,也可以指定记录的相对字节地址对特定记录进行存取。 ESDS is best suited for applications where most processing is done sequentially.
3.VSAM文件的几种组织形式 • KSDS supports a variety of processing options. Hence it is the most commonly used VSAM data set type. • VSAM supports the various options provided by the KSDS, by providing a two-tiered indexing structure to locate data records. • A KSDS has two components: • Index component • Data component
3.VSAM文件的几种组织形式 (3)相对记录文件 RRDS: 记录以其相对记录号顺序存放,访问也是通过相对记录号来进行访问。 有两种RRDS数据集:固定长度的RRDS,记录必须是定长的;可变长度的RRDS,记录长度可变。文件空间被划分为等长的 SLOT,每个SLOT只存放一个记录,从第一个SLOT开始分别赋予连续的顺序号,这样的号码叫相对记录号(RRN)。只要给出相对记录号,就可实现对特定记录的直接存取。 (4)线性文件 LDS: 线性数据集中的数据没有记录边界。也没有其他VSAM数据集中所具有的控制信息。只有数据组件,由应用程序完成对逻辑记录的组合或分解。
4.Relative Byte Address • The terms used to describe the VSAM data sets are different from those of non-VSAM data sets. This topic discusses the basic terms that are used throughout this course and across various VSAM manuals. • What is a Relative Byte Address? • The Relative Byte Address (RBA) of a record is its displacement (in bytes) from the beginning of the data set. • VSAM treats data as a contiguous string of bytes. This approach makes the address of a record device-independent. • A VSAM data set can be moved without effecting the RBAs of its records. Continued…
0 100 200 300 Record 1 Record 2 Record3 Relative Byte Addresses 4.Relative Byte Address (cont’d) • In addition to data records VSAM also stores control Information. The presence of control Information affects the RBAs of subsequent data records. • The example represents a VSAM data set containing 100-byte, fixed-length records. The RBA of the first record is 0. The RBA of the second record is 100. The RBA of the third record is 200 and so on.
4.Cluster • What is a cluster? A cluster is the collection of physical data sets that make up one logical data set. • The concept of a cluster is more suited for a KSDS. Continued…
KSDS.CLUSTER KSDS.INDEX KSDS.DATA 4.Cluster (cont’d) • A KSDS cluster has two data sets. One data set holds the actual data records. The other data set contains an index component. • The index component permits the direct retrieval of data. Continued…
Cluster C A B Related Data Sets 4.Cluster (cont’d) • With an ESDS, an RRDS and an LDS, the cluster name and the data set component name both refer to the same data set and a cluster consists of only a single physical data set: the data component. Continued…
4.Control Interval • What is a control interval? • A control interval is the amount of data transferred between the device and virtual storage. • When a record is read from or written to a data set, VSAM groups individual data records into larger units of storage. These units of storage are called control intervals. Continued…
A 2k (2048 bytes) Control Interval R1 R1 R1 Unused Space 4 2 0 3 4 0 2 2 0 • 9 1 • 0 • 0 5 • 5 4.Control Interval (cont’d) • VSAM groups individual data records into larger units of storage. These units of storage are called control intervals. • All control intervals for a given data set are of the same size. However, the records within a control interval can vary in length.
Control Area with Control Intervals 05 08 09 10 12 14 15 20 21 22 24 25 29 30 35 36 4.Control Area • What is a control area? The control intervals for a data set are grouped Into one or more control areas. The number of control intervals in a control area is fixed by VSAM.
4.Access Control Block • What is an Access Control Block? • An Access Control Block (ACB) is a control block in a processing program that identifies a VSAM data • set and specifies how it will be processed. • What are the functions of an ACB? • The functions of an ACB are listed below: • It identifies the data set using a symbolic ddname. • It specifies how the program will process the data set. • It specifies other basic options. Continued…
第二节VSAM的相关操作 • IDCAMS • VSAM的创建,加载数据,删除 • AIX相关概念
Access Method Services • What is Access Method Services? • VSAM uses certain utility programs for managing and maintaining data sets. • Access Method Services (AMS) is a utility that defines VSAM data sets and allocates space for them. It also converts indexed sequential data sets to KSDS with indexes. • AMS can also be used to perform some functions for non-VSAM data sets. Continued…
Access Method Services (cont’d) • What is IntegrateD Catalog Access Method Services? IntegrateD Catalog Access Method Services (IDCAMS)is the program name for VSAM’s AMS utility. IDCAMS is easy to use and multipurpose in nature. • IDCAMS performs the following functions: It changes certain attributes of a data set It lists data set attributes and statistical information It deletes a data set It defines a data set It loads a data set It copies or backs up a data set It prints the contents of a data set
Non-VSAM Utility Programs • Some of the non-VSAM utility programs are: • IEBGENER – Used to copy a SAM data set • IEBCOPY – Used to copy a partitioned data set • IEBISAM – Used to copy an ISAM data set
DASD Defining a VSAM Data Set with IDCAMS • What is defining a data set? • The process of creating catalog entries for a VSAM data set and allocating space for them is called defining the data set. • A data set must be defined before it is loaded with data or accessed by a processing program. • VSAM data sets can be defined using either IDCAMS or JCL. Continued…
Defining a VSAM Data Set with IDCAMS • The DEFINE CLUSTER function of IDCAMS is used to give the cluster a name and describe its • characteristics. • Listed below are some of the characteristics that can be specified during a DEFINE CLUSTER operation: • Cluster name • Names of the data and index components • Volume(s) on which the data set is to reside • Data set type • Space requirements of the data set • Record size • Position and length of the key field for KSDS • Retention period • Data set passwords
VSAM文件的建立 例1: 建立VSAM数据集(定义一个KSDS): //TE002DS JOB NOTIFY=&SYSUID //STEP1 EXEC PGM=IDCAMS //SYSPRINT DD SYSOUT=* //SYSIN DD * DEL TE002.TEST.KSDS1 CL DEFINE CLUSTER - (NAME(TE002.TEST.KSDS1) - VOLUME(USER01) - RECORDS(1000,500)) - DATA - (NAME(TE002.TEST.KSDS1.DATA1) - KEYS(5,0) - RECORDSIZE(96,96) - FREESPACE(20,10) - BUFFERSPACE(25000)) - INDEX - (NAME(TE002.TEST.KSDS1.INDEX1) - IMBED) - CATALOG(TE002)
VSAM文件的建立 例1: 建立VSAM数据集(定义一个KSDS): 建立一个KSDS数据集TE002.TEST.KSDS1,这个数据集由数据部分TE002.TEST.KSDS1.DATA1和索引部分….INDEX1组成。 VOLUME(USER01) –表示数据集放在哪个卷上。 RECORDS(1000,500)) –表示主分配数为1000个记录,附加分配为500个记录。 KEYS(5,0) –表示主键长度为5, 起始位置在记录的第一个字节 (偏移为0) RECORDSIZE(96,96) –表示记录的平均长度为96,最大长度为96. (相等意味定长) FREESPACE(20,10) –指明加载数据时为CI留20%的自由空间,CA留10%。 BUFFERSPACE(25000)) –提供至少25000字节的I/O缓冲区。
VSAM数据集是如何命名的 定义一个VSAM CLUSTER时,要指定CLUSTER名。通常,CLUSTER名就是JCL中给出的DSNAME。也可以各自独立命名索引和数据部件。如果只指定了CLUSTER名而未指定索引和数据部件的名字,VSAM会自动产生索引和数据部件名字。VSAM以下列方式产生索引和数据部件名字: (1).如果CLUSTER的最后一个字段是CLUSTER,则数据部件和索引部件名 字的最后一个字段分别替换为DATA和INDEX。例如: Cluster名: SALES.REGIONZ.CLUSTER 数据部件名: SALES.REGIONZ.DATA 索引部件名: SALES.REGIONZ.INDEX
VSAM数据集是如何命名的 (2).如果CLUSTER名的长度小于等于38个字符,则分别在CLUSTER名后加"DATA"和"INDEX"。例如: Cluster名: DEPT64.ASSET.INFO 数据部件名: DEPT64.ASSET.INFO.DATA 索引部件名: DEPT64.ASSET.INFO.INDEX (3).如果CLUSTER名长度在39~42之间,则在数据部件和索引部件名后分别加"D"和"I"。例如: Cluster名: DEPTABCD.RESOURCE.REGION66.DATA1234.STUFF 数据部件名: DEPTABCD.RESOURCE.REGION66.DATA1234.STUFF.D 索引部件名: DEPTABCD.RESOURCE.REGION66.DATA1234.STUFF.I
Initial Loading • Once a data set has been defined, it needs to be loaded with data. The IDCAMS REPRO function is used for • initial loading of a data set. • The REPRO function can be used to: • Load a VSAM data set from a SAM, ISAM or another VSAM data set • Copy a VSAM data set to a SAM or VSAM but not to an ISAM data set
REPRO REPRO INDATASET (entry-name) or INFILE (ddname) OUTDATASET (ENTRY-NAME) or OUTFILE (ddname) O SKIP (count) P PROMKEY (key) T FROMNUMBER (number) I O COUNT (count) N TOKEY (key) A TONUMBER (number) L TOADDRESS (address) REUSE . NORUSE REPLACE . NOREPLACE • The example shows the format of another IDCAMS command REPRO. REPRO performs the following tasks: • It loads a VSAM file. • It makes a copy of a VSAM file. Continued…
REPRO REPRO INDATASET(KSVS.MAST.TRAN) _ OUTDATASET(KSVS.MAST.CLUSTER) Here is an example of a REPRO command that can be used to load a KSDS from a sequential file. Continued…
REPRO Input: SKIP(no.-records-to-skip) FROMKEY(key-value) KSDS or ISAM FROMNUMBER(RELATIVE-RECLORD-NUMBER) RRDS only FROMADDRESS(relative-byte-address) KSDS or ESDS only Output: COUNT (no.-records-to-be-copied) TOKEY(key-value) KSDS or ISAM only TONUMBER (relative-record-number) RRDS only TOADDRESS(relative-byte-address) KSDS or ESDS only The rest of the parameters for REPRO are optional. To load a part of the file, use the parameter to establish a range for both input and output data sets. The example here shows code for the same. Continued…
REPRO REPRO INDATASET(KSVS.MAST.SEQ) _ OUTDATASET(KSVS.CUST.MAST) _ SKIP(100) COUNT(500) This example shows the REPRO command defined with a range of records skipped (SKIP) and copied (COUNT). Continued…
REPRO DEFINE CLUSTER _ NAME(KSVS.CUST.CLUSTER) _ REUSE REPRO INDATASET(KSVS.TRAN) _ OUTDATASET(KSVS.CUST.CLUSTER) REUSE Use the REUSE parameter to specify that a file may be loaded even if the output file already contains records, if it is specified earlier in the DEFINE CLUSTER command. The REPLACE option is a method for dealing with KSDS duplicate methods. (REUSE deletes all the old records in the file. ) Continued…
如何复制VSAM数据集 你可用"REPRO"命令复制VSAM数据集: (1)复制或合并一个VSAM数据集到另一个VSAM数据集。 (2)复制或合并一个顺序数据集到另一个顺序数据集。 (3)把顺序或索引顺序的数据集转换为VSAM数据集。 (4)把VSAM数据集复制为顺序数据集。 //**-----------------------------------------------------------------* //** VSAM CONVERT TO FB FORMAT * //**-----------------------------------------------------------------* //LOAD EXEC PGM=IDCAMS //SYSPRINT DD SYSOUT=* //DD1 DD DSN=TE002.TEST.KSDS1,DISP=SHR //DD2 DD DSN=TE002.VSAM.CTTPS,DISP=(,CATLG), // SPACE=(TRK,(2,5),RLSE),VOL=SER=USER01,UNIT=3390, // DCB=(RECFM=FB,LRECL=80) //SYSIN DD * REPRO INFILE(DD1) OUTFILE(DD2) //**-----------------------------------------------------------------* 注意,PS的记录长要和输入到VSAM的那个PS的记录长一样,不然会有问题。
如何LOAD数据到VSAM数据集 //* VSAM LOAD , * //**-----------------------------------------------------------------* //TE002LD JOB NOTIFY=&SYSUID //STEP1 EXEC PGM=IDCAMS //SYSPRINT DD SYSOUT=* //DD1 DD DSN=TE002.VSAM.PS,DISP=SHR //DD2 DD DSN=TE002.TEST.KSDS1,DISP=SHR //SYSIN DD * REPRO INFILE(DD1) OUTFILE(DD2) // //**-----------------------------------------------------------------* 注意:此例子是将TE002.VSAM.PS的数据load到TE002.TEST.KSDS1中,记录长最好一样; PS中按关键字顺序排列的才会load到VSAM中,乱序的行被discard.
PRINT PRINT INDATASET (entry-name) or INFILE (ddname) (format) Input: SKIP(no.-records-to-skip) PROMKEY (key-value) KSDS or ISAM FROMNUMBER (relative-record-number) RRDS only FROMADDRESS (relative-byte-address) KSDS or ESDS only Output: COUNT (no.-records-to-be-copied) TOKEY (key-value) KSDS or ISAM only TONUMBER (relative-record-number) RRDS only TOADDRESS (relative-byte-address) KSDS or ESDS only_ To see any data after it is loaded, use the PRINT command. This dumps the contents of the data set in CHARACTER, HEX or DUMP format. Continued…
LISTCAT-ALTER-DELETE LISTCAT ENTRIES (KISVS.CUSTOMER.*) ALTER KSVS.CUSTOMER.DATA INHIBIT DELETE KSVS.CUSTOMER.CLUSTER _ CLUSTER _ This example shows the LISCAT, ALTER and DELETE commands.
Other IDCAMS functions • IDCAMS can be used to perform a variety of data set management functions. • Listed below are some of the commonly used IDCAMS functions: • PRINT – Prints a VSAM data set • ALTER – Changes certain attributes of a VSAM data set • LISTCAT – Lists data set attribute and statistical information • DELETE – Deletes a data set
Defining Alternate Index • The index of a KSDS provides the means by which VSAM can quickly locate and retrieve a record in the data • component. • But the limitation of a KSDS is that, only one field as the record key can be used here as the search argument • To retrieve a record randomly. • Therefore, VSAM supports another structure, called an alternate index, that can be used to define additional • search argument field. Continued…
Defining Alternate Index (cont’d) • Alternate Index Concepts • Following are some concepts associated with alternate index: • Base Cluster: It is the data set with which an alternate index is associated. • Prime index: When an alternate index is associated with a KSDS, the index component of the KSDS is referred to as the prime index. • Primary key: When an alternate index is associated with a KSDS, the key field is referred to as primary key. • Alternate key: It is the field within base cluster records on which access is desired.
An Alternate Index is Stored Like a KSDS Index Component Data Component Alternate Index Organization • An alternate index is stored physically as a KSDS with variable-length records. • An alternate index has the following components: • Index component • Data components Continued… Concepts
Alternate Index Organization (cont’d) • For a bank account application, the adjacent example shows the account KSDS. Account Number Customer Name 00445 Smith, Harry 00453 Jones, Frank 00455 Todd, John 00545 Smith, Harry 00555 Todd, John 00578 Hodgson, Linda 00581 Dremp, Sally 00596 Crompton, Pearl 00842 Todd, John 00867 Crompton, Pearl Continued… Concepts
Cromption, Peart 00596 00867 Dramp, Sally 00581 Hodgson, Linda 00578 Jones, Frank 00453 Smith, Harry 00545 00445 Todd, John 00555 04555 00842 Alternate Index Organization (cont’d) • Since there are six unique customer names in the base cluster, the customer name alternate index will contain six records. • These records, without control information, are shown in the example. Continued… Concepts
Cromption, Peart 00596 00867 Dramp, Sally 00581 Hodgson, Linda 00578 Jones, Frank 00453 Smith, Harry 00545 00445 Todd, John 00555 04555 00842 Alternate Index Organization (cont’d) • When the alternate key for an alternate index is non-unique, the alternate index record can contain multiple pointer entries. • In the given bank account example, John Todd has three accounts. Consequently, the alternate index record for him contains three pointer entries. • As the base cluster is a KSDS, the pointers are stored as primary key values, which in this case are account numbers. . Continued… Concepts
Creating Alternate Index • An alternate index is created using IDCAMS. JCL does not have the facility to relate an alternate index to its data set. • Given a base cluster that has been defined and loaded, there are three steps that must be performed before a base cluster can be accessed through an alternate index. These steps performed using IDCAMS are: • Define the alternate index • Built the alternate index • Define a path that relates the alternate index to its base cluster Continued… Concepts
第三节大型主机数据库对象介绍 • 在程序中指定VSAM文件 • 打开和关闭VSAM文件 • 读取VSAM文件数据 • 修改VSAM文件数据 • 删除VSAM文件数据 • 对KSDS的特殊处理 • 程序事例
在程序中指定VSAM文件 对于COBOL程序而言,在对VSAM文件进行处 理之前,需要在环境部和数据部对其进行指定。 例如,以下为在环境部中指定VSAM文件的一 段代码: ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT KS-FILE ASSIGN TO VKSDS ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS TEST-KEY FILE STATUS IS TEST-STAT.