View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.statement;
22  
23  import org.apache.hadoop.hbase.hbql.client.ExecutionResults;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.client.HRecord;
26  import org.apache.hadoop.hbase.hbql.client.HResultSet;
27  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
28  import org.apache.hadoop.hbase.hbql.impl.ParseException;
29  import org.apache.hadoop.hbase.hbql.impl.Query;
30  import org.apache.hadoop.hbase.hbql.parser.ParserUtil;
31  
32  import java.io.BufferedReader;
33  import java.io.FileNotFoundException;
34  import java.io.FileReader;
35  import java.io.IOException;
36  import java.io.PrintWriter;
37  import java.util.List;
38  
39  public class ImportStatement extends GenericStatement implements ConnectionStatement {
40  
41      private final String filename;
42  
43      public ImportStatement(final String filename) {
44          super(null);
45          this.filename = filename;
46      }
47  
48      private String getFilename() {
49          return filename;
50      }
51  
52      public ExecutionResults execute(final HConnectionImpl conn) {
53  
54          final ExecutionResults results = new ExecutionResults();
55  
56          boolean success;
57          try {
58              success = processInput(new PrintWriter(results.out), conn, readFile(this.getFilename()));
59          }
60          catch (IOException e) {
61              success = false;
62              results.out.println(e.getMessage());
63          }
64          results.setSuccess(success);
65  
66          return results;
67      }
68  
69      public static String readFile(final String filename) throws IOException {
70  
71          try {
72              final StringBuilder stmtBuffer = new StringBuilder();
73              final BufferedReader in = new BufferedReader(new FileReader(filename));
74              String str;
75              while ((str = in.readLine()) != null)
76                  stmtBuffer.append(str);
77              in.close();
78              return stmtBuffer.toString();
79          }
80          catch (FileNotFoundException e) {
81              throw new IOException("Cannot find file: " + filename);
82          }
83          catch (IOException e) {
84              throw new IOException("Unable to read file: " + filename + " - " + e.getMessage());
85          }
86      }
87  
88      public static boolean processInput(final PrintWriter out,
89                                         final HConnectionImpl conn,
90                                         final String str) {
91  
92          try {
93              final List<HBqlStatement> stmtList = ParserUtil.parseConsoleStatements(str);
94  
95              for (final HBqlStatement stmt : stmtList) {
96                  if (stmt instanceof SelectStatement)
97                      processSelect(out, conn, (SelectStatement)stmt);
98                  else if (stmt instanceof ConnectionStatement)
99                      out.println(((ConnectionStatement)stmt).evaluatePredicateAndExecute(conn));
100                 else if (stmt instanceof NonConnectionStatement)
101                     out.println(((NonConnectionStatement)stmt).execute());
102                 else
103                     out.println("Unsupported statement type: " + stmt.getClass().getSimpleName() + " - " + str);
104             }
105         }
106         catch (ParseException e) {
107             out.println(e.getErrorMessage());
108             return false;
109         }
110         catch (HBqlException e) {
111             out.println("Error in statement: " + str);
112             out.println(e.getMessage());
113             return false;
114         }
115         finally {
116             out.flush();
117         }
118 
119         return true;
120     }
121 
122     private static void processSelect(final PrintWriter out,
123                                       final HConnectionImpl conn,
124                                       final SelectStatement selectStatement) throws HBqlException {
125 
126         // selectStatement.validate(connection);
127 
128         final Query<HRecord> query = Query.newQuery(conn, selectStatement, HRecord.class);
129         final HResultSet<HRecord> results = query.newResultSet(false);
130         for (final HRecord rec : results) {
131             for (final String columnName : rec.getColumnNameList()) {
132                 out.println(columnName + ": " + rec.getCurrentValue(columnName));
133             }
134         }
135     }
136 
137     public static String usage() {
138         return "IMPORT file_name";
139     }
140 }