熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java應用程序put操作

2022-06-13   來源: Java核心技術 

  創建表

  put操作

  public class myHbase {

  private static Configuration conf= null;

  static {

  conf=HBaseConfigurationcreate()

  }

  /**

  * column familys add column <>hbase columnName

  * @param tableName

  * @param familys

  * @throws Exception

  */

  public void createTable(String tableNameString[] familys) throws Exception{

  HBaseAdmin admin = new HBaseAdmin(conf)

  if(!admintableExists(tableName)){

  HTableDescriptor desc = new HTableDescriptor(tableName)

  for (int i = ; i < familyslength; i++) {

  descaddFamily(new HColumnDescriptor(familys[i]))

  }

  admincreateTable(desc)

  }else{

  Systemoutprintln(the table:+ tableName + exsists

  };

  }

  /**

  * @param tableName

  * @param rowKey

  * @param family

  * @param qualifier

  * @param value

  * @throws Exception

  */

  public void putData(String tableNameString rowKey String family

  String qualifier String value) throws Exception {

  HTable ht = new HTable(conf tableName)

  Put put = new Put(BytestoBytes(rowKey))

  putadd(BytestoBytes(family) BytestoBytes(qualifier) BytestoBytes(value))

  htput(put)

  }

  public static void main(String [] agrs) throws Exception{

  myHbase h= new myHbase()

  String [] familys = {familyfamily};

  hcreateTable(mytable familys)

  hputData(mytable row family qualifier:name value

  }

  }

  運行java應用程序登錄hbase shell 發現可以get和scan到數據了

  hbase(main):> scan mytable

  ROW                   COLUMN+CELL

  row                  column=family:qualifier:name timestamp= v

  alue=value

   row(s) in seconds

  hbase(main):> get mytablerowfamily:qualifier:name

  COLUMN                CELL

  family:qualifier:na timestamp= value=value

  me

   row(s) in seconds


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26356.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.