1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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 }