hbase-scala

1:创建一个名为HELLO的表:

>create 'HELLO','id','address','info'
2:插入数据:

>put'HELLO','10','info:age','24'
>put'HELLO','12','info:age','24'
>put'HELLO','12','info:city','shanghai'
......
3:删除数据:

>disable 'HELLO'
>is_enabled 'HELLO'
>alter 'HELLO',{NAME=>'id',METHOD=>'delete'}
>enable'HELLO'

删除表:

>disable 'HELLO'
>drop 'HELLO'
>exists 'HELLO'检查表是否存在

4:获取数据:

>get'HELLO','10'
>get'HELLO','10','info'
>get'HELLO','10','info:age'
5:查询表中数据多少行:
>count'HELLO'
6:删除整行:

>deleteall'HELLO','10'

7:增加字段:

>incr 'HELLO','10','info:city'

8:scan浏览表中的数据:

>scan "HELLO"

ROW                     COLUMN+CELL                                                        
 10                     column=info:age, timestamp=1513232005994, value=24                 
 12                     column=address:city, timestamp=1513143006267, value=hefei          
 12                     column=address:contry, timestamp=1513142953004, value=china        
 12                     column=address:province, timestamp=1513142991236, value=anhui      
 12                     column=info:age, timestamp=1513142508015, value=24                 
 12                     column=info:birthday, timestamp=1513142882018, value=2017          
 12                     column=info:company, timestamp=1513142919588, value=hpe
9:scala读取hbase数据

import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.{HColumnDescriptor, HTableDescriptor, TableName, HBaseConfiguration}
import org.apache.hadoop.hbase.client._
import org.apache.spark.SparkContext
import scala.collection.JavaConversions._
import org.apache.hadoop.fs.Path
import kafka.utils.ZkUtils

val hbaseConf = HBaseConfiguration.create()
hbaseConf.addResource(new Path("/etc/alternatives/hbase-conf/hbase-site.xml"))
val zkClientAndConnection = ZkUtils.createZkClientAndConnection(zkUrl, sessionTimeOut, connectionTimeOut)
val conn = ConnectionFactory.createConnection(hbaseConf)
val hbaseTableName = "HELLO"
val table = conn.getTable(TableName.valueOf(hbaseTableName))
//将'10'的age数据改为25
val p = new Put("10".getBytes)
p.addColumn("info".getBytes,"age".getBytes,"25".getBytes)
table.put(p)
//查询某条数据
val g = new Get("10".getBytes)
val result = table.get(g)
val value = Bytes.toString(result.getValue("info".getBytes,"age".getBytes))
//扫描数据
val scan = new Scan()
scan.addColumn("info".getBytes,"age".getBytes)
val scanner = table.getScanner(scan)

val r = scanner.next() //数据10的信息
Bytes.toString(r.value)//返回结果:String = 25

val r1 = scanner.next() //数据12的信息
Bytes.toString(r1.value)//返回结果:String = 24
//删除某条数据
val d = new Delete("10".getBytes)
d.addColumn("info".getBytes,"age".getBytes)
table.delete(d)
//删除某表
val hbaseConf = HBaseConfiguration.create()
hbaseConf.addResource(new Path("/etc/alternatives/hbase-conf/hbase-site.xml"))
val conn = ConnectionFactory.createConnection(hbaseConf)
val admin = conn.getAdmin()
val hbaseTableName = "HELLO"
val hbaseTable = TableName.valueOf(hbaseTableName)
admin.disableTable(hbaseTable)
admin.deleteTable(hbaseTable)











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值