com.hydraql.template.Query Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hydraql.template;
import com.hydraql.adapter.HBaseTableAdapter;
import com.hydraql.common.meta.HBaseField;
import com.hydraql.common.meta.HBaseMetaFactory;
import com.hydraql.common.meta.HBaseTableSchema;
import com.hydraql.common.query.GetRowParam;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;
import java.util.List;
/**
* @author jielongping
*/
public class Query {
private final Class domainModelClass;
private final Configuration configuration;
private final HBaseTableAdapter tableAdapter;
private final List rows;
private final String family;
private final List qualifiers;
private Query(Builder builder) {
this.domainModelClass = builder.domainModelClass;
this.configuration = builder.configuration;
this.tableAdapter = new HBaseTableAdapter(this.configuration);
this.rows = builder.rows;
this.family = builder.family;
this.qualifiers = builder.qualifiers;
}
public static Query.Builder of(Class domainModelClass) {
return new Builder<>(domainModelClass);
}
public T get() {
if (this.rows.isEmpty()) {
return null;
}
HBaseTableSchema tableSchema = HBaseMetaFactory.getInstance().create(this.getModelClass());
HBaseField rowField = tableSchema.getFields().get(0);
byte[] row = rowField.getRow(rows.get(0));
// todo 此处需优化
GetRowParam getRowParam = GetRowParam.of(Bytes.toString(row)).family(this.getFamily())
.qualifiers(this.getQualifiers()).build();
return tableAdapter.getRow(getRowParam, this.getModelClass());
}
public static class Builder {
private final Class domainModelClass;
private Configuration configuration;
private final List rows;
private String family;
private final List qualifiers;
private Builder(final Class domainModelClass) {
this.domainModelClass = domainModelClass;
this.configuration = HBaseConfiguration.create();
this.rows = new ArrayList<>();
this.family = domainModelClass.getSimpleName();
this.qualifiers = new ArrayList<>();
}
public Builder config(Configuration configuration) {
this.configuration = configuration;
return this;
}
public Builder config(String key, String value) {
configuration.set(key, value);
return this;
}
public Builder withRow(String rowKey) {
this.rows.add(rowKey);
return this;
}
public Builder withRows(List rows) {
this.rows.addAll(rows);
return this;
}
public Builder addFamily(String family) {
this.family = family;
return this;
}
public Builder addQualifier(String qualifier) {
this.qualifiers.add(qualifier);
return this;
}
public Builder addQualifiers(List qualifiers) {
this.qualifiers.addAll(qualifiers);
return this;
}
public Query build() {
return new Query<>(this);
}
}
private Class getModelClass() {
return domainModelClass;
}
public Configuration getConfiguration() {
return configuration;
}
public String getFamily() {
return family;
}
public List getQualifiers() {
return qualifiers;
}
}