Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hbase-table.cc
Go to the documentation of this file.
1 // Copyright 2013 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "runtime/hbase-table.h"
16 
17 #include <string>
18 
19 #include "runtime/runtime-state.h"
20 #include "util/jni-util.h"
21 
22 #include "common/names.h"
23 
24 namespace impala {
25 
26 jclass HBaseTable::htable_cl_ = NULL;
27 jmethodID HBaseTable::htable_ctor_ = NULL;
28 jmethodID HBaseTable::htable_close_id_ = NULL;
29 jmethodID HBaseTable::htable_get_scanner_id_ = NULL;
30 jmethodID HBaseTable::htable_put_id_ = NULL;
31 
32 jclass HBaseTable::bytes_cl_ = NULL;
33 jmethodID HBaseTable::bytes_to_bytes_id_ = NULL;
34 
35 HBaseTable::HBaseTable(const string& table_name,
36  jobject& conf, jobject& executor )
37  : table_name_(table_name),
38  conf_(conf),
39  executor_(executor),
40  htable_(NULL) {
41 }
42 
44  DCHECK(htable_ == NULL) << "Must call Close()";
45 }
46 
48  // If this has already been closed then return out early.
49  if (htable_ == NULL) return;
50 
51  JNIEnv* env = getJNIEnv();
52  if (env == NULL) {
53  state->LogError(ErrorMsg(
54  TErrorCode::GENERAL, "HBaseTable::Close(): Error creating JNIEnv"));
55  } else {
56  env->CallObjectMethod(htable_, htable_close_id_);
57  Status s = JniUtil::GetJniExceptionMsg(env, "HBaseTable::Close(): ");
58  if (!s.ok()) state->LogError(s.msg());
59  env->DeleteGlobalRef(htable_);
60 
61  s = JniUtil::GetJniExceptionMsg(env, "HBaseTable::Close(): ");
62  if (!s.ok()) state->LogError(s.msg());
63  }
64 
65  htable_ = NULL;
66 }
67 
69  JNIEnv* env = getJNIEnv();
70  JniLocalFrame jni_frame;
71  RETURN_IF_ERROR(jni_frame.push(env));
72  if (env == NULL) return Status("Error creating JNIEnv");
73 
74  // Get the Java string for the table name
75  jstring jtable_name_string = env->NewStringUTF(table_name_.c_str());
77  // Use o.a.h.hbase.util.Bytes.toBytes to convert into a byte array.
78  jobject jtable_name = env->CallStaticObjectMethod(bytes_cl_,
79  bytes_to_bytes_id_, jtable_name_string);
81 
82  // Create the HTable.
83  jobject local_htable = env->NewObject(htable_cl_,
84  htable_ctor_, conf_, jtable_name, executor_);
86 
87  // Make sure the GC doesn't remove the HTable until told to.
89  return Status::OK;
90 }
91 
93  JNIEnv* env = getJNIEnv();
94  if (env == NULL) {
95  return Status("Failed to get/create JVM");
96  }
97 
98  // Bytes used to get String -> Java Byte Array the same way HBase will.
100  JniUtil::GetGlobalClassRef(env, "org/apache/hadoop/hbase/util/Bytes",
101  &bytes_cl_));
102 
103  bytes_to_bytes_id_ = env->GetStaticMethodID(bytes_cl_, "toBytes",
104  "(Ljava/lang/String;)[B");
105  RETURN_ERROR_IF_EXC(env);
106 
107  // HTable
109  JniUtil::GetGlobalClassRef(env, "org/apache/hadoop/hbase/client/HTable",
110  &htable_cl_));
111 
112  htable_ctor_ = env->GetMethodID(htable_cl_, "<init>",
113  "(Lorg/apache/hadoop/conf/Configuration;"
114  "[BLjava/util/concurrent/ExecutorService;)V");
115  RETURN_ERROR_IF_EXC(env);
116 
117  htable_close_id_ = env->GetMethodID(htable_cl_, "close", "()V");
118  RETURN_ERROR_IF_EXC(env);
119 
120  htable_get_scanner_id_ = env->GetMethodID(htable_cl_, "getScanner",
121  "(Lorg/apache/hadoop/hbase/client/Scan;)"
122  "Lorg/apache/hadoop/hbase/client/ResultScanner;");
123  RETURN_ERROR_IF_EXC(env);
124 
125  htable_put_id_ = env->GetMethodID(htable_cl_, "put",
126  "(Ljava/util/List;)V");
127  RETURN_ERROR_IF_EXC(env);
128 
129  return Status::OK;
130 }
131 
133  jobject* result_scanner) {
134  JNIEnv* env = getJNIEnv();
135  if (env == NULL) return Status("Error creating JNIEnv");
136 
137  (*result_scanner) = env->CallObjectMethod(htable_,
138  htable_get_scanner_id_, scan);
139  RETURN_ERROR_IF_EXC(env);
140  return Status::OK;
141 }
142 
143 Status HBaseTable::Put(const jobject& puts_list) {
144  JNIEnv* env = getJNIEnv();
145  if (env == NULL) return Status("Error creating JNIEnv");
146 
147  env->CallObjectMethod(htable_, htable_put_id_, puts_list);
148  RETURN_ERROR_IF_EXC(env);
149 
150  // TODO(eclark): FlushCommits
151  return Status::OK;
152 }
153 
154 } // namespace impala
static jmethodID htable_put_id_
htable.put(List<Put> puts
Definition: hbase-table.h:70
static jmethodID htable_get_scanner_id_
htable.getScannerId(Scan)
Definition: hbase-table.h:67
#define RETURN_IF_ERROR(stmt)
some generally useful macros
Definition: status.h:242
Status Put(const jobject &puts_list)
Send an list of puts to hbase through an HTable.
Definition: hbase-table.cc:143
static Status InitJNI()
Call this to initialize the HBase HTable jni references.
Definition: hbase-table.cc:92
static jclass bytes_cl_
Bytes class and static methods.
Definition: hbase-table.h:73
HBaseTable(const std::string &table_name, jobject &conf, jobject &executor)
static Status LocalToGlobalRef(JNIEnv *env, jobject local_ref, jobject *global_ref)
Definition: jni-util.cc:67
void Close(RuntimeState *state)
Close and release the HTable wrapped by this class.
Definition: hbase-table.cc:47
std::string table_name_
Definition: hbase-table.h:52
bool LogError(const ErrorMsg &msg)
static Status GetJniExceptionMsg(JNIEnv *env, bool log_stack=true, const std::string &prefix="")
Definition: jni-util.cc:161
static jmethodID htable_ctor_
new HTable(Configuration, ExecutorService)
Definition: hbase-table.h:61
static jmethodID bytes_to_bytes_id_
Bytes.toBytes(String)
Definition: hbase-table.h:76
Status push(JNIEnv *env, int max_local_ref=10)
Definition: jni-util.cc:34
static jclass htable_cl_
org.apache.hadoop.hbase.client.HTable
Definition: hbase-table.h:58
#define RETURN_ERROR_IF_EXC(env)
Definition: jni-util.h:99
static const Status OK
Definition: status.h:87
static Status GetGlobalClassRef(JNIEnv *env, const char *class_str, jclass *class_ref)
Definition: jni-util.cc:56
ImpaladQueryExecutor * executor_
execution state of coordinator fragment
Definition: expr-test.cc:71
const ErrorMsg & msg() const
Returns the error message associated with a non-successful status.
Definition: status.h:189
bool ok() const
Definition: status.h:172
Status GetResultScanner(const jobject &scan, jobject *result_scanner)
Definition: hbase-table.cc:132
static jmethodID htable_close_id_
htable.close()
Definition: hbase-table.h:64
JNIEnv * getJNIEnv(void)
C linkage for helper functions in hdfsJniHelper.h.