2 * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
3 * See accompanying LICENSE file.
10 import com
.yahoo
.pig
.impl
.PigContext
;
11 import com
.yahoo
.pig
.PigServer
.ExecType
;
12 import com
.yahoo
.pig
.impl
.logicalLayer
.LogicalPlan
;
13 import com
.yahoo
.pig
.impl
.logicalLayer
.LogicalPlanBuilder
;
14 import com
.yahoo
.pig
.impl
.logicalLayer
.parser
.ParseException
;
15 import com
.yahoo
.pig
.impl
.logicalLayer
.parser
.QueryParser
;
16 import com
.yahoo
.pig
.impl
.physicalLayer
.IntermedResult
;
18 public class StandAloneParser
{
19 public static ClassLoader classloader
= LogicalPlanBuilder
.class.getClassLoader();
21 public static void main(String args
[]) throws IOException
{
23 Map
<String
, IntermedResult
> aliases
= new HashMap
<String
, IntermedResult
>();
25 BufferedReader in
= new BufferedReader(new InputStreamReader(System
.in
));
28 System
.out
.print("> ");
33 } catch (IOException e
) {
38 if (line
.toLowerCase().equals("quit")) break;
39 if (line
.toLowerCase().startsWith("#")) continue;
40 else tryParse(line
, aliases
);
46 private static void tryParse(String query
, Map
<String
, IntermedResult
> aliases
) {
47 if (query
.trim().equals(""))
49 ByteArrayInputStream in
= new ByteArrayInputStream(query
.getBytes());
50 QueryParser parser
= new QueryParser(in
, new PigContext(ExecType
.LOCAL
), aliases
);
52 LogicalPlan lp
= parser
.Parse();
53 System
.out
.println("--- Query parsed successfully ---");
54 if (lp
.alias
!= null) aliases
.put(lp
.alias
, new IntermedResult(lp
));
55 System
.out
.print("Current aliases: ");
56 for (Iterator
<String
> it
= aliases
.keySet().iterator(); it
.hasNext(); ) {
57 String alias
= it
.next();
58 IntermedResult ir
= aliases
.get(alias
);
59 System
.out
.print(alias
+ "->" + ir
.lp
.root().outputSchema());
60 if (it
.hasNext()) System
.out
.print(", \n");
61 else System
.out
.print("\n");
63 } catch (ParseException e
) {
64 System
.out
.println("Parse error: " + e
);