参数为:配置对象

时间:2022-05-29 08:24:42

创建maven工程,,改削jdk

参数为:配置对象

pom文件里添加需要的jar包

dependencies>

<dependency>

<groupId>jdk.tools</groupId>

<artifactId>jdk.tools</artifactId>

<version>1.8</version>

<scope>system</scope>

<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.apache.hbase</groupId>

<artifactId>hbase-it</artifactId>

<version>1.2.5</version>

<type>pom</type>

</dependency> 

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-common</artifactId>

<version>2.6.1</version>

</dependency>

</dependencies>

import java.io.IOException; import java.util.ArrayList; 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.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; 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.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class CreateTableTest { static Configuration conf = HBaseConfiguration.create();//读取hbase的配置文件 static HBaseAdmin admin = null;//执行打点员 public static void main(String[] args) throws Exception{ admin=new HBaseAdmin(conf); // createTable(); // listTable(); // deleteTable(); // listTable(); // putData(); // scanTable(); getData(); // putaLots(); } //创建表 public static void createTable() throws Exception, IOException{ //表的描述东西 HTableDescriptor table=new HTableDescriptor(TableName.valueOf("javaemp1")); //列簇 table.addFamily(new HColumnDescriptor("personal")); table.addFamily(new HColumnDescriptor("professional")); admin.createTable(table); System.out.println("create table finished"); } //列出所有的表 public static void listTable() throws IOException{ HTableDescriptor[] Tablelist = admin.listTables(); for(int i=0;i<Tablelist.length;i++) { System.out.println(Tablelist[i].getNameAsString()); } } //删除表 public static void deleteTable() throws Exception{ //删表之前先禁用表 // admin.disableTable("javaemp1"); // admin.deleteTable("javaemp1"); // System.out.println("delete finished"); //删除表中的某一列簇 // admin.deleteColumn("javaemp","professional"); // System.out.println("delete column"); //增加一列 admin.addColumn("javaemp",new HColumnDescriptor("professional")); System.out.println("add column"); } //---------------------------------------------------------------------------------------------------------- //插入数据 public static void putData() throws Exception{ //HTable类实现对单个表的操纵,参数为:配置东西,表名 HTable table = new HTable(conf,"javaemp"); Put p = new Put(Bytes.toBytes("1001"));//实例化Put类,指定rwo key来操纵 Put p1=new Put(Bytes.toBytes("1002")); // p.add(Bytes.toBytes("personal"),Bytes.toBytes("name"),Bytes.toBytes("lalisa")); // 参数:列簇,列,值 p.add(Bytes.toBytes("personal"),Bytes.toBytes("city"),Bytes.toBytes("beijing")); table.put(p); p1.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),Bytes.toBytes("it")); p1.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),Bytes.toBytes("16010")); table.put(p1); System.out.println("put data finished"); table.close();//释放HTable的资源 } //批量插入数据 public static void putaLots() throws IOException{ HTable table = new HTable(conf,"javaemp"); ArrayList<Put> list = new ArrayList<Put>(10); for (int i=0;i<10;i++) { Put put = new Put(Bytes.toBytes("row"+i)); put.add(Bytes.toBytes("personal"),Bytes.toBytes("name"),Bytes.toBytes("people"+i)); list.add(put); } table.put(list); System.out.println("put list finished"); } //获取某一列数据 public static void getData() throws IOException{ HTable table = new HTable(conf, "javaemp"); Get get = new Get(Bytes.toBytes("1001"));//实例化Get类 Result result = table.get(get);//获取这一row的数据 // 输出这一行的某一个字段 byte[] value = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name")); String name=Bytes.toString(value); System.out.println("Name:"+name); //输出这一行的所有数据 Cell[] cells = result.rawCells(); for(Cell cell:cells) { System.out.print(Bytes.toString(CellUtil.cloneRow(cell))+"--"); System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+":"); System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell))+"->"); System.out.println(Bytes.toString(CellUtil.cloneValue(cell))); } table.close();//释放HTable的资源 } //scan某一列 public static void scanTable() throws IOException{ HTable table = new HTable(conf, "javaemp"); Scan scan = new Scan(); // 实例化Scan类 scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));//scan某列簇的某列 scan.addFamily(Bytes.toBytes("professional"));//scan某列簇 ResultScanner scanner = table.getScanner(scan); for(Result res=scanner.next();res!=null;res=scanner.next()) { System.out.println(res); } table.close();//释放HTable的资源 } //统计row key的个数 public static void count(){ } //*hbase public static void close() throws IOException{ admin.shutdown(); } }