HBASE的安装配置

    xiaoxiao2023-10-28  36

    HBASE的安装配置

    安装

    1、下载压缩包 地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/stable/ 选择稳定版hbase-1.4.9-bin.tar.gz

    2、将压缩包通过xftp传到xshell中解压到/opt/module目录下

    tar -zxvf ~/hbase-1.4.9-bin.tar.gz -C /opt/modle

    3、配置环境变量 在/etc/profile文件里添加HBase安装路径的配置信息,之后用source命令使配置生效。

    export HBASE_HOME=/opt/module/hbase-1.4.9 export PATH=$HBASE_HOME/bin:$PATH

    4、执行命令hbase测试是否安装成功

    配置(伪分布式模式)

    配置文件位于HBase安装路径的conf目录(/opt/module/hbase/conf)下面 1、配置hbase-env.sh ①、设置Java安装路径 ②、设置HBase的配置文件路径 ③、采用HBase自带Zookeeper,设置参数true

    2、配置hbase-site.xml

    <!--hbase共享目录,持久化hbase数据--> <!--配置为core-site.xml 中的fs.defaultFS --> <property> <name>hbase.rootdir</name> <value>hdfs://bigdata128:9000/hbase</value> </property> <!--分布式运行模式,false(默认)为单机模式--> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <!--Zookeeper集群的地址列表,伪分布式用默认localhost--> <property> <name>hbase.zookeeper.quorum</name> <value>localhost</value> </property>

    3、启动并运行HBase(之前启动Hadoop) 启动HBase,并jps查看 命令:start-hbase.sh

    进入HBASE数据库

    进入HBase的shell命令行模式 完成数据库基本操作(建表,增删改数据,删除表格…)

    编程调试数据表操作的Java程序

    在eclipse运行Java程序打包至xshell运行 程序代码如下:

    package hbase.tables; import java.io.IOException; import java.util.Scanner; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; public class HbaseTables { public static Configuration conf; public static Connection con; public static Admin adm; @SuppressWarnings("all") public static void init() throws IOException { conf=HBaseConfiguration.create(); conf.set("hbase.rootdir", "hdfs://hadoop:9000/hbase"); con= ConnectionFactory.createConnection(conf); adm = con.getAdmin(); System.out.println(adm); } public static void createTable(String myTableName, String[] colFamily) throws IOException { init(); TableName tableName = TableName .valueOf(myTableName); if (adm.tableExists(tableName)) { System.out.println("table is exists!"); }else { HTableDescriptor htd=new HTableDescriptor(tableName); for(String str:colFamily) { HColumnDescriptor hcd =new HColumnDescriptor(str); htd.addFamily(hcd); } adm.createTable(htd); } close(); } public static void close() { try { if (adm != null) { adm.close(); } if (con != null) { con.close(); } }catch (IOException e) { e.printStackTrace();} } public static void deleteTable(String myTableName) throws IOException { init(); TableName tableName = TableName .valueOf(myTableName); if (adm.tableExists(tableName)) { adm.disableTable(tableName); adm.deleteTable(tableName); } close(); } public static void listTables() throws IOException { init(); HTableDescriptor htds[] =adm.listTables(); for(HTableDescriptor htd : htds) { System.out.println(htd.getNameAsString()); } close(); } public static void insertRow(String myTableName, String rowKey, String colFamily, String col, String val) throws IOException { init(); TableName tableName = TableName .valueOf(myTableName); @SuppressWarnings("deprecation") HTable table = new HTable(conf,tableName); Put put=new Put(rowKey.getBytes()); put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); table.put(put); table.close(); close(); } private static void deleteRow(String myTableName, String rowKey, String colFamily, String col) throws IOException { init(); TableName tableName =TableName .valueOf(myTableName); @SuppressWarnings("deprecation") HTable table = new HTable(conf, tableName); Delete delete=new Delete(rowKey.getBytes()); delete.addFamily(Bytes.toBytes(colFamily)); delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col)); table.delete(delete); table.close(); close(); } public static void getData(String myTableName, String rowKey, String colFamily, String col) throws IOException { init(); TableName tableName = TableName .valueOf(myTableName); @SuppressWarnings("deprecation") HTable table = new HTable(conf, tableName); Get get= new Get(rowKey.getBytes()); Result result = table.get(get); showCell(result); table.close(); close(); } private static void showCell(Result result) { Cell[] cells = result.rawCells(); for (Cell cell : cells) { System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " "); System.out.println("Timetamp:" + cell.getTimestamp() + " "); System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " "); System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " "); System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " "); } } public static void main(String[] args) throws IOException { System.out.println("*****Please enter the number:1.createtable/2.insertRow/3.getData/4.deleteRow/5.listTables/6.deleteTable*****"); for(int j=0;j<7;j++) { int i = 0; @SuppressWarnings("resource") Scanner scan = new Scanner(System.in); i = scan.nextInt(); switch (i) { case 1: System.out.println("please enter tablename:"); String tbn = scan.next(); String[] cf = {"cf1,cf2"}; HbaseTables.createTable(tbn, cf); System.out.println("createTable success!!!"); break; case 2: System.out.println("please enter tablename:"); String tbn1 = scan.next(); System.out.println("please enter rowkey:"); String rk1 = scan.next(); System.out.println("please enter column:"); String clm1 = scan.next(); System.out.println("please enter colname:"); String cn1 = scan.next(); System.out.println("please enter colvalue:"); String cv1 = scan.next(); HbaseTables.insertRow(tbn1, rk1, clm1, cn1, cv1); System.out.println("insertRow success!!!"); break; case 3: System.out.println("please enter tablename:"); String tbn2 = scan.next(); System.out.println("please enter rowkey:"); String rk2 = scan.next(); System.out.println("please enter colname:"); String cn2 = scan.next(); System.out.println("please enter colvalue:"); String cv2 = scan.next(); HbaseTables.getData(tbn2, rk2, cn2, cv2); System.out.println("getData success!!!"); break; case 4: System.out.println("please enter tablename:"); String tbn3 = scan.next(); System.out.println("please enter rowkey:"); String rk3 = scan.next(); System.out.println("please enter column:"); String clm3 = scan.next(); System.out.println("please enter colname:"); String cn3 = scan.next(); HbaseTables.deleteRow(tbn3, rk3, clm3, cn3); System.out.println("deleteRow success!!!"); break; case 5: HbaseTables.listTables(); System.out.println("listTables success!!!"); break; case 6: System.out.println("please enter tablename:"); String tbn4 = scan.next(); HbaseTables.deleteTable(tbn4); System.out.println("deleteTable success!!!"); break; default: System.out.println("input error!!!"); break; } } } }

    pom.xml代码:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bla</groupId> <artifactId>HBASE</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-it</artifactId> <version>1.4.9</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId> maven-assembly-plugin </artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>hbase.tables.HbaseTables</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

    运行程序后根据提示进行相应操作即可完成数据库操作 网页显示:IP地址:60010

    最新回复(0)