- All Known Implementing Classes:
- SimpleDoc
- See Also:
- Source Code,
getPrintData()
, javax.print.attribute.DocAttributeSet
, DocFlavor
DocAttributeSet getAttributes()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1767]hash table
By Amira Okasha on 2006/06/01 12:39:18 Rate
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
public class Join {
// The Instance variables used in the class.
static Hashtable ht = new Hashtable ( ) ;
static String [ ] table1Schema;
static String [ ] table2Schema;
static String table1JoinAttName;
static String table2JoinAttName;
private static int getAttInd ( String [ ] arr, String s ) {
int i = 0;
for ( i = 0; i < arr.length; i++ ) {
if ( arr [ i ] .compareTo ( s ) == 0 )
return i;
}
return -1;
}
static void joinParameter ( ) {
Scanner input = new Scanner ( System.in ) ;
System.out
.println ( "Please Enter the Attribute of the join of the First Table:" ) ;
table1JoinAttName = "DEPNO"; // input.nextLine ( ) ;
System.out
.println ( "Please Enter the Attribute of the join of the Second Table:" ) ;
table2JoinAttName = "DPTNO"; // input.nextLine ( ) ;
}
private static void Partition ( String file ) throws IOException, Exception {
BufferedReader buffer = new BufferedReader ( new FileReader ( file ) ) ;
String nextLine;
StringBuffer sb = new StringBuffer ( ) ;
int p = 0;
// String [ ] ccc = table1Schema;
// While loop for reading each tuple in the file
while ( ( ( nextLine = buffer.readLine ( ) ) != null ) ) {
sb.delete ( 0, sb.capacity ( ) ) ;
sb.append ( nextLine ) ;
StringTokenizer st = new StringTokenizer ( sb.toString ( ) , "," ) ;
if ( p == 0 )
table1Schema = new String [ st.countTokens ( ) ] ;
String [ ] ccc = table1Schema;
int i = 0;
String record [ ] = new String [ st.countTokens ( ) ] ;
// nested while loop for reading each element in the tuple
while ( st.hasMoreElements ( ) ) {
String s = new String ( st.nextToken ( ) .toString ( ) .trim ( ) ) ;
// System.out.println ( s ) ;
// for first loop,assigning attributes values to table1schema
// array
if ( p == 0 ) {
table1Schema [ i ] = s;
}
else {
record [ i ] = s;
}
// System.out.println ( table1Schema [ i ] ) ;
i++;
}
if ( p > 0 ) {
ccc = table1Schema;
int joinAttInd = getAttInd ( table1Schema, table1JoinAttName ) ;
if ( joinAttInd == -1 ) {
System.out.println ( "Cannot find Join Attribute in Table 1" ) ;
return;
}
try {
String sx = record [ joinAttInd ] ;
ht.put ( ( String ) record [ joinAttInd ] , record ) ;
} catch ( Exception e ) {
System.out.println ( e ) ;
}
}
p++;
}
String [ ] ccc = table1Schema;
System.out.println ( ht ) ;
}
public static void probe ( Hashtable h, String F ) throws IOException,
Exception {
BufferedReader buffer = new BufferedReader ( new FileReader ( F ) ) ;
String nextLine;
StringBuffer sb = new StringBuffer ( ) ;
int p = 0;
while ( ( ( nextLine = buffer.readLine ( ) ) != null ) ) {
sb.delete ( 0, sb.capacity ( ) ) ;
sb.append ( nextLine ) ;
StringTokenizer st = new StringTokenizer ( sb.toString ( ) , "," ) ;
if ( p == 0 )
table2Schema = new String [ st.countTokens ( ) ] ;
int i = 0;
String record [ ] = new String [ st.countTokens ( ) ] ;
while ( st.hasMoreElements ( ) ) {
String s = new String ( st.nextToken ( ) .toString ( ) .trim ( ) ) ;
// System.out.println ( s ) ;
if ( p == 0 ) {
table2Schema [ i ] = s;
} else {
record [ i ] = s;
}
// System.out.println ( table1Schema [ i ] ) ;
i++;
}
if ( p > 0 ) {
int joinAttInd = getAttInd ( table2Schema, table2JoinAttName ) ;
Object o = ht.get ( record [ joinAttInd ] ) ;
if ( o == null )
System.out.println ( "No join" ) ;
else {
String [ ] y = ( String [ ] ) o;
if ( p == 1 ) {
for ( int c = 0; c < table1Schema.length; c++ )
System.out.print ( table1Schema [ c ] + "\t" ) ;
for ( int c = 0; c < table2Schema.length; c++ )
System.out.print ( table2Schema [ c ] + "\t" ) ;
System.out.println ( ) ;
}
for ( int c = 0; c < y.length; c++ )
System.out.print ( y [ c ] + "\t" ) ;
for ( int c = 0; c < record.length; c++ )
System.out.print ( record [ c ] + "\t" ) ;
System.out.println ( ) ;
}
}
p++;
}
}
public static void nestedLoopJoin ( String f1, String f2 ) throws IOException,
Exception {
BufferedReader buffer = new BufferedReader ( new FileReader ( f1 ) ) ;
String nextLine;
StringBuffer sb = new StringBuffer ( ) ;
int p = 0;
String record1 [ ] = null;
int joinAttInd2 = 0;
int joinAttInd1 = 0;
// Read records from first Table
while ( ( ( nextLine = buffer.readLine ( ) ) != null ) ) {
sb.delete ( 0, sb.capacity ( ) ) ;
sb.append ( nextLine ) ;
StringTokenizer st = new StringTokenizer ( sb.toString ( ) , "," ) ;
if ( p == 0 )
table1Schema = new String [ st.countTokens ( ) ] ;
int i = 0;
record1 = new String [ st.countTokens ( ) ] ;
while ( st.hasMoreElements ( ) ) {
String s = new String ( st.nextToken ( ) .toString ( ) .trim ( ) ) ;
// System.out.println ( s ) ;
if ( p == 0 ) {
table1Schema [ i ] = s;
} else {
record1 [ i ] = s;
}
// System.out.println ( table1Schema [ i ] ) ;
i++;
}
FileReader fr2 = new FileReader ( f2 ) ;
if ( p > 0 ) {
if ( p == 1 ) {
joinAttInd1 = getAttInd ( table1Schema, table1JoinAttName ) ;
}
BufferedReader buff = new BufferedReader ( fr2 ) ;
String nxtLine;
StringBuffer sbb = new StringBuffer ( ) ;
String record2 [ ] = null;
int pp = 0;
while ( ( ( nxtLine = buff.readLine ( ) ) != null ) ) {
sbb.delete ( 0, sbb.capacity ( ) ) ;
sbb.append ( nxtLine ) ;
StringTokenizer stt = new StringTokenizer ( sbb.toString ( ) ,
"," ) ;
if ( pp == 0 )
table2Schema = new String [ stt.countTokens ( ) ] ;
int ii = 0;
record2 = new String [ stt.countTokens ( ) ] ;
while ( stt.hasMoreElements ( ) ) {
String s = new String ( stt.nextToken ( ) .toString ( ) .trim ( ) ) ;
// System.out.println ( s ) ;
if ( pp == 0 ) {
table2Schema [ ii ] = s;
} else {
record2 [ ii ] = s;
}
ii++;
}
String [ ] t1s = table1Schema;
String [ ] t2s = table2Schema;
// printing output header
if ( ( p == 1 ) && ( pp == 0 ) ) {
for ( int cnt = 0; cnt < table1Schema.length; cnt++ )
System.out.print ( table1Schema [ cnt ] + "\t\t" ) ;
for ( int cnt = 0; cnt < table2Schema.length; cnt++ )
System.out.print ( table2Schema [ cnt ] + "\t\t" ) ;
System.out.println ( ) ;
}
if ( pp == 1 ) {
joinAttInd2 = getAttInd ( table2Schema, table2JoinAttName ) ;
}
// int c = 0;
if ( pp > 0 ) {
String st1 = record1 [ joinAttInd1 ] ;
String st2 = record2 [ joinAttInd2 ] ;
if ( ( st1.compareTo ( st2 ) ) == 0 ) {
for ( int cnt = 0; cnt < record1.length; cnt++ )
System.out.print ( record1 [ cnt ] + "\t\t" ) ;
for ( int cnt = 0; cnt < record2.length; cnt++ )
System.out.print ( record2 [ cnt ] + "\t\t" ) ;
System.out.println ( ) ;
}
}
pp++;
}
// c++;
} // inner Table
fr2.close ( ) ;
p++;
} // Outer Table
}
// else {
// if ( p == 0 )
public static void main ( String [ ] args ) {
Scanner input = new Scanner ( System.in ) ;
try {
joinParameter ( ) ;
System.out
.print ( "Enter 1 to perform a hash join\nEnter 2 to perform a Nested Loop join " ) ;
int ch = input.nextInt ( ) ;
switch ( ch ) {
case 1: {
// elapsed time with System.nanoTime ( )
long startTime = System.nanoTime ( ) ;
Partition ( "DEPT.txt" ) ;
probe ( ht, "EMP.txt" ) ;
// elapsed time in nanoseconds, billionths of a second
long endTime = System.nanoTime ( ) - startTime;
break;
}
case 2: {
nestedLoopJoin ( "DEPT.txt", "EMP.txt" ) ;
break;
}
default: {
System.out.println ( "Enter a valid choice" ) ;
break;
}
}
} catch ( Exception e ) {
}
}
}
DocFlavor getDocFlavor()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Object getPrintData()
throws IOException
- See Also:
-
getRepresentationClassName()
, getDocFlavor()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Reader getReaderForText()
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
InputStream getStreamForBytes()
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples