<dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.2.5</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.2.5</version> </dependency> <!-- 使用mr程序操作hbase 数据的导入 --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-mapreduce</artifactId> <version>2.2.5</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <!-- phoenix 凤凰 用来整合Hbase的工具 --> <dependency> <groupId>org.apache.phoenix</groupId> <artifactId>phoenix-core</artifactId> <version>5.0.0-HBase-2.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>3.1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <!-- bind to the packaging phase --> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
package com.doit.day01; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; 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 java.io.IOException; /** * Hbase的java客户端连接hbase的时候,只需要连接zookeeper的集群 * 就可以找到你Hbase集群的位置 * 核心的对象: * Configuration:HbaseConfiguration.create(); * Connection:ConnectionFactory.createConnection(conf); * table:conn.getTable(TableName.valueOf("tb_b")); 对表进行操作 DML * Admin:conn.getAdmin();操作Hbase系统DDL,对名称空间等进行操作 */ public class ConnectionDemo { public static void main(String[] args) throws Exception { //获取到hbase的配置文件对象 Configuration conf = HBaseConfiguration.create(); //针对配置文件设置zk的集群地址 conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181"); //创建hbase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); //获取到操作hbase的对象 Admin admin = conn.getAdmin(); //调用api获取到所有的表 TableName[] tableNames = admin.listTableNames(); //获取到哪个命名空间下的所有的表 TableName[] doits = admin.listTableNamesByNamespace("doit"); for (TableName tableName : doits) { byte[] name = tableName.getName(); System.out.println(new String(name)); } conn.close(); } }
package com.doit.day01; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; /** * Hbase的java客户端连接hbase的时候,只需要连接zookeeper的集群 * 就可以找到你Hbase集群的位置 * 核心的对象: * Configuration:HbaseConfiguration.create(); * Connection:ConnectionFactory.createConnection(conf); * Admin:conn.getAdmin();操作Hbase系统DDL,对名称空间等进行操作 */ public class NameSpaceDemo { public static void main(String[] args) throws Exception { //获取到hbase的配置文件对象 Configuration conf = HBaseConfiguration.create(); //针对配置文件设置zk的集群地址 conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181"); //创建hbase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); //获取到操作hbase的对象 Admin admin = conn.getAdmin(); //获取到命名空间的描述器 NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors(); for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) { //针对描述器获取到命名空间的名称 String name = namespaceDescriptor.getName(); System.out.println(name); } conn.close(); } }
package com.doit.day01; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.NamespaceDescriptor; 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.protobuf.generated.HBaseProtos; import java.util.Properties; /** * Hbase的java客户端连接hbase的时候,只需要连接zookeeper的集群 * 就可以找到你Hbase集群的位置 * 核心的对象: * Configuration:HbaseConfiguration.create(); * Connection:ConnectionFactory.createConnection(conf); * Admin:conn.getAdmin();操作Hbase系统DDL,对名称空间等进行操作 */ public class CreateNameSpaceDemo { public static void main(String[] args) throws Exception { //获取到hbase的配置文件对象 Configuration conf = HBaseConfiguration.create(); //针对配置文件设置zk的集群地址 conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181"); //创建hbase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); //获取到操作hbase的对象 Admin admin = conn.getAdmin(); //获取到命名空间描述器的构建器 NamespaceDescriptor.Builder spaceFromJava = NamespaceDescriptor.create("spaceFromJava"); //当然还可以给命名空间设置属性 spaceFromJava.addConfiguration("author","robot_jiang"); spaceFromJava.addConfiguration("desc","this is my first java namespace..."); //拿着构建器构建命名空间的描述器 NamespaceDescriptor build = spaceFromJava.build(); //创建命名空间 admin.createNamespace(build); conn.close(); } }
package com.doit.day01; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.protobuf.generated.TableProtos; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Map; import java.util.Set; /** * Hbase的java客户端连接hbase的时候,只需要连接zookeeper的集群 * 就可以找到你Hbase集群的位置 * 核心的对象: * Configuration:HbaseConfiguration.create(); * Connection:ConnectionFactory.createConnection(conf); * Admin:conn.getAdmin();操作Hbase系统DDL,对名称空间等进行操作 */ public class CreateTableDemo { public static void main(String[] args) throws Exception { //获取到hbase的配置文件对象 Configuration conf = HBaseConfiguration.create(); //针对配置文件设置zk的集群地址 conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181"); //创建hbase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); //获取到操作hbase操作表的对象 TableDescriptorBuilder java = TableDescriptorBuilder.newBuilder(TableName.valueOf("java")); //表添加列族需要集合的方式 ArrayList<ColumnFamilyDescriptor> list = new ArrayList<>(); //构建一个列族的构造器 ColumnFamilyDescriptorBuilder col1 = ColumnFamilyDescriptorBuilder.newBuilder("f1".getBytes(StandardCharsets.UTF_8)); ColumnFamilyDescriptorBuilder col2 = ColumnFamilyDescriptorBuilder.newBuilder("f2".getBytes(StandardCharsets.UTF_8)); ColumnFamilyDescriptorBuilder col3 = ColumnFamilyDescriptorBuilder.newBuilder("f3".getBytes(StandardCharsets.UTF_8)); //构建列族 ColumnFamilyDescriptor build1 = col1.build(); ColumnFamilyDescriptor build2 = col2.build(); ColumnFamilyDescriptor build3 = col3.build(); //将列族添加到集合中去 list.add(build1); list.add(build2); list.add(build3); //给表设置列族 java.setColumnFamilies(list); //构建表的描述器 TableDescriptor build = java.build(); //创建表 admin.createTable(build); conn.close(); } }
package com.doit.day01; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; /** * 注意:put数据需要指定往哪个命名空间的哪个表的哪个rowKey的哪个列族的哪个列中put数据,put的值是什么 */ public class PutDataDemo { public static void main(String[] args) throws Exception { //获取到hbase的配置文件对象 Configuration conf = HBaseConfiguration.create(); //针对配置文件设置zk的集群地址 conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181"); //创建hbase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); //指定往哪一张表中put数据 Table java = conn.getTable(TableName.valueOf("java")); //创建put对象,设置rowKey Put put = new Put("rowkey_001".getBytes(StandardCharsets.UTF_8)); put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8),"xiaotao".getBytes(StandardCharsets.UTF_8)); put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"age".getBytes(StandardCharsets.UTF_8),"42".getBytes(StandardCharsets.UTF_8)); Put put1 = new Put("rowkey_002".getBytes(StandardCharsets.UTF_8)); put1.addColumn("f1".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8),"xiaotao".getBytes(StandardCharsets.UTF_8)); put1.addColumn("f1".getBytes(StandardCharsets.UTF_8),"age".getBytes(StandardCharsets.UTF_8),"42".getBytes(StandardCharsets.UTF_8)); Put put2 = new Put("rowkey_003".getBytes(StandardCharsets.UTF_8)); put2.addColumn("f1".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8),"xiaotao".getBytes(StandardCharsets.UTF_8)); put2.addColumn("f1".getBytes(StandardCharsets.UTF_8),"age".getBytes(StandardCharsets.UTF_8),"42".getBytes(StandardCharsets.UTF_8)); java.put(Arrays.asList(put,put1,put2)); conn.close(); } }
package com.doit.day01; 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.TableName; import org.apache.hadoop.hbase.client.*; import java.nio.charset.StandardCharsets; /** * 注意:put数据需要指定往哪个命名空间的哪个表的哪个rowKey的哪个列族的哪个列中put数据,put的值是什么 */ public class GetDataDemo { public static void main(String[] args) throws Exception { //获取到hbase的配置文件对象 Configuration conf = HBaseConfiguration.create(); //针对配置文件设置zk的集群地址 conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181"); //创建hbase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); //指定往哪一张表中put数据 Table java = conn.getTable(TableName.valueOf("java")); Get get = new Get("rowkey_001".getBytes(StandardCharsets.UTF_8)); // get.addFamily("f1".getBytes(StandardCharsets.UTF_8)); get.addColumn("f1".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8)); Result result = java.get(get); boolean advance = result.advance(); if(advance){ Cell current = result.current(); String family = new String(CellUtil.cloneFamily(current)); String qualifier = new String(CellUtil.cloneQualifier(current)); String row = new String(CellUtil.cloneRow(current)); String value = new String(CellUtil.cloneValue(current)); System.out.println(row+","+family+","+qualifier+","+value); } conn.close(); } }
package com.doit.day01; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Iterator; /** * 注意:put数据需要指定往哪个命名空间的哪个表的哪个rowKey的哪个列族的哪个列中put数据,put的值是什么 */ public class ScanDataDemo { public static void main(String[] args) throws Exception { //获取到hbase的配置文件对象 Configuration conf = HBaseConfiguration.create(); //针对配置文件设置zk的集群地址 conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181"); //创建hbase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); //指定往哪一张表中put数据 Table java = conn.getTable(TableName.valueOf("java")); Scan scan = new Scan(); scan.withStartRow("rowkey_001".getBytes(StandardCharsets.UTF_8)); scan.withStopRow("rowkey_004".getBytes(StandardCharsets.UTF_8)); ResultScanner scanner = java.getScanner(scan); Iterator<Result> iterator = scanner.iterator(); while (iterator.hasNext()){ Result next = iterator.next(); while (next.advance()){ Cell current = next.current(); String family = new String(CellUtil.cloneFamily(current)); String row = new String(CellUtil.cloneRow(current)); String qualifier = new String(CellUtil.cloneQualifier(current)); String value = new String(CellUtil.cloneValue(current)); System.out.println(row+","+family+","+qualifier+","+value); } } conn.close(); } }
package com.doit.day02; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import java.io.IOException; import java.nio.charset.StandardCharsets; public class _12_删除一行数据 { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","linux01"); Connection conn = ConnectionFactory.createConnection(conf); Table java = conn.getTable(TableName.valueOf("java")); Delete delete = new Delete("rowkey_001".getBytes(StandardCharsets.UTF_8)); java.delete(delete); } }
传统的行式数据库将一个个完整的数据行存储在数据页中
列式数据库是将同一个数据列的各个值存放在一起
传统行式数据库的特性如下:
列式数据库的特性如下:
列族式存储是一种非关系型数据库存储方式,按列而非行组织数据。它的数据模型是面向列的,即把数据按照列族的方式组织,将属于同一列族的数据存储在一起。每个列族都有一个唯一的标识符,一般通过列族名称来表示。它具有高效的写入和查询性能,能够支持极大规模的数据
在conf目录下的hbase-site.xml文件中配置了数据存储的路径在hdfs上
<property> <name>hbase.rootdir</name> <value>hdfs://linux01:8020/hbase</value> </property>
hdfs上的存储路径: