hbase-java-client
来源:互联网 发布:python在spss中的应用 编辑:程序博客网 时间:2024/05/21 00:14
package hbase.test;import java.io.IOException;import org.apache.avro.generic.GenericData;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.KeyValue;import org.apache.hadoop.hbase.MasterNotRunningException;import org.apache.hadoop.hbase.ZooKeeperConnectionException;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.util.Bytes;public class HbaseTest { static Configuration configuration = null; static { configuration = HBaseConfiguration.create(); } //创建表并指定列簇 public static void createTable(String tableName, String cols[]) throws MasterNotRunningException, ZooKeeperConnectionException, IOException { HBaseAdmin baseAdmin = new HBaseAdmin(configuration); if (baseAdmin.tableExists(tableName)) { System.out.println("表【" + tableName + "】存在"); } else { HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); for (String s : cols) { HColumnDescriptor columnDescriptor = new HColumnDescriptor(s); tableDescriptor.addFamily(columnDescriptor); } baseAdmin.createTable(tableDescriptor); baseAdmin.close(); } } //删除表 public static void deleteTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException { HBaseAdmin admin = new HBaseAdmin(configuration); if (admin.tableExists(tableName)) { admin.disableTable(tableName); admin.deleteTable(tableName); } } //添加数据 /** * @param tableName 表名 * @param rowkey 行号 * @param colFamily 列簇 * @param column 列 * @param value 列值 */ public static void insertData(String tableName, String rowkey, String colFamily, String column, String value) throws IOException { HTable table = new HTable(configuration, tableName); Put put = new Put(Bytes.toBytes(rowkey)); put.add(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); table.close(); } //查询一条数据 public static void getOneByeRowkey(String tableName, String rowkey) throws IOException { HTable table = new HTable(configuration, tableName); Get get = new Get(Bytes.toBytes(rowkey)); Result result = table.get(get); for (KeyValue k : result.raw()) { System.out.println("行号:" + Bytes.toStringBinary(k.getRow())); System.out.println("时间戳:" + k.getTimestamp()); System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily())); System.out.println("列:" + Bytes.toStringBinary(k.getQualifier())); System.out.println("值:" + Bytes.toString(k.getValue())); } table.close(); } //查询指定数据 public static void scanData(String tableName) throws IOException { HTable table = new HTable(configuration, tableName); Scan scan = new Scan(); ResultScanner rs = table.getScanner(scan); for (Result result : rs) { for (KeyValue k : result.raw()) { System.out.println("行号:" + Bytes.toStringBinary(k.getRow())); System.out.println("时间戳:" + k.getTimestamp()); System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily())); System.out.println("列:" + Bytes.toStringBinary(k.getQualifier())); System.out.println("值:" + Bytes.toString(k.getValue())); } } table.close(); } //删除一条数据 @SuppressWarnings("empty-statement") public static void deleteByRow(String tableName, String rowkey) throws IOException { HTable table = new HTable(configuration, tableName); //删除一条数据 Delete delete = new Delete(Bytes.toBytes(rowkey)); table.delete(delete); //删除多条数据 String[] rowkeys = {}; java.util.List<Delete> list = new java.util.ArrayList<Delete>(); for (String rk : rowkeys) { Delete d = new Delete(Bytes.toBytes(rk)); list.add(d); } table.delete(list); // table.close(); } public static void main(String[] args) throws ZooKeeperConnectionException, IOException { String tableName = "mytest"; String cols[] = {"a", "b", "c"}; // createTable(tableName, cols); // insertData(tableName, "1", "a", "abc", "11111"); // getOneByeRowkey(tableName, "1"); //scanData(tableName); // deleteByRow(tableName, "1"); // scanData(tableName); }}
0 0
- hbase java client 操作
- hbase-java-client
- HBase Java Client
- hbase java client无法连接 hbase server
- HBase java client配置参数
- java client 连接hbase报错,超时
- HBase Client
- 【HBase】org.apache.zookeeper.client.ZooKeeperSaslClient: SecurityException: java.lang.SecurityExcepti
- HBase Client: Delete Method
- HBase client总览
- Hbase Client Test Case
- HBase体系结构02(Client)
- HBase client API 实例
- HBase Client API简单
- 集群外访问Hbase – access Hbase use a java client program
- JAVA api 远程hbase出错 org.apache.hadoop.hbase.client.RetriesExhaustedException
- JAVA API远程hbase出错org.apache.hadoop.hbase.client.RetriesExhaustedException
- hadoop、zookeeper、hbase整合,使用java查询hbase数据报错org.apache.hadoop.hbase.client.NoServerForRegionException:
- java 压缩图片 添加水印
- OC_构造方法,自定义构造方法,分类,类的深入研究,description,SEL数据类型笔记
- Oracle INV Debug Log 路径问题
- 发个好的护醛污时代
- js判断各个浏览器的方法
- hbase-java-client
- 2014年下半年计划
- 杭电2135 仙人球的残影
- 基于UDP协议的局域网网络聊天工具
- 成都传智播客Java/PHP培训就业率高
- const限定符和指针的用法实例
- 使用Linkify来检测一些链接
- WPS分为PBC(BUTTON)和PIN两种方式
- 左中右布局经典案例