HbaseCURD操作java API
来源:互联网 发布:证券公司 知乎 编辑:程序博客网 时间:2024/06/15 22:28
public class HbaseCURD {
private Configuration conf = null;
private Connection con = null;
/**由于是测试类,没有main方法,要提前加载一些配置项。所以用@Before * * @throws IOException */@Beforepublic void init() throws IOException { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "worker05:2181,worker06:2181,worker07:2181");}
/**
* 在Hbase中添加people表并创建两个列簇:info、data
* 并设置创建的版本为3(也就是最新版本)
* @throws IOException
*/
@Test
public void testCreateTable() throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(“people”));
HColumnDescriptor hcdInfo = new HColumnDescriptor(“info”);
HColumnDescriptor hcdData = new HColumnDescriptor(“data”);
hcdInfo.setMaxVersions(3);
htd.addFamily(hcdInfo);
htd.addFamily(hcdData);
admin.createTable(htd);
//用完关闭
admin.close();
}
/**插入数据 * @throws IOException **/
@Test
public void testPut(String tableName) throws IOException {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(“row001”));
put.addColumn(Bytes.toBytes(“info”), Bytes.toBytes(“name”), Bytes.toBytes(“lingxin”));
put.addColumn(Bytes.toBytes(“info”), Bytes.toBytes(“age”), Bytes.toBytes(“57”));
put.addColumn(Bytes.toBytes(“info”), Bytes.toBytes(“money”), Bytes.toBytes(30000));
table.put(put);
table.close();
}
/**测试插入100万条数据**/@Testpublic void testPutAll() throws IOException { HTable table = new HTable(conf, "people"); List<Put> puts = new ArrayList<Put>(10000); for(int i = 1; i < 1000000; i++) { Put put = new Put(Bytes.toBytes("row" + i)); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lingxin" + i)); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("57")); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(30000)); puts.add(put); //注意!!!不能一次写一条。即会造成资源的浪费和磁盘的损坏、更会可能使Hbase负载增加而挂掉 //每隔10000,写一次 if(i % 10000 == 0) { table.put(puts); puts = new ArrayList<Put>(10000); } } table.put(puts); table.close(); //以下方式不可取 /* for(int i=1; i<1000000; i++) { Put put = new Put(Bytes.toBytes("row"+i)); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lingxin"+i)); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("57")); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(30000)); puts.add(put); } table.put(puts); table.close();*/}
/**查看某个cell的值**/@Testpublic void testGet() throws IOException { Table table = con.getTable(TableName.valueOf("people")); Get get = new Get(Bytes.toBytes("row9999")); Result resut = table.get(get); String r = Bytes.toString(resut.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))); System.out.println(r); table.close();}/**查看某个rowkey范围的数据,按字典顺序排序**/@Testpublic void testScan() throws IOException { Table table = con.getTable(TableName.valueOf("people")); Scan scan = new Scan(Bytes.toBytes("row010000"), Bytes.toBytes("row110")); ResultScanner scanner = table.getScanner(scan); for(Result result : scanner) { String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))); System.out.println(r); } table.close();}
/**组合使用扫描器缓存和批量大小**/@Testpublic void testScanWithCacheAndBatch(int caching, int batch) throws IOException { Table table = con.getTable(TableName.valueOf("people")); Scan scan = new Scan(Bytes.toBytes("row010000"), Bytes.toBytes("row110")); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //根据提供表的特性,(宽表,还是窄表,设置不同的caching和batch值) scan.setCaching(caching);//设置每次扫描的打印行数 scan.setBatch(batch);//设置每次扫描的打印列数 //!!!!!!!!!!!!!!!!!!!!!!!!!! ResultScanner scanner = table.getScanner(scan); for(Result result : scanner) { String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))); System.out.println(r); } table.close();}@Testpublic void testDel() throws IOException { Table table = con.getTable(TableName.valueOf("people"));// HTable table = new HTable(conf, "people"); Delete delete = new Delete(Bytes.toBytes("row9999")); table.delete(delete); table.close();}
}
- HbaseCURD操作java API
- java 操作 zookeeper API
- hbase java api操作
- Java API操作HDFS
- hbase java api操作
- hbase Java Api 操作
- hbase java api操作
- Hbase-Java API操作
- Java API 操作Hbase
- Java API操作hbase
- Java API操作HDFS
- java api操作 hbase
- Java Api操作Kafka
- hdfs java api操作
- java api操作hbase
- java api操作HDFS
- Java API操作Hive
- java api操作hbase
- Ubuntu命令行下运行matlab
- 软件开发标准(文档模板)
- Arcgis Javascript那些事儿(五)--arcgis server发布自定义比例尺地图
- Android开发——AutoCompleteTextView自动提示控件
- 归并排序寻找和最大的子数组
- HbaseCURD操作java API
- 学c语言的第一步,编译器的使用
- 11.2 RAC , patch auto 方式打psu补丁
- 【NOIP提高组】神炎皇
- 多精度通用加法子程序源码
- web开发日常博客阅读记录
- ERROR 1130: Host ’192.168.1.3′ is not allowed to connect to this MySQL serve
- 《社会动物——爱、性格和成就的潜在根源》读后感及摘录(1)
- unsigned和signed间的转换