本文整理汇总了Java中org.apache.metamodel.DataContext.executeQuery方法的典型用法代码示例。如果您正苦于以下问题:Java DataContext.executeQuery方法的具体用法?Java DataContext.executeQuery怎么用?Java DataContext.executeQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.metamodel.DataContext
的用法示例。
在下文中一共展示了DataContext.executeQuery方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadIntoMemory
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public SimpleDictionary loadIntoMemory(final DatastoreConnection datastoreConnection) {
final DataContext dataContext = datastoreConnection.getDataContext();
final Column column = getColumn(datastoreConnection);
final Query query = dataContext.query().from(column.getTable()).select(column).toQuery();
if (datastoreConnection.getDatastore().getPerformanceCharacteristics().isQueryOptimizationPreferred()) {
query.getSelectClause().setDistinct(true);
}
final Set<String> values = new HashSet<>();
try (DataSet dataSet = dataContext.executeQuery(query)) {
while (dataSet.next()) {
final Object value = dataSet.getRow().getValue(0);
if (value != null) {
values.add(value.toString());
}
}
}
return new SimpleDictionary(getName(), values);
}
示例2: DataSetWindow
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public DataSetWindow(final Query query, final DataContext dataContext, final int pageSize,
final WindowContext windowContext) {
super(windowContext);
_table = new DCTable();
_query = query;
_pageSize = pageSize;
_title = "DataSet: " + _query.toSql();
_tableModelCallable = () -> {
final DataSet dataSet = dataContext.executeQuery(_query);
return new DataSetTableModel(dataSet);
};
_previousPageButton = WidgetFactory.createDefaultButton("Previous page", IconUtils.ACTION_BACK);
_previousPageButton.setEnabled(false);
_nextPageButton = WidgetFactory.createDefaultButton("Next page", IconUtils.ACTION_FORWARD);
_nextPageButton.setHorizontalTextPosition(SwingConstants.LEFT);
_nextPageButton.setEnabled(false);
}
示例3: getPreviewData
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
private DataSet getPreviewData(final String filename) {
if (!isPreviewDataAvailable()) {
logger.info("Not displaying preview table because isPreviewDataAvailable() returned false");
return null;
}
final D datastore = getPreviewDatastore(filename);
try (DatastoreConnection con = datastore.openConnection()) {
final DataContext dc = con.getDataContext();
final Table table = getPreviewTable(dc);
List<Column> columns = table.getColumns();
if (columns.size() > getPreviewColumns()) {
// include max 10 columns
columns = columns.stream().limit(getPreviewColumns()).collect(Collectors.toList());
}
final Query q = dc.query().from(table).select(columns).toQuery();
q.setMaxRows(7);
return dc.executeQuery(q);
}
}
示例4: testFullScenario
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testFullScenario() throws Exception {
final OutputWriterScenarioHelper scenarioHelper = new OutputWriterScenarioHelper();
final String filename = "target/test-output/csv-file1.txt";
final OutputWriter writer = CsvOutputWriterFactory.getWriter(filename, scenarioHelper.getColumns());
scenarioHelper.writeExampleData(writer);
writer.close();
final DataContext dc = DataContextFactory.createCsvDataContext(new File(filename));
final Table table = dc.getDefaultSchema().getTable(0);
final Query q = dc.query().from(table).select(table.getColumns()).toQuery();
final DataSet dataSet = dc.executeQuery(q);
scenarioHelper.performAssertions(dataSet, false);
}
示例5: getValues
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
@Override
public Collection<String> getValues() {
try (final DatastoreConnection con = _datastore.openConnection()) {
final DataContext dataContext = con.getDataContext();
final Query q = dataContext.query().from(_column.getTable()).select(_column).toQuery();
q.selectDistinct();
try (final DataSet dataSet = dataContext.executeQuery(q)) {
final List<String> values = new ArrayList<String>();
while (dataSet.next()) {
final Row row = dataSet.getRow();
Object value = row.getValue(0);
if (value != null) {
value = value.toString();
}
values.add((String) value);
}
return values;
}
}
}
示例6: testConfigurationWithoutHeader
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testConfigurationWithoutHeader() throws Exception {
File file = copyOf("src/test/resources/xls_people.xls");
DataContext dc = new ExcelDataContext(file, new ExcelConfiguration(ExcelConfiguration.NO_COLUMN_NAME_LINE,
true, true));
Table table = dc.getDefaultSchema().getTables().get(0);
String[] columnNames = table.getColumnNames().toArray(new String[0]);
assertEquals("[A, B, C, D]", Arrays.toString(columnNames));
Query q = dc.query().from(table).select(table.getColumnByName("A")).toQuery();
assertEquals("SELECT xls_people.A FROM testConfigurationWithoutHeader-xls_people.xls.xls_people", q.toSql());
DataSet dataSet = dc.executeQuery(q);
assertTrue(dataSet.next());
assertEquals("id", dataSet.getRow().getValue(0));
for (int i = 1; i <= 9; i++) {
assertTrue(dataSet.next());
assertEquals(i + "", dataSet.getRow().getValue(0));
}
assertFalse(dataSet.next());
}
示例7: testConfigurationNonDefaultColumnNameLineNumber
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testConfigurationNonDefaultColumnNameLineNumber() throws Exception {
File file = copyOf("src/test/resources/xls_people.xls");
DataContext dc = new ExcelDataContext(file, new ExcelConfiguration(2, true, true));
Table table = dc.getDefaultSchema().getTables().get(0);
String[] columnNames = table.getColumnNames().toArray(new String[0]);
assertEquals("[1, mike, male, 18]", Arrays.toString(columnNames));
Query q = dc.query().from(table).select(table.getColumnByName("1")).toQuery();
assertEquals("SELECT xls_people.1 FROM testConfigurationNonDefaultColumnNameLineNumber-xls_people.xls.xls_people", q.toSql());
DataSet dataSet = dc.executeQuery(q);
assertTrue(dataSet.next());
assertEquals("2", dataSet.getRow().getValue(0));
for (int i = 3; i <= 9; i++) {
assertTrue(dataSet.next());
assertEquals(i + "", dataSet.getRow().getValue(0));
}
assertFalse(dataSet.next());
}
示例8: testMissingValues
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testMissingValues() throws Exception {
File file = copyOf("src/test/resources/xls_missing_values.xls");
DataContext dc = new ExcelDataContext(file);
Schema schema = dc.getDefaultSchema();
assertEquals(2, schema.getTableCount());
Table table = schema.getTables().get(0);
assertEquals("[Column[name=a,columnNumber=0,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
+ "Column[name=b,columnNumber=1,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
+ "Column[name=c,columnNumber=2,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
+ "Column[name=d,columnNumber=3,type=VARCHAR,nullable=true,nativeType=null,columnSize=null]]",
Arrays.toString(table.getColumns().toArray()));
Query q = new Query().select(table.getColumns()).from(table);
DataSet ds = dc.executeQuery(q);
assertTrue(ds.next());
assertEquals("[1, 2, 3, null]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[5, null, 7, 8]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[9, 10, 11, 12]", Arrays.toString(ds.getRow().getValues()));
assertFalse(ds.next());
}
示例9: testMissingColumnHeader
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testMissingColumnHeader() throws Exception {
File file = copyOf("src/test/resources/xls_missing_column_header.xls");
DataContext dc = new ExcelDataContext(file);
Schema schema = dc.getDefaultSchema();
assertEquals(2, schema.getTableCount());
Table table = schema.getTables().get(0);
assertEquals("[Column[name=a,columnNumber=0,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
+ "Column[name=b,columnNumber=1,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
+ "Column[name=A,columnNumber=2,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
+ "Column[name=d,columnNumber=3,type=VARCHAR,nullable=true,nativeType=null,columnSize=null]]",
Arrays.toString(table.getColumns().toArray()));
Query q = new Query().select(table.getColumns()).from(table);
DataSet ds = dc.executeQuery(q);
assertTrue(ds.next());
assertEquals("[1, 2, 3, 4]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[5, 6, 7, 8]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[9, 10, 11, 12]", Arrays.toString(ds.getRow().getValues()));
assertFalse(ds.next());
}
示例10: testAlternativeConnectionString
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testAlternativeConnectionString() throws Exception {
if (!isConfigured()) {
return;
}
DataContext dc = new JdbcDataContext(getConnection(), TableType.DEFAULT_TABLE_TYPES, "sakila");
final Schema sakila = dc.getSchemaByName("sakila");
assertNotNull(sakila);
Table table = sakila.getTableByName("film");
Query q = new Query().from(table).select(table.getColumns());
DataSet data = dc.executeQuery(q);
TableModel tableModel = new DataSetTableModel(data);
assertEquals(13, tableModel.getColumnCount());
assertEquals(1000, tableModel.getRowCount());
}
示例11: testVaryingValueLengthsCorrect
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testVaryingValueLengthsCorrect() throws Exception {
DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
new FixedWidthConfiguration(new int[] { 1, 8, 7 }));
Table table = dc.getDefaultSchema().getTables().get(0);
assertEquals("[i, greeting, greeter]", Arrays.toString(table.getColumnNames().toArray()));
assertEquals(1, table.getColumnByName("i").getColumnSize().intValue());
assertEquals(8, table.getColumnByName("greeting").getColumnSize().intValue());
assertEquals(7, table.getColumnByName("greeter").getColumnSize().intValue());
Query q = dc.query().from(table).select(table.getColumns()).toQuery();
DataSet ds = dc.executeQuery(q);
assertTrue(ds.next());
assertEquals("[1, hello, world]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[2, hi, there]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[3, howdy, partner]", Arrays.toString(ds.getRow().getValues()));
assertFalse(ds.next());
}
示例12: testVaryingValueLengthsTooShortLengthErrorTolerant
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testVaryingValueLengthsTooShortLengthErrorTolerant() throws Exception {
DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
new FixedWidthConfiguration(FixedWidthConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", new int[] { 1, 5,
7 }, false));
Table table = dc.getDefaultSchema().getTables().get(0);
assertEquals("[i, greet, inggree]", Arrays.toString(table.getColumnNames().toArray()));
Query q = dc.query().from(table).select(table.getColumns()).toQuery();
DataSet ds = dc.executeQuery(q);
assertTrue(ds.next());
assertEquals("[1, hello, worl]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[2, hi, ther]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[3, howdy, part]", Arrays.toString(ds.getRow().getValues()));
assertFalse(ds.next());
}
示例13: testVaryingValueLengthsTooLongLengthErrorTolerant
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testVaryingValueLengthsTooLongLengthErrorTolerant() throws Exception {
DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
new FixedWidthConfiguration(FixedWidthConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", new int[] { 1, 8,
9 }, false));
Table table = dc.getDefaultSchema().getTables().get(0);
assertEquals("[i, greeting, greeter]", Arrays.toString(table.getColumnNames().toArray()));
Query q = dc.query().from(table).select(table.getColumns()).toQuery();
DataSet ds = dc.executeQuery(q);
assertTrue(ds.next());
assertEquals("[1, hello, world]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[2, hi, there]", Arrays.toString(ds.getRow().getValues()));
assertTrue(ds.next());
assertEquals("[3, howdy, partner]", Arrays.toString(ds.getRow().getValues()));
assertFalse(ds.next());
}
示例14: testNonConvertedQuery
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
@SuppressWarnings("resource")
public void testNonConvertedQuery() throws Exception {
MockUpdateableDataContext source = new MockUpdateableDataContext();
Column fooColumn = source.getColumnByQualifiedLabel("schema.table.foo");
assertNotNull(fooColumn);
Map<Column, TypeConverter<?, ?>> converters = new HashMap<Column, TypeConverter<?, ?>>();
converters.put(fooColumn, new StringToIntegerConverter());
DataContext converted = Converters.addTypeConverter(source, fooColumn, new StringToIntegerConverter());
// only select "bar" which is not converted
Table table = converted.getDefaultSchema().getTableByName("table");
Query query = converted.query().from(table).select("bar").toQuery();
assertEquals("SELECT table.bar FROM schema.table", query.toSql());
DataSet ds = converted.executeQuery(query);
while (ds instanceof WrappingDataSet) {
ds = ((WrappingDataSet) ds).getWrappedDataSet();
}
ds.close();
assertEquals(InMemoryDataSet.class, ds.getClass());
}
示例15: testApproximatedCountHugeFile
import org.apache.metamodel.DataContext; //导入方法依赖的package包/类
public void testApproximatedCountHugeFile() throws Exception {
DataContext dc = new CsvDataContext(getHugeFile());
Table table = dc.getDefaultSchema().getTables().get(0);
Query q = dc.query().from(table).selectCount().toQuery();
SelectItem selectItem = q.getSelectClause().getItem(0);
selectItem.setFunctionApproximationAllowed(true);
DataSet ds = dc.executeQuery(q);
assertTrue(ds.next());
Object[] values = ds.getRow().getValues();
assertEquals(1, values.length);
assertEquals(3332, ((Long) ds.getRow().getValue(selectItem)).intValue());
assertEquals(3332, ((Long) values[0]).intValue());
assertFalse(ds.next());
}