Small note -------- hbase database java API common methods and case

HBaseAdmin categories: management hbase table information database, 'create table, drop table, the options listed in the table, the table is valid / invalid, add or remove columns clusters';

 

 

 

EG: 

// Create a connection using the factory pattern 
 Val conn: Connection = ConnectionFactory.createConnection (config)
 // instantiate instances hbase managers 
 Val ADMIN: Admin = conn.getAdmin 

// Delete table 
admin.deleteTable (TableName.valueOf ( " T1 " ))

 

HbaseConfiguration class inherits from the Configuration class: the hbase added to the configuration profile

 

//配置hbase资源配置
val config: Configuration =HBaseConfiguration.create()
config.set("hbase.zookeeper.quorum" , "df1:2181,df2:2181,df3:2181")
config.set("hbase.master" , "df1:60000")
config.set("hbase.zookeeper.property.clientPort" , "2181")

 

 

Table class: and for hbase Table direct communication;

In 0.94 is not recommended to use after release

val HTable = new HTable(conf, Bytes.toBytes(tablename))

But the use of

val table = Connection.getTable(TableName.valueOf(driver_info))

 

 

 

: EG 

/ * * 
  Use new HTable Connection.getTable after use before * 0.94 (TableName the TableName); 
  * used to communicate directly with hbase table 
  * / 
DEF tableoperation (): Unit = {
   // Get an instance of the table 
  val tab = conn. getTable (TableName.valueOf ( " citycode " ))
   // check whether the hand 99 rowkey values exist in the table 
  Val Q: Boolean = tab.exists ( new new the Get (Bytes.toBytes ( " 99 " )))
   // check row / family / qualifier match with the database. If so, then submit the operation to put the server 
  Val sss = tab.checkAndPut (Bytes.toBytes ( " 99 " ), Bytes.toBytes ( " MM "), Bytes.toBytes ( " citycode " ), Bytes.toBytes ( " 3213 " ), PUT ())
   // release all the resources or pending internal buffer update 
  tab.close ()
 // get rowkey of column families MM the value of the column name id value 
  Val GET = new new the Get (Bytes.toBytes ( " rowKey " )) 
  Val the Result: the Result . = the Tab GET ( GET ) 
  Val bytes = result.getValue (Bytes.toBytes ( " MM " ), bytes .toBytes ( " the above mentioned id " )) 
  println (Bytes.toString (bytes)) 
  //Examples of scanner acquired MM 
  Val scanner: ResultScanner = tab.getScanner (Bytes.toBytes ( " MM " ))
   // Get the table HTableDescriptor Example 
  Val tableDescriptor: HTableDescriptor = tab.getTableDescriptor ()
   // data put discharge Example the table 
  tab.put (PUT ()) 
}

 

 

HTableDescriptor : class hbase details table

 

 

 

EG: 

// create HTableDescriptor table table describes 
 Val tabledescriptor = new new HTableDescriptor (TableName.valueOf ( " order_info " ))
 // to the table to add a column cluster 
tabledescriptor.addFamily ( new new HColumnDescriptor ( " CC " ))
 // delete the row CC cluster 
tabledescriptor .removeFamily (Bytes.toBytes ( " CC " ))
 // get table name 
Val TableName = tabledescriptor.getTableName

 

 

HColumnDescriptor categories: Column cluster description

 

 

 

Eg:

 val columndescriptor = new HColumnDescriptor(Bytes.toBytes("MM"))
val columnName = columndescriptor.getName()
 val c = columndescriptor.getValue("id")

 

Put categories: performing single line put operation

 

 

 

Eg:

val put = new Put(Bytes.toBytes("rowkey"))
//添加列id和值123 到MM列簇
put.addColumn(Bytes.toBytes("MM"),Bytes.toBytes("id"),Bytes.toBytes("123"))
val rowname = put.getRow()
val time = put.getTimeStamp()
val s:Boolean = put.isEmpty()

 

 

Get Class: means for performing one-way get operation

 

 

 

 

Scan type: for performing a scanning operation; different methods of scanning the entire table structure, the entire column of the cluster, the entire column of data within a range of the time stamp

Eg:

val table: Table = conn.getTable(TableName.valueOf("driver_info"))
// scan 全表
val scan = new Scan()
//给scan加条件获取列簇MM中的列id
scan.addColumn(Bytes.toBytes("MM"),Bytes.toBytes("id"))
//获取列簇MM中所有的列
scan.addFamily(Bytes.toBytes("MM"))
// 获取时间戳在11111-22222之间的所有数据
scan.setTimeRange(11111,22222)

//scan从rowkey20190101 开始到结束
val scan1 = new Scan(Bytes.toBytes("20190101"))

//scan从rowkey20190101开始到20200101结束
val scan2 = new Scan(Bytes.toBytes("20190101"),Bytes.toBytes("20200101"))

//通过扫描器scan控制查询到的结果
val rs: ResultScanner = table.getScanner(scan)

 

Filter配合Scan工作

//1.实例化filter RowFilter 按行过滤 行键小于等于20190101的所有数据
//CompareFilter.CompareOp.LESS_OR_EQUAL 比较符 LESS_OR_EQUAL 小于等于、LESS小于、EQUAL等于、NOT_EQUAL不等于、GREATER_OR_EQUAL大于等于、GREATER大于、NO_OP 无操作
val rowfilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes( "20190101")))
//实例化scan
val scan4 = new Scan()
//添加 行过滤器
scan.setFilter(rowfilter)
// 获取数据
val scanner: ResultScanner = table.getScanner(scan4)
import scala.collection.JavaConverters._
//遍历数据
for(sca <- scanner.iterator.asScala){
  println(sca)
}
//2. 实例化 QualifierFilter 扫描出所有的列名为id  的列数据
val liefilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("id")))
//3. 实例化FamilyFilter 扫描出所有的列簇为'MM'的列簇数据
val familyfilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("MM")))

// 4.扫描出行键前缀为2019 的所有行数据
val prefilter = new PrefixFilter(Bytes.toBytes("2019"))

// 5.只返回每行数据的 行键-列簇-列;不返回值。
  // 所以适用于 不需要值的应用场景, 减少值的传递
val keyonlyfilter = new KeyOnlyFilter()

// 6.扫描出所有列的值为100 的 列
val valuefilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("100")))

 

Guess you like

Origin www.cnblogs.com/yzqyxq/p/12103474.html