在CentOS上实现HBase的实时查询,可以通过以下步骤来完成:
下载HBase:
wget https://archive.apache.org/dist/hbase/2.4.9/hbase-2.4.9-bin.tar.gz 解压并移动到合适的位置:
tar -zxvf hbase-2.4.9-bin.tar.gz mv hbase-2.4.9 /opt/hbase 配置环境变量: 编辑/etc/profile.d/hbase.sh文件,添加以下内容:
export HBASE_HOME=/opt/hbase export PATH=$PATH:$HBASE_HOME/bin 然后使配置生效:
source /etc/profile.d/hbase.sh 编辑hbase-site.xml: 在$HBASE_HOME/conf目录下编辑hbase-site.xml文件,添加以下配置:
<configuration> <property> <name>hbase.rootdir</name> <value>hdfs://namenode:8020/hbase</value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>zookeeper1,zookeeper2,zookeeper3</value> </property> <property> <name>hbase.zookeeper.property.dataDir</name> <value>/var/lib/zookeeper</value> </property> </configuration> 编辑core-site.xml: 在$HBASE_HOME/conf目录下编辑core-site.xml文件,添加以下配置:
<configuration> <property> <name>fs.defaultFS</name> <value>hdfs://namenode:8020</value> </property> </configuration> 编辑hdfs-site.xml: 在$HBASE_HOME/conf目录下编辑hdfs-site.xml文件,添加以下配置:
<configuration> <property> <name>dfs.replication</name> <value>3</value> </property> </configuration> 格式化HDFS(如果尚未格式化):
hdfs namenode -format 启动HDFS:
start-dfs.sh 启动Zookeeper:
$HBASE_HOME/bin/start-zookeeper.sh 启动HBase Master:
$HBASE_HOME/bin/start-master.sh 启动HBase RegionServer: 在其他节点上执行:
$HBASE_HOME/bin/start-regionserver.sh 创建表:
hbase shell create 'my_table', 'cf1' 插入数据:
put 'my_table', 'row1', 'cf1:col1', 'value1' 打开HBase Shell:
hbase shell 扫描表:
scan 'my_table' 添加依赖: 在你的Java项目中添加HBase客户端依赖:
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.4.9</version> </dependency> 编写Java代码:
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class HBaseRealTimeQuery { public static void main(String[] args) throws Exception { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "zookeeper1,zookeeper2,zookeeper3"); config.set("hbase.zookeeper.property.clientPort", "2181"); try (Connection connection = ConnectionFactory.createConnection(config)) { Table table = connection.getTable(TableName.valueOf("my_table")); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { System.out.println(result); } } } } 通过以上步骤,你可以在CentOS上实现HBase的实时查询。根据具体需求,你可以选择使用HBase Shell或Java API进行查询。