Java连接阿里云HBase示例
2024-09-05 04:40:19
使用前要在阿里云的 HBase控制台中点击“修改网络白名单”,然后将你的ip地址(会有提示的)添加到网络白名单中,这样以后才能访问。
所需依赖:
<dependencies>
<dependency>
<groupId>com.aliyun.hbase</groupId>
<artifactId>alihbase-client</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
官方示例代码:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* Created by leibiao on 2017/3/6.
*/
public class HBaseDemo {
private static final String TABLE_NAME = "mytable";
private static final String CF_DEFAULT = "cf";
public static final byte[] QUALIFIER = "col1".getBytes();
private static final byte[] ROWKEY = "rowkey1".getBytes();
public static void main(String[] args) {
Configuration config = HBaseConfiguration.create();
String zkAddress = "xxxxxxxxx";
config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
Connection connection = null;
try {
connection = ConnectionFactory.createConnection(config);
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
tableDescriptor.addFamily(new HColumnDescriptor(CF_DEFAULT));
System.out.print("Creating table. ");
Admin admin = connection.getAdmin();
admin.createTable(tableDescriptor);
System.out.println(" Done.");
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
try {
Put put = new Put(ROWKEY);
put.addColumn(CF_DEFAULT.getBytes(), QUALIFIER, "this is value".getBytes());
table.put(put);
Get get = new Get(ROWKEY);
Result r = table.get(get);
byte[] b = r.getValue(CF_DEFAULT.getBytes(), QUALIFIER); // returns current version of value
System.out.println(new String(b));
} finally {
if (table != null) table.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
其中 zkAddress 对应 你阿里云 HBase控制台 中 ZK链接地址(外网地址,需要阿里云发行的客户端。参考文档:https://help.aliyun.com/document_detail/53718.html) 对应的那个url链接。
最新文章
- angularJS中的ui-router和ng-grid模块
- 第3章 jQuery的DOM操作
- demo03linearlayoutdemo;
- 在centos6.5中安装reids
- 如何获取Android系统中申请对象的信息
- 总结几种C#窗体间通讯的处理方法
- html表格标签与属性
- iOS 按住Home键的代理
- FileUpload类别FileUpload1.FileName和FileUpload1.PostedFile.FileName差异
- Java之面向对象例子(三) 多态,重写,重载,equals()方法和toString()方法的重写
- docker~Dockerfile方式建立镜像HelloWorld
- 【转】adb操作命令详解及大全
- ARM中几个典型的汇编指令解析
- ASP.NET MVC 4 (六) 帮助函数
- 能改变this各种情况下的总结,还有没有总结到的,请留言!!
- BZOJ 3585 mex
- spring mvc 使用kaptcha配置生成验证码实例
- onkeyup小练习
- vue时间格式化
- spring的bean容器加载
热门文章
- YOLO---Darknet下的 GPU vs CPU 速度
- @@identity, scope_identity ident_current 的区别
- temp数据预处理--以24h为周期的序列
- thinkphp session跨域
- Java锁--共享锁和ReentrantReadWriteLock
- ZooKeeper某一QuorumPeerMain挂了
- MongoDB 运维实总结
- set/unset
- qt 给父窗体设置样式不影响子控件样式以及子控件设置透明
- java.security.InvalidKeyException: Illegal key size or default parameters