hbase java api樣例(版本1.3.1,新API)
阿新 • • 發佈:2017-09-15
quorum desc color -i arraylist byte logs sin fig
驗證了如下幾種java api的使用方法。
1.創建表
2.創建表(預分區)
3.單條插入
4.批量插入
5.批量插入(寫緩存)
6.單條get
7.批量get
8.簡單scan
具體請參考GitHub。
https://github.com/quchunhui/hbase_sample
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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"> <artifactId>hbase_sample</artifactId> <groupId>hbase_sample</groupId> <version>1.0</version> <modelVersion>4.0.0</modelVersion> <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>1.3.1</version> </dependency> </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <outputDirectory>target/classes</outputDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
===創建表===
import org.apache.hadoop.conf.Configuration; 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; public class create_table_sample1 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin(); HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("TEST1")); HColumnDescriptor family1 = new HColumnDescriptor(constants.COLUMN_FAMILY_DF.getBytes()); family1.setTimeToLive(2 * 60 * 60 * 24); //過期時間 family1.setMaxVersions(2); //版本數 desc.addFamily(family1); HColumnDescriptor family2 = new HColumnDescriptor(constants.COLUMN_FAMILY_EX.getBytes()); family2.setTimeToLive(3 * 60 * 60 * 24); //過期時間 family2.setMaxVersions(3); //版本數 desc.addFamily(family2); admin.createTable(desc); } }
===創建表(預分區)===
import org.apache.hadoop.conf.Configuration; 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.regionserver.BloomType; import org.apache.hadoop.hbase.util.Bytes; public class create_table_sample2 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin(); TableName table_name = TableName.valueOf("TEST1"); if (admin.tableExists(table_name)) { admin.disableTable(table_name); admin.deleteTable(table_name); } HTableDescriptor desc = new HTableDescriptor(table_name); HColumnDescriptor family1 = new HColumnDescriptor(constants.COLUMN_FAMILY_DF.getBytes()); family1.setTimeToLive(3 * 60 * 60 * 24); //過期時間 family1.setBloomFilterType(BloomType.ROW); //按行過濾 family1.setMaxVersions(3); //版本數 desc.addFamily(family1); HColumnDescriptor family2 = new HColumnDescriptor(constants.COLUMN_FAMILY_EX.getBytes()); family2.setTimeToLive(2 * 60 * 60 * 24); //過期時間 family2.setBloomFilterType(BloomType.ROW); //按行過濾 family2.setMaxVersions(2); //版本數 desc.addFamily(family2); byte[][] splitKeys = { Bytes.toBytes("row01"), Bytes.toBytes("row02"), Bytes.toBytes("row04"), Bytes.toBytes("row06"), Bytes.toBytes("row08"), }; admin.createTable(desc, splitKeys); } }
===單條插入===
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 org.apache.hadoop.hbase.util.Bytes; import java.util.Random; public class table_put_sample1 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Random random = new Random(); String[] rows = new String[] {"01", "02", "03"}; String[] names = new String[] {"zhang san", "li si", "wang wu", "wei liu"}; String[] sexs = new String[] {"men", "women"}; String[] heights = new String[] {"165cm", "170cm", "175cm", "180cm"}; String[] weights = new String[] {"50kg", "55kg", "60kg", "65kg", "70kg", "75kg", "80kg"}; Put put = new Put(Bytes.toBytes("row" + rows[random.nextInt(rows.length)])); String name = names[random.nextInt(names.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes(), name.getBytes()); String sex = sexs[random.nextInt(sexs.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes(), sex.getBytes()); String height = heights[random.nextInt(heights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes(), height.getBytes()); String weight = weights[random.nextInt(weights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes(), weight.getBytes()); table.put(put); table.close(); connection.close(); } }
===批量插入===
方式一:
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 org.apache.hadoop.hbase.util.Bytes; import java.util.ArrayList; import java.util.List; import java.util.Random; public class table_put_sample2 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Random random = new Random(); String[] rows = new String[] {"01", "02", "03"}; String[] names = new String[] {"zhang san", "li si", "wang wu", "wei liu"}; String[] sexs = new String[] {"men", "women"}; String[] heights = new String[] {"165cm", "170cm", "175cm", "180cm"}; String[] weights = new String[] {"50kg", "55kg", "60kg", "65kg", "70kg", "75kg", "80kg"}; List<Put> puts = new ArrayList<>(); for(String row : rows) { Put put = new Put(Bytes.toBytes("row" + row)); String name = names[random.nextInt(names.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes(), name.getBytes()); String sex = sexs[random.nextInt(sexs.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes(), sex.getBytes()); String height = heights[random.nextInt(heights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes(), height.getBytes()); String weight = weights[random.nextInt(weights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes(), weight.getBytes()); puts.add(put); } table.put(puts); table.close(); connection.close(); } }
方式二:
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 org.apache.hadoop.hbase.util.Bytes; import java.util.ArrayList; import java.util.List; import java.util.Random; public class table_put_sample3 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Random random = new Random(); String[] rows = new String[] {"01", "02", "03"}; String[] names = new String[] {"zhang san", "li si", "wang wu", "wei liu"}; String[] sexs = new String[] {"men", "women"}; String[] heights = new String[] {"165cm", "170cm", "175cm", "180cm"}; String[] weights = new String[] {"50kg", "55kg", "60kg", "65kg", "70kg", "75kg", "80kg"}; List<Row> batch = new ArrayList<>(); for(String row : rows) { Put put = new Put(Bytes.toBytes("row" + row)); String name = names[random.nextInt(names.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes(), name.getBytes()); String sex = sexs[random.nextInt(sexs.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes(), sex.getBytes()); String height = heights[random.nextInt(heights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes(), height.getBytes()); String weight = weights[random.nextInt(weights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes(), weight.getBytes()); batch.add(put); } Object[] results = new Object[batch.size()]; try { table.batch(batch, results); } catch (InterruptedException e) { e.printStackTrace(); return; } batch.clear(); table.close(); connection.close(); } }
===批量插入(寫緩存)===
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 org.apache.hadoop.hbase.util.Bytes; import java.util.ArrayList; import java.util.List; import java.util.Random; public class table_put_sample4 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); conf.set("hbase.client.write.buffer", "1048576");//1M Connection connection = ConnectionFactory.createConnection(conf); BufferedMutator table = connection.getBufferedMutator(TableName.valueOf(constants.TABLE_NAME)); System.out.print("[--------]write buffer size = " + table.getWriteBufferSize()); Random random = new Random(); String[] rows = new String[] {"01", "02", "03", "04", "05"}; String[] names = new String[] {"zhang san", "li si", "wang wu", "wei liu"}; String[] sexs = new String[] {"men", "women"}; String[] heights = new String[] {"165cm", "170cm", "175cm", "180cm"}; String[] weights = new String[] {"50kg", "55kg", "60kg", "65kg", "70kg", "75kg", "80kg"}; List<Mutation> batch = new ArrayList<>(); for(String row : rows) { Put put = new Put(Bytes.toBytes("row" + row)); String name = names[random.nextInt(names.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes(), name.getBytes()); String sex = sexs[random.nextInt(sexs.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes(), sex.getBytes()); String height = heights[random.nextInt(heights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes(), height.getBytes()); String weight = weights[random.nextInt(weights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes(), weight.getBytes()); batch.add(put); } table.mutate(batch); table.flush(); table.close(); connection.close(); } }
===單條get===
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; public class table_get_sample1 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Get get = new Get(("row01").getBytes()); Result result = table.get(get); byte[] name = result.getValue(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); byte[] weight = result.getValue(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); System.out.print("[------]name=" + new String(name) + "\n"); System.out.print("[------]name=" + new String(weight) + "\n"); } }
===批量get===
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.util.ArrayList; import java.util.List; public class table_get_sample4 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); List<Get> gets = new ArrayList<>(); Get get1 = new Get(("row01").getBytes()); get1.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); get1.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes()); get1.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes()); get1.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); gets.add(get1); Get get2 = new Get(("row02").getBytes()); get2.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); get2.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes()); get2.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes()); get2.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); gets.add(get2); Result[] results = table.get(gets); for ( Result result : results) { byte[] name = result.getValue(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); byte[] sex = result.getValue(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes()); byte[] height = result.getValue(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes()); byte[] weight = result.getValue(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); System.out.print("[------]name=" + new String(name) + "\n"); System.out.print("[------]sex=" + new String(sex) + "\n"); System.out.print("[------]height=" + new String(height) + "\n"); System.out.print("[------]weight=" + new String(weight) + "\n"); } } }
===簡單scan===
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; public class table_scan_sample3 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Scan scan = new Scan(); scan.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); scan.addFamily(constants.COLUMN_FAMILY_EX.getBytes()); ResultScanner rs = table.getScanner(scan); for (Result r = rs.next(); r != null; r = rs.next()) { byte[] row_key = r.getRow(); System.out.print("[------]row_key=" + new String(row_key) + "\n"); byte[] name = r.getValue(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); System.out.print("[------]name=" + new String(name) + "\n"); byte[] weight = r.getValue(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); System.out.print("[------]weight=" + new String(weight) + "\n"); } } }
--END--
hbase java api樣例(版本1.3.1,新API)