当前位置: 首页 > news >正文

镇江住房建设网站/网站页面关键词优化

镇江住房建设网站,网站页面关键词优化,网络营销方式选择考虑的因素,贵阳市房地产交易平台上线原文: http://www.cnblogs.com/NicholasLee/archive/2012/09/14/2684815.html HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,下面看一下Java api创建、删除表,及记录的增删改查操作&#xf…

原文:

http://www.cnblogs.com/NicholasLee/archive/2012/09/14/2684815.html

HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,下面看一下Java api创建、删除表,及记录的增删改查操作:

复制代码
package hbase;import java.io.IOException;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.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.HTablePool;
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 HBaseTest2 {// 声明静态配置static Configuration conf = null;static final HTablePool tablePool;static {conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "libin2");tablePool = new HTablePool(conf, 10);}/** 创建表* * @tableName 表名* * @family 列族列表*/public static void creatTable(String tableName, String[] family)throws Exception {HBaseAdmin admin = new HBaseAdmin(conf);HTableDescriptor desc = new HTableDescriptor(tableName);for (int i = 0; i < family.length; i++) {desc.addFamily(new HColumnDescriptor(family[i]));}if (admin.tableExists(tableName)) {System.out.println("table Exists!");System.exit(0);} else {admin.createTable(desc);System.out.println("create table Success!");}}/** 为表添加数据(适合知道有多少列族的固定表)* * @rowKey rowKey* * @tableName 表名* * @column1 第一个列族列表* * @value1 第一个列的值的列表* * @column2 第二个列族列表* * @value2 第二个列的值的列表*/public static void addData(String rowKey, String tableName,String[] column1, String[] value1, String[] column2, String[] value2)throws IOException {Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkeyHTable table = (HTable) tablePool.getTable(tableName);// 获取表HColumnDescriptor[] columnFamilies = table.getTableDescriptor() // 获取所有的列族
                .getColumnFamilies();for (int i = 0; i < columnFamilies.length; i++) {String familyName = columnFamilies[i].getNameAsString(); // 获取列族名if (familyName.equals("article")) { // article列族put数据for (int j = 0; j < column1.length; j++) {put.add(Bytes.toBytes(familyName),Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));}}if (familyName.equals("author")) { // author列族put数据for (int j = 0; j < column2.length; j++) {put.add(Bytes.toBytes(familyName),Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j]));}}}table.put(put);System.out.println("add data Success!");/** Put put = new Put(Bytes.toBytes("rowkey1"));* put.add(Bytes.toBytes("article"), Bytes.toBytes("title"),* Bytes.toBytes("Head First HBase")); put.add(Bytes.toBytes("article"),* Bytes.toBytes("content"), Bytes.toBytes(* "HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data."* )); put.add(Bytes.toBytes("article"), Bytes.toBytes("tags"),* Bytes.toBytes("Hadoop,HBase,NoSQL"));* put.add(Bytes.toBytes("author"), Bytes.toBytes("name"),* Bytes.toBytes("nicholas")); put.add(Bytes.toBytes("author"),* Bytes.toBytes("nickname"), Bytes.toBytes("lee")); HTable table = new* HTable(conf, Bytes.toBytes("blog2"));table.put(put);*/}/** 根据rwokey查询* * @rowKey rowKey* * @tableName 表名*/public static Result getResult(String tableName, String rowKey)throws IOException {Get get = new Get(Bytes.toBytes(rowKey));HTable table = (HTable) tablePool.getTable(tableName);// 获取表Result result = table.get(get);for (KeyValue kv : result.list()) {System.out.println("family:" + Bytes.toString(kv.getFamily()));System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));System.out.println("value:" + Bytes.toString(kv.getValue()));System.out.println("Timestamp:" + kv.getTimestamp());System.out.println("-------------------------------------------");}return result;}/** 遍历查询hbase表* * @tableName 表名*/public static void getResultScann(String tableName) throws IOException {Scan scan = new Scan();ResultScanner rs = null;HTable table = (HTable) tablePool.getTable(tableName);try {rs = table.getScanner(scan);for (Result r : rs) {for (KeyValue kv : r.list()) {System.out.println("family:"+ Bytes.toString(kv.getFamily()));System.out.println("qualifier:"+ Bytes.toString(kv.getQualifier()));System.out.println("value:" + Bytes.toString(kv.getValue()));System.out.println("timestamp:" + kv.getTimestamp());System.out.println("-------------------------------------------");}}} finally {rs.close();}}/** 查询表中的某一列* * @tableName 表名* * @rowKey rowKey*/public static void getResultByColumn(String tableName, String rowKey,String familyName, String columnName) throws IOException {HTable table = (HTable) tablePool.getTable(tableName);Get get = new Get(Bytes.toBytes(rowKey));get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); // 获取指定列族和列修饰符对应的列// assertThat(Bytes.toString(table.get(get).list().get(0).getValue()),is("一叶渡江"));Result result = table.get(get);for (KeyValue kv : result.list()) {System.out.println("family:" + Bytes.toString(kv.getFamily()));System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));System.out.println("value:" + Bytes.toString(kv.getValue()));System.out.println("Timestamp:" + kv.getTimestamp());System.out.println("-------------------------------------------");}}/** 更新表中的某一列* * @tableName 表名* * @rowKey rowKey* * @familyName 列族名* * @columnName 列名* * @value 更新后的值*/public static void updateTable(String tableName, String rowKey,String familyName, String columnName, String value)throws IOException {HTable table = (HTable) tablePool.getTable(tableName);Put put = new Put(Bytes.toBytes(rowKey));put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName),Bytes.toBytes(value));table.put(put);System.out.println("update table Success!");}/** 查询某列数据的多个版本* * @tableName 表名* * @rowKey rowKey* * @familyName 列族名* * @columnName 列名*/public static void getResultByVersion(String tableName, String rowKey,String familyName, String columnName) throws IOException {HTable table = (HTable) tablePool.getTable(tableName);Get get = new Get(Bytes.toBytes(rowKey));get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));get.setMaxVersions(5);Result result = table.get(get);for (KeyValue kv : result.list()) {System.out.println("family:" + Bytes.toString(kv.getFamily()));System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));System.out.println("value:" + Bytes.toString(kv.getValue()));System.out.println("Timestamp:" + kv.getTimestamp());System.out.println("-------------------------------------------");}/** List<?> results = table.get(get).list(); Iterator<?> it =* results.iterator(); while (it.hasNext()) {* System.out.println(it.next().toString()); }*/}/** 删除指定的列* * @tableName 表名* * @rowKey rowKey* * @familyName 列族名* * @columnName 列名*/public static void deleteColumn(String tableName, String rowKey,String falilyName, String columnName) throws IOException {HTable table = (HTable) tablePool.getTable(tableName);Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));deleteColumn.deleteColumns(Bytes.toBytes(falilyName),Bytes.toBytes(columnName));table.delete(deleteColumn);System.out.println(falilyName + ":" + columnName + "is deleted!");}/** 删除指定的列* * @tableName 表名* * @rowKey rowKey*/public static void deleteAllColumn(String tableName, String rowKey)throws IOException {HTable table = (HTable) tablePool.getTable(tableName);Delete deleteAll = new Delete(Bytes.toBytes(rowKey));table.delete(deleteAll);System.out.println("all columns are deleted!");}/** 删除表* * @tableName 表名*/public static void deleteTable(String tableName) throws IOException {HBaseAdmin admin = new HBaseAdmin(conf);admin.disableTable(tableName);admin.deleteTable(tableName);System.out.println(tableName + "is deleted!");}public static void main(String[] args) throws Exception {// 创建表/** String tableName = "blog2"; String[] family = { "article","author" };* creatTable(tableName,family);*/// 为表添加数据/** String[] column1 = { "title", "content", "tag" }; String[] value1 = {* "Head First HBase",* "HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data."* , "Hadoop,HBase,NoSQL" }; String[] column2 = { "name", "nickname" };* String[] value2 = { "nicholas", "lee" }; addData("rowkey1", "blog2",* column1, value1, column2, value2);*/// 删除一列// deleteColumn("blog2", "rowkey1", "author", "nickname");// 删除所有列//deleteAllColumn("blog2", "rowkey1");//删除表//deleteTable("blog2");// 查询// getResult("blog2", "rowkey1");// 查询某一列的值// getResultByColumn("blog2", "rowkey1", "author", "name");// updateTable("blog2", "rowkey1", "author", "name","bin");// getResultByColumn("blog2", "rowkey1", "author", "name");// 遍历查询// getResultScann("blog2");// 查询某列的多版本// getResultByVersion("blog2", "rowkey1", "author", "name");
    }
}
复制代码

以上就是通过Java Api与HBase交互的一些常用的操作。


http://www.lbrq.cn/news/796249.html

相关文章:

  • 做网站的说3年3年包括什么/汕头seo全网营销
  • 网站开发兼职合同/网络推广网站排名
  • 小程序商城制作平台/厦门seo培训学校
  • 二级建造师官网查询系统/优化大师最新版下载
  • 武汉建设网站哪家好/网站联盟推广
  • 网站开发指的是什么/百度发作品入口在哪里
  • 长春网站建设及推广/北京网络营销推广公司
  • 兰州网站建设报价/房地产销售技巧和话术
  • 教育网站建设策划书/郑州seo网站关键词优化
  • ui设计速成培训机构/淘宝关键词优化软件
  • 网站没排名怎么办/网站制作多少钱一个
  • 做图片的网站都有哪些/南通网络推广
  • 石家庄 外贸网站建设公司/汽车行业网站建设
  • wordpress 邮件美化/aso优化服务平台
  • 纯ajax网站如何做seo/免费seo培训
  • 昆明小程序定制开发/清理优化大师
  • WordPress主题zero/优化推广服务
  • 广州家居网站设计/百度搜索智能精选
  • 江阴公司企业网站建设/济南做seo的公司排名
  • 中铁建设集团集采网站/注册一个公司网站需要多少钱
  • 商城网站建设平台/竞价防恶意点击
  • 互联网做网站/1个百度指数代表多少搜索
  • 效果图制作软件app/seo 优化思路
  • 西宁做手机网站的公司/太原网站建设开发
  • 如何设计公司网站/潍坊网站模板建站
  • 公司网站建设建议/关键词数据分析工具有哪些
  • 做3dmax效果图任务的网站/水果网络营销策划书
  • 做兽设的网站/windows优化大师是自带的吗
  • 短网址在线生成短网址/上海seo搜索优化
  • 石家庄的网站建设公司哪家好/广告优化师是做什么的
  • Qt-vs加载exe图标
  • Java 大视界 -- Java 大数据在智能教育学习资源个性化推荐与学习路径动态调整中的深度应用(378)
  • 动态置信度调优实战:YOLOv11多目标追踪精度跃迁方案(附完整代码)
  • 实战解析:编程式事务在实际开发中的典型应用场景
  • Java 中的 HashMap.merge() 方法详解
  • 除数博弈(动态规划)