• 沒有找到結果。

使用Java调用REST API,请按照以下步骤。(可参见样例代码中RestExample部分代 码)。

1. 进行kerberos认证(未开启Kerberos认证集群可以跳过此步骤)

2. 创建一个org.apache.hadoop.hbase.rest.client.Cluster类的集群对象,通过调用集 群类的add方法和REST server的集群IP和端口来添加集群。

Cluster cluster = new Cluster();

cluster.add("10.10.10.10:21309");

3. 使用在步骤2中添加的集群初始化类

“org.apache.hadoop.hbase.rest.client.Client”的客户端对象,调用doAs来操作 HBase。

Client client = new Client(cluster, true);

UserGroupInformation.getLoginUser().doAs(new PrivilegedAction() { public Object run() {

String namespacePath = "/namespaces/" + "nameSpaceName";

response = client.get(namespacePath);

System.out.println(Bytes.toString(response.getBody()));

● 创建或修改命名空间

1. 在创建或修改命名空间时,都是使用NamespacesInstanceModel创建模型并使用 buildTestModel()方法构建模型,如下所示。这里创建模型,使模型包含要创建的 命名空间的属性信息。

Map<String, String> NAMESPACE1_PROPS = new HashMap<String, String>();

NAMESPACE1_PROPS.put("key1", "value1");

NamespacesInstanceModel model = buildTestModel(NAMESPACE1,NAMESPACE1_PROPS);

private static NamespacesInstanceModel buildTestModel(String namespace, Map<String, String>

properties) {

NamespacesInstanceModel model = new NamespacesInstanceModel();

for (String key : properties.keySet()) { model.addProperty(key, properties.get(key));

}

return model;

String namespacePath = "/namespaces/" + "nameSpaceName";

response = client.post(namespacePath, Constants.MIMETYPE_XML, toXML(model));

private static byte[] toXML(NamespacesInstanceModel model) throws JAXBException { StringWriter writer = new StringWriter();

context.createMarshaller().marshal(model, writer);

return Bytes.toBytes(writer.toString());

}

2. 在使用xml方式进行Get请求时,可使用如下所示的fromXML()方法,从响应中获 取模型,并从模型中获取创建的命名空间的名称。

private static <T> T fromXML(byte[] content) throws JAXBException {

return (T) context.createUnmarshaller().unmarshal(new ByteArrayInputStream(content));

}

ObjectMapper jsonMapper = new JacksonProvider().locateMapper(NamespacesInstanceModel.class, MediaType.APPLICATION_JSON_TYPE);

Response response;

String namespacePath = "/namespaces/" + "nameSpaceName";

String jsonString = jsonMapper.writeValueAsString(model);

response = client.put(namespacePath, Constants.MIMETYPE_JSON, Bytes.toBytes(jsonString));

2. 在使用json方式进行Get请求时,jsonMapper具有如下所示的readValue()方法,

用于从响应中获取模型,并能从模型中获取创建的命名空间。

jsonMapper.readValue(response.getBody(), NamespacesInstanceModel.class);

/*Here second argument should be according to API, if its **related to table it should be TableSchemaModel.class*/

String namespacePath = "/namespaces/" + "nameSpaceName";

response = client.put(namespacePath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());

model.getObjectFromMessage(response.getBody());

2. 在使用protobuf的方式进行Get请求时,可使用如下所示的

getObjectFromMessage 方法,用于从响应中获取模型,并从模型中获取创建的 命名空间。

model.getObjectFromMessage(response.getBody());

3.7 FAQ

3.7.1 运行 HBase 应用开发程序产生异常

提示信息包含org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory的 解决办法

步骤1 检查应用开发工程的配置文件hbase-site.xml中是否包含配置项 hbase.rpc.controllerfactory.class。

<name>hbase.rpc.controllerfactory.class</name>

<value>org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory</value>

步骤2 如果当前的应用开发工程配置项中包含该配置项,则应用开发程序还需要引入Jar包

“phoenix-core-4.4.0-HBase-1.0.jar”。此Jar包可以从HBase客户端安装目录下的

“HBase/hbase/lib”获取。

步骤3 如果不想引入该Jar包,请将应用开发工程的配置文件“hbase-site.xml”中的配置

“hbase.rpc.controllerfactory.class”删除掉。

----结束

3.7.2 bulkload 和 put 应用场景

HBase支持使用bulkload和put方式加载数据,在大部分场景下bulkload提供了更快的 数据加载速度,但bulkload并不是没有缺点的,在使用时需要关注bulkload和put适合 在哪些场景使用。

bulkload是通过启动MapReduce任务直接生成HFile文件,再将HFile文件注册到 HBase,因此错误的使用bulkload会因为启动MapReduce任务而占用更多的集群内存 和CPU资源,也可能会生成大量很小的HFile文件频繁的触发Compaction,导致查询速 度急剧下降。

错误的使用put,会造成数据加载慢,当分配给RegionServer内存不足时会造成 RegionServer内存溢出从而导致进程退出。

下面给出bulkload和put适合的场景:

● bulkload适合的场景:

– 大量数据一次性加载到HBase。

– 对数据加载到HBase可靠性要求不高,不需要生成WAL文件。

– 使用put加载大量数据到HBase速度变慢,且查询速度变慢时。

– 加载到HBase新生成的单个HFile文件大小接近HDFS block大小。

● put适合的场景:

– 每次加载到单个Region的数据大小小于HDFS block大小的一半。

– 数据需要实时加载。

– 加载数据过程不会造成用户查询速度急剧下降。

3.8 开发规范

3.8.1 规则