HBase共有HMaster(老板)、Zookeeper(秘书)、RegionServer(员工)三个模块
我们先进入shell,进入hbase目录下的bin
./hbase shell
使用help获得全部命令的列表,使用help ‘xxx’获得xxx命令的详细信息
help ‘status’
status
list
第一个参数是表名称,后面是列蔟
create ‘hbase_demo’,‘family1’,‘family2’
describe ‘hbase_demo’
alter ‘hbase_demo’, ‘family3’
alter ‘hbase_demo’, {NAME => ‘family3’, METHOD => ‘delete’}
put ‘hbase_demo’, ‘rowkey1’,‘family1:name’,‘file1.txt’
put ‘hbase_demo’, ‘rowkey1’,‘family1:type’,‘txt’
put ‘hbase_demo’, ‘rowkey1’,‘family1:size’,‘1024’
put ‘hbase_demo’, ‘rowkey1’,‘family2:path’,’/home’
put ‘hbase_demo’, ‘rowkey1’,‘family2:creator’,‘tom’
put ‘hbase_demo’, ‘rowkey2’,‘family1:name’,‘file2.jpg’
put ‘hbase_demo’, ‘rowkey2’,‘family1:type’,‘jpg’
put ‘hbase_demo’, ‘rowkey2’,‘family1:size’,‘2048’
put ‘hbase_demo’, ‘rowkey2’,‘family2:path’,’/home/pic’
put ‘hbase_demo’, ‘rowkey2’,‘family2:creator’,‘jerry’
rowkey、列蔟、列形成唯一,比如先执行:
put ‘hbase_demo’, ‘rowkey1’,‘family1:name’,‘file1.txt’
在执行
put ‘hbase_demo’, ‘rowkey1’,‘family1:name’,‘file2222.txt’
最终结果以file2222.txt为主
count ‘hbase_demo’
get ‘hbase_demo’, ‘rowkey1’
get ‘hbase_demo’, ‘rowkey1’, ‘family1’
scan ‘hbase_demo’
scan ‘hbase_demo’, {COLUMN=>‘family1’}
scan ‘hbase_demo’, {COLUMNS=> ‘family1:name’}
除了列(COLUMNS)修饰词外,HBase还支持Limit(限制查询结果行数),STARTROW(ROWKEY起始行。会先根据这个key定位到region,再向后扫描)、STOPROW(结束行)、TIMERANGE(限定时间戳范围)、VERSIONS(版本数)、和FILTER(按条件过滤行)等。比如我们从RowKey1这个rowkey开始,找下一个行的最新版本
scan ‘hbase_demo’, { STARTROW => ‘rowkey1’, LIMIT=>1, VERSIONS=>1}
限制名称为file1.txt
scan ‘hbase_demo’, FILTER=>“ValueFilter(=,‘name:file21.txt’)”
filter中支持多个过滤条件通过括号、AND和OR的条件组合
scan ‘hbase_demo’, FILTER=>“ColumnPrefixFilter(‘typ’) AND ValueFilter ValueFilter(=,‘substring:10’)”
delete ‘hbase_demo’,‘rowkey1’,‘family1:size’
get ‘hbase_demo’,‘rowkey1’,‘family1:size’
deleteall ‘hbase_demo’,‘rowkey1’
get ‘hbase_demo’,‘rowkey1’
enable ‘hbase_demo’
is_enabled ‘hbase_demo’
disable ‘hbase_demo’
is_disabled ‘hbase_demo’
exists ‘hbase_demo’
disable ‘hbase_demo’
drop ‘hbase_demo’
org.springframework.data
spring-data-hadoop-boot
2.5.0.RELEASE
javax.servlet
servlet-api
org.springframework.data
spring-data-hadoop
2.5.0.RELEASE
org.slf4j
slf4j-log4j12
log4j
log4j
javax.servlet
servlet-api
org.apache.hbase
hbase-client
1.4.4
org.slf4j
slf4j-log4j12
log4j
log4j
javax.servlet
servlet-api
org.apache.logging.log4j
log4j-1.2-api
2.11.0
com.alibaba
fastjson
1.2.47
org.springframework.boot
spring-boot-starter-test
test
junit
junit
4.12
test
这里根据大家自己的hbase集群ip,博主是在电脑上配置类profile
hbase:
config:
hbase.zookeeper.quorum: master,node1,node2
hbase.zookeeper.property.clientPort: 2181
HBaseConfig:
package codemperor.hbase.config;
import java.util.Map;
import java.util.Set;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
@Configuration
@EnableConfigurationProperties(HBaseProperties.class)
public class HBaseConfig {
private final HBaseProperties properties;
public HBaseConfig(HBaseProperties properties) {
this.properties = properties;
}
@Bean
public HbaseTemplate hbaseTemplate() {
HbaseTemplate hbaseTemplate = new HbaseTemplate();
hbaseTemplate.setConfiguration(configuration());
hbaseTemplate.setAutoFlush(true);
return hbaseTemplate;
}
public org.apache.hadoop.conf.Configuration configuration() {
org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
Map config = properties.getConfig();
Set keySet = config.keySet();
for (String key : keySet) {
configuration.set(key, config.get(key));
}
return configuration;
}
}
HBaseProperties:
package codemperor.hbase.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Map;
@ConfigurationProperties(prefix = "hbase")
public class HBaseProperties {
private Map config;
public Map getConfig() {
return config;
}
public void setConfig(Map config) {
this.config = config;
}
}
HBaseSpringbootTest:
package codemperor.hbase;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.RowMapper;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class HBaseSpringbootTest {
@Autowired
private HbaseTemplate hbaseTemplate;
@Test
public void putTest() {
hbaseTemplate.put("hbase_demo", "rowKey1", "family1", "column1", Bytes.toBytes("test for data"));
}
@Test
public void getTest() {
HBaseMapper mapper = new HBaseMapper();
hbaseTemplate.get("hbase_demo", "rowKey1", mapper);
}
public class HBaseMapper implements RowMapper {
@Override
public Object mapRow(Result result, int i) throws Exception {
System.out.println("rowkey=" + Bytes.toString(result.getRow()));
System.out.println("value=" + Bytes
.toString(result.getValue(Bytes.toBytes("family1"), Bytes.toBytes("column1"))));
return result;
}
}
}
上面我们在博主阿里云上存入了一条数据:
rowKey1 column=family1:column1, timestamp=1557658487147, value=test for data