sequential reading from hash file

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
Stef
Participant
Posts: 24
Joined: Wed May 21, 2003 12:37 pm
Location: Montreal, Canada

sequential reading from hash file

Post by Stef »

Hi,
I'm new in Basic language. I would like to know how to implement the sequential reading of an hash file records. Every example I saw on hash file reading showed me the reading of records using the key.
I'll appreciate any kind of suggestion.
Thanks.

Stef.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

While I'm certainly no expert on this subject, I do believe this is the proper way to access hash files from DS BASIC. You are, in essence, getting the next key 'sequentially' and then using that key to fetch the appropriate data for that key.

You can optionally order things or use alternate indexes to bring things out in a different order (there should be posts here on the subject that search turns up), otherwise I believe the examples you must be looking at will do what you want.

Have you tried them? Are you seeing a different behaviour? Are you trying to ensure that records come out in the order they went in? [?] Not sure that's possible given the nature of hash files...

Please clarify for us what exactly you mean by 'sequential reading'.

-craig
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Stef

A hash file its nature is stored randomly based on the algorythm of the key. All hash files require a key. You can only loop through a sequential file in the order it was built. In BASIC we use a different version of the SELECT statement to get a list of keys. You can also get a list of any column in the hash file. A record is one long string. Each column is separated by field marks or @FM. If you have a compound key then the columns in the key are separate by @TM. A @FM is a char(254). A @TM is a char(252). If you have these characters in your data stream that built the hash file then you are in trouble. You can use indexes and do anything that most databases can do if you know how. To access a column's value in a record then they use what is called dynamic arrays. A dynamic array is an undimensioned array. Where normally you would use ArrayName(3) to get the third element of an array in a dynamic array use ArrayName. Here is how to read all the records in a DS_JOBS and pull out the category field which is the fifth element. Records are sometimes called "items" and keys are called "id".


open "DS_JOBS" to DsJobs else stop
ecmd='SELECT DS_JOBS UNLIKE "..."'
execute ecmd capturing output
loop while readnext id
read JobRec from DsJobs, id then
JobCategory = JobRec
*... do whatever ...
end
repeat


Kim.

Kim Duke
DwNav - ETL Navigator
www.Duke-Consulting.com
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

A slightly technical addendum to Kim's post. Kim's example code uses what is called "the default Select List", or Select List number 0. I have found on occasion that this clashes with DataStage's own use of this Select List, particularly if running from the Debugger.
Therefore I always use one of the non-default numbered Select List (these are numbered from 1 to 10).
Also I believe it is bad practice to include the STOP statement in a routine; I always prefer to log an appropriate warning message and to RETURN.
For example:
Open hashedfilename To filevariable
On Error

ErrMsg = 'Error opening "' : hashedfilename : '". Code = ' : Status()
Call DSLogWarn(ErrMsg, RoutineName)

End
Then

* Select all records in key order.
SSelect filevariable To 10

* Go through this list one key at a time.
Loop
While ReadNext KeyValue From 10

* Read and process the record.
Read HFRecord From filevariable, KeyValue
Then
* code to process this record
End
Else
* Code to handle "selected record not found"
End

Repeat

End
Else

ErrMsg = 'Unable to open "' : hashedfilename : '". Code = ' : Status()
Call DSLogWarn(ErrMsg, RoutineName)

End

If Len(ErrMsg) Then RETURN


Ray Wurlod
Education and Consulting Services
ABN 57 092 448 518
Post Reply